Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #223 from nodaguti/assign-reviewers
Browse files Browse the repository at this point in the history
Send review requests when assigning reviewers
  • Loading branch information
tetsuharuohzeki authored Aug 18, 2021
2 parents 7fd11b1 + ecfd337 commit 86e6513
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/operations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { removeStateLabels } from './labels.js';
import { isPullRequest } from './pulls.js';

/**
* By the document, we need remove & add assignees to replace them.
Expand Down Expand Up @@ -43,6 +44,44 @@ async function replaceAssignees(
return changeAssign;
}

/**
* @param {import('@octokit/rest').Octokit} aOctoKit
* @param {string} owner
* @param {string} repo
* @param {number} issueNum
* @param {Array<string>} currentAssignees
* @param {Array<string>} nextAssignees
*/
async function replaceReviewRequests(
aOctoKit,
owner,
repo,
issueNum,
currentAssignees,
nextAssignees
) {
if (currentAssignees.length > 0) {
await aOctoKit.pulls.removeRequestedReviewers({
owner,
repo,
// eslint-disable-next-line camelcase
pull_number: issueNum,
reviewers: currentAssignees
});
// TODO: assert return value
}

const changeAssign = await aOctoKit.pulls.requestReviewers({
owner,
repo,
// eslint-disable-next-line camelcase
pull_number: issueNum,
reviewers: nextAssignees
});

return changeAssign;
}

/**
* @param {import('@octokit/rest').Octokit} aOctoKit
* @param {string} repoName
Expand Down Expand Up @@ -88,7 +127,23 @@ export async function assignReviewer(
nextAssignees
);

await Promise.all([changeLabels, changeAssign]);
const issueIsPullRequest = await isPullRequest(
aOctoKit,
owner,
repo,
issueNum
);

const changeReviewer = issueIsPullRequest ? replaceReviewRequests(
aOctoKit,
owner,
repo,
issueNum,
currentAssignees,
nextAssignees,
) : null;

await Promise.all([changeLabels, changeAssign, changeReviewer]);
}

/**
Expand Down Expand Up @@ -184,4 +239,4 @@ export async function rejectPullRequest(
);

await Promise.all([changeLabels, changeAssign]);
}
}
21 changes: 21 additions & 0 deletions src/pulls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {import('@octokit/rest').Octokit} aOctoKit
* @param {string} owner
* @param {string} repo
* @param {number} issueOrPullNumber
* @return {Promise<boolean>}
*/
export async function isPullRequest(aOctoKit, owner, repo, issueOrPullNumber) {
try {
await aOctoKit.pulls.get({
owner,
repo,
// eslint-disable-next-line camelcase
pull_number: issueOrPullNumber,
});

return true;
} catch (_e) {
return false;
}
}

0 comments on commit 86e6513

Please sign in to comment.