Skip to content

Commit

Permalink
#117 added possibility to add teams as revviewer (#118)
Browse files Browse the repository at this point in the history
* 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 <JohanM@centiro.se>
  • Loading branch information
johmara and Johan Martinson committed Oct 16, 2023
1 parent ec7266a commit dac5353
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/><br/>**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.<br/><br/>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.<br/><br/>**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.<br/><br/>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

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
39 changes: 28 additions & 11 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
});
}
}
}
};
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ try {
);
}

const teamIsPullRequestReviewer = core.getBooleanInput(
'teamIsPullRequestReviewer',
{
required: false
}
);

// Get octokit
const octokit = github.getOctokit(gitHubToken);

Expand All @@ -68,7 +75,8 @@ try {
removePreviousAssignees,
allowNoAssignees,
allowSelfAssign,
manualIssueNumber
manualIssueNumber,
teamIsPullRequestReviewer
});
} catch (error) {
core.setFailed(error.message);
Expand Down

0 comments on commit dac5353

Please sign in to comment.