diff --git a/src/operations.js b/src/operations.js index e20f759..7437efc 100644 --- a/src/operations.js +++ b/src/operations.js @@ -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. @@ -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} currentAssignees + * @param {Array} 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 @@ -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]); } /** @@ -184,4 +239,4 @@ export async function rejectPullRequest( ); await Promise.all([changeLabels, changeAssign]); -} \ No newline at end of file +} diff --git a/src/pulls.js b/src/pulls.js new file mode 100644 index 0000000..6944e00 --- /dev/null +++ b/src/pulls.js @@ -0,0 +1,21 @@ +/** + * @param {import('@octokit/rest').Octokit} aOctoKit + * @param {string} owner + * @param {string} repo + * @param {number} issueOrPullNumber + * @return {Promise} + */ +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; + } +}