From dac53531aadae1265b0547f4def485e3364a9e37 Mon Sep 17 00:00:00 2001 From: Johan Martinson Date: Mon, 16 Oct 2023 18:40:42 +0200 Subject: [PATCH] #117 added possibility to add teams as revviewer (#118) * fix so that you can add the team it self as the reviewer * fix so that you can add the team it self as the reviewer * fix so that you can add the team it self as the reviewer * fix so that you can add the team it self as the reviewer * fix README.md --------- Co-authored-by: Johan Martinson --- README.md | 21 +++++++++++---------- action.yml | 4 ++++ src/action.js | 39 ++++++++++++++++++++++++++++----------- src/index.js | 10 +++++++++- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d3d74c84e..098f3008b 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,17 @@ ## Inputs -| Parameter | Type | Required | Default | Description | -| -------------------------- | ------- | ------------------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `assignees` | String | only if `teams` is not specified | n/a | Comma separated list of user names. Issue will be assigned to those users. | -| `teams` | String | only if `assignees` is not specified | n/a | Comma separated list of team names without the org prefix. Issue will be assigned to the team members.

**Important Requirement:** if using the `teams` input parameter, you need to use a personal access token with `read:org` scope (the default `GITHUB_TOKEN` is not enough). | -| `numOfAssignee` | Number | false | n/a | Number of assignees that will be randomly picked from the teams or assignees. If not specified, assigns all users. | -| `abortIfPreviousAssignees` | Boolean | false | false | Flag that aborts the action if there were assignees previously. | -| `removePreviousAssignees` | Boolean | false | false | Flag that removes assignees before assigning them (useful the issue is reasigned). | -| `allowNoAssignees` | Boolean | false | false | Flag that prevents the action from failing when there are no assignees. | -| `allowSelfAssign` | Boolean | false | true | Flag that allows self-assignment to the issue author.

This flag is ignored when working with PRs as self assigning a PR for review is forbidden by GitHub. | -| `issueNumber` | Number | false | n/a | Allows to override the issue number. This can be useful when context is missing. | +| Parameter | Type | Required | Default | Description | +| --------------------------- | ------- | ------------------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `assignees` | String | only if `teams` is not specified | n/a | Comma separated list of user names. Issue will be assigned to those users. | +| `teams` | String | only if `assignees` is not specified | n/a | Comma separated list of team names without the org prefix. Issue will be assigned to the team members.

**Important Requirement:** if using the `teams` input parameter, you need to use a personal access token with `read:org` scope (the default `GITHUB_TOKEN` is not enough). | +| `numOfAssignee` | Number | false | n/a | Number of assignees that will be randomly picked from the teams or assignees. If not specified, assigns all users. | +| `abortIfPreviousAssignees` | Boolean | false | false | Flag that aborts the action if there were assignees previously. | +| `removePreviousAssignees` | Boolean | false | false | Flag that removes assignees before assigning them (useful the issue is reasigned). | +| `allowNoAssignees` | Boolean | false | false | Flag that prevents the action from failing when there are no assignees. | +| `allowSelfAssign` | Boolean | false | true | Flag that allows self-assignment to the issue author.

This flag is ignored when working with PRs as self assigning a PR for review is forbidden by GitHub. | +| `issueNumber` | Number | false | n/a | Allows to override the issue number. This can be useful when context is missing. | +| `teamIsPullRequestReviewer` | Boolean | false | false | Sets team as the PR reviewer instead of a member of the team. | ## Examples diff --git a/action.yml b/action.yml index 43207d189..eb552a243 100644 --- a/action.yml +++ b/action.yml @@ -32,6 +32,10 @@ inputs: issueNumber: description: 'Manually specified issue (or PR) ID to be used instead of the one in the context.' required: false + teamIsPullRequestReviewer: + description: 'Flag that allows a team to be set as reviewer' + required: false + default: false runs: using: 'node16' diff --git a/src/action.js b/src/action.js index 38b983fcf..f1c627c18 100644 --- a/src/action.js +++ b/src/action.js @@ -20,6 +20,7 @@ const { * @param {boolean} parameters.allowNoAssignees * @param {boolean} parameters.allowSelfAssign * @param {number} parameters.manualIssueNumber + * @param {boolean} parameters.teamIsPullRequestReviewer */ const runAction = async (octokit, context, parameters) => { const { @@ -30,7 +31,8 @@ const runAction = async (octokit, context, parameters) => { removePreviousAssignees = false, allowNoAssignees = false, allowSelfAssign = true, - manualIssueNumber = 0 + manualIssueNumber = 0, + teamIsPullRequestReviewer = false } = parameters; // Check assignees and teams parameters @@ -138,25 +140,40 @@ const runAction = async (octokit, context, parameters) => { // Assign PR reviewers if (!isIssue) { - // Remove author from reviewers - const newReviewers = [...newAssignees]; - const foundIndex = newReviewers.indexOf(author); - if (foundIndex !== -1) { - newReviewers.splice(foundIndex, 1); - } - - if (newReviewers.length > 0) { + if (teamIsPullRequestReviewer) { console.log( `Setting reviewers for PR ${issueNumber}: ${JSON.stringify( - newReviewers + teams )}` ); + await octokit.rest.pulls.requestReviewers({ owner, repo, pull_number: issueNumber, - reviewers: newReviewers + reviewers: teams }); + } else { + // Remove author from reviewers + const newReviewers = [...newAssignees]; + const foundIndex = newReviewers.indexOf(author); + if (foundIndex !== -1) { + newReviewers.splice(foundIndex, 1); + } + + if (newReviewers.length > 0) { + console.log( + `Setting reviewers for PR ${issueNumber}: ${JSON.stringify( + newReviewers + )}` + ); + await octokit.rest.pulls.requestReviewers({ + owner, + repo, + pull_number: issueNumber, + reviewers: newReviewers + }); + } } } }; diff --git a/src/index.js b/src/index.js index 2de4c5f64..3533b92ec 100644 --- a/src/index.js +++ b/src/index.js @@ -53,6 +53,13 @@ try { ); } + const teamIsPullRequestReviewer = core.getBooleanInput( + 'teamIsPullRequestReviewer', + { + required: false + } + ); + // Get octokit const octokit = github.getOctokit(gitHubToken); @@ -68,7 +75,8 @@ try { removePreviousAssignees, allowNoAssignees, allowSelfAssign, - manualIssueNumber + manualIssueNumber, + teamIsPullRequestReviewer }); } catch (error) { core.setFailed(error.message);