From 15329652bad795622ab6665094c8dab053ae5014 Mon Sep 17 00:00:00 2001 From: Jack Baldry Date: Fri, 20 Sep 2024 15:43:06 +0100 Subject: [PATCH 1/2] Add configuration to opt out of removal of default reviewers It requires extra permissions that can't be satisfied by the default `GITHUB_TOKEN`. By making it configurable, teams can choose to use the default token and sacrifice this feature, or use a GitHub App if they want it. Signed-off-by: Jack Baldry --- backport/action.yml | 6 +++++- backport/backport.js | 11 ++++++----- backport/backport.ts | 13 ++++++++++--- backport/index.js | 1 + backport/index.ts | 3 ++- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/backport/action.yml b/backport/action.yml index c70af66..698aecc 100644 --- a/backport/action.yml +++ b/backport/action.yml @@ -5,7 +5,7 @@ inputs: description: | GitHub token with read and write permissions for issues, comments, and labels. - Additionally, the token needs read permissions for organization members. + Additionally, the token needs read permissions for organization members if `removeDefaultReviewers` is set to `true`. default: ${{ github.token }} title: description: Title for the backport PR @@ -13,6 +13,10 @@ inputs: labelsToAdd: description: Comma separated list of labels to add to the backport PR. required: false + removeDefaultReviewers: + default: true + description: Whether to remove default reviewers from the backport PRs. + type: boolean metricsWriteAPIKey: description: Grfanaa Cloud metrics api key required: false diff --git a/backport/backport.js b/backport/backport.js index c205772..33547e3 100644 --- a/backport/backport.js +++ b/backport/backport.js @@ -4,7 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.backport = exports.getFinalLabels = exports.getFailedBackportCommentBody = exports.isBettererConflict = exports.LABEL_NO_CHANGELOG = exports.LABEL_ADD_TO_CHANGELOG = exports.BETTERER_RESULTS_PATH = void 0; +exports.backport = exports.getFailedBackportCommentBody = exports.isBettererConflict = exports.LABEL_NO_CHANGELOG = exports.LABEL_ADD_TO_CHANGELOG = exports.BETTERER_RESULTS_PATH = void 0; +exports.getFinalLabels = getFinalLabels; const core_1 = require("@actions/core"); const exec_1 = require("@actions/exec"); const github_1 = require("@actions/github"); @@ -53,7 +54,7 @@ const isBettererConflict = async (gitUnmergedPaths) => { return gitUnmergedPaths.length === 1 && gitUnmergedPaths[0] === exports.BETTERER_RESULTS_PATH; }; exports.isBettererConflict = isBettererConflict; -const backportOnce = async ({ base, body, commitToBackport, github, head, labelsToAdd, owner, repo, title, mergedBy, }) => { +const backportOnce = async ({ base, body, commitToBackport, github, head, labelsToAdd, removeDefaultReviewers, owner, repo, title, mergedBy, }) => { const git = async (...args) => { await (0, exec_1.exec)('git', args, { cwd: repo }); }; @@ -126,7 +127,7 @@ const backportOnce = async ({ base, body, commitToBackport, github, head, labels }); } // Remove default reviewers - if (createRsp.data.requested_reviewers) { + if (removeDefaultReviewers && createRsp.data.requested_reviewers) { const reviewers = createRsp.data.requested_reviewers.map((user) => user.login); await github.pulls.deleteReviewRequest({ pull_number: pullRequestNumber, @@ -206,7 +207,7 @@ const getFailedBackportCommentBody = ({ base, commitToBackport, errorMessage, he return lines.join('\n'); }; exports.getFailedBackportCommentBody = getFailedBackportCommentBody; -const backport = async ({ issue, labelsToAdd, payload: { action, label, pull_request: { labels, merge_commit_sha: mergeCommitSha, merged, number: pullRequestNumber, title: originalTitle, merged_by, }, repository: { name: repo, owner: { login: owner }, }, }, titleTemplate, token, github, sender, }) => { +const backport = async ({ issue, labelsToAdd, payload: { action, label, pull_request: { labels, merge_commit_sha: mergeCommitSha, merged, number: pullRequestNumber, title: originalTitle, merged_by, }, repository: { name: repo, owner: { login: owner }, }, }, titleTemplate, removeDefaultReviewers, token, github, sender, }) => { const payload = github_1.context.payload; console.log('payloadAction: ' + payload.action); if (payload.action !== 'closed') { @@ -322,6 +323,7 @@ const backport = async ({ issue, labelsToAdd, payload: { action, label, pull_req github: github, head, labelsToAdd: prLabels, + removeDefaultReviewers, owner, repo, title, @@ -387,5 +389,4 @@ function getFinalLabels(originalLabels, labelsToAdd) { } return result; } -exports.getFinalLabels = getFinalLabels; //# sourceMappingURL=backport.js.map \ No newline at end of file diff --git a/backport/backport.ts b/backport/backport.ts index 876f45e..2a0c0aa 100644 --- a/backport/backport.ts +++ b/backport/backport.ts @@ -83,6 +83,7 @@ const backportOnce = async ({ github, head, labelsToAdd, + removeDefaultReviewers, owner, repo, title, @@ -94,6 +95,7 @@ const backportOnce = async ({ github: InstanceType head: string labelsToAdd: string[] + removeDefaultReviewers: boolean owner: string repo: string title: string @@ -154,7 +156,9 @@ const backportOnce = async ({ if (/^v\d+\.\d+\.x$/.test(base)) { const milestoneName = base.substring(1) const allMilestones = await github.issues.listMilestonesForRepo({ owner, repo, state: 'open' }) - const milestone = allMilestones.data.find((milestone) => milestone.title === milestoneName) + const milestone = allMilestones.data.find( + (milestone: { title: string }) => milestone.title === milestoneName, + ) if (milestone) { await github.issues.update({ repo, @@ -177,8 +181,8 @@ const backportOnce = async ({ } // Remove default reviewers - if (createRsp.data.requested_reviewers) { - const reviewers = createRsp.data.requested_reviewers.map((user) => user.login) + if (removeDefaultReviewers && createRsp.data.requested_reviewers) { + const reviewers = createRsp.data.requested_reviewers.map((user: { login: string }) => user.login) await github.pulls.deleteReviewRequest({ pull_number: pullRequestNumber, repo, @@ -284,6 +288,7 @@ interface BackportArgs { labelsToAdd: string[] payload: EventPayloads.WebhookPayloadPullRequest titleTemplate: string + removeDefaultReviewers: boolean token: string github: GitHub sender: EventPayloads.PayloadSender @@ -309,6 +314,7 @@ const backport = async ({ }, }, titleTemplate, + removeDefaultReviewers, token, github, sender, @@ -442,6 +448,7 @@ const backport = async ({ github: github, head, labelsToAdd: prLabels, + removeDefaultReviewers, owner, repo, title, diff --git a/backport/index.js b/backport/index.js index 7ab9765..945f76f 100644 --- a/backport/index.js +++ b/backport/index.js @@ -24,6 +24,7 @@ class Backport extends Action_1.Action { labelsToAdd: (0, exports.getLabelsToAdd)((0, core_1.getInput)('labelsToAdd')), payload: github_1.context.payload, titleTemplate: (0, core_1.getInput)('title'), + removeDefaultReviewers: (0, core_1.getBooleanInput)('removeDefaultReviewers'), github: issue.octokit, token: this.getToken(), sender: github_1.context.payload.sender, diff --git a/backport/index.ts b/backport/index.ts index 15f0e6e..40702b7 100644 --- a/backport/index.ts +++ b/backport/index.ts @@ -1,4 +1,4 @@ -import { error as logError, getInput, setFailed } from '@actions/core' +import { error as logError, getBooleanInput, getInput, setFailed } from '@actions/core' import { context } from '@actions/github' import { EventPayloads } from '@octokit/webhooks' import { OctoKitIssue } from '../api/octokit' @@ -23,6 +23,7 @@ class Backport extends Action { labelsToAdd: getLabelsToAdd(getInput('labelsToAdd')), payload: context.payload as EventPayloads.WebhookPayloadPullRequest, titleTemplate: getInput('title'), + removeDefaultReviewers: getBooleanInput('removeDefaultReviewers'), github: issue.octokit, token: this.getToken(), sender: context.payload.sender as EventPayloads.PayloadSender, From 7d41831401737d46c14800459053eb2259d8b999 Mon Sep 17 00:00:00 2001 From: Jack Baldry Date: Mon, 23 Sep 2024 17:44:26 +0100 Subject: [PATCH 2/2] Manually apply CI patch I'm getting different output locally... Signed-off-by: Jack Baldry --- backport/backport.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backport/backport.js b/backport/backport.js index 33547e3..8e1375d 100644 --- a/backport/backport.js +++ b/backport/backport.js @@ -4,8 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.backport = exports.getFailedBackportCommentBody = exports.isBettererConflict = exports.LABEL_NO_CHANGELOG = exports.LABEL_ADD_TO_CHANGELOG = exports.BETTERER_RESULTS_PATH = void 0; -exports.getFinalLabels = getFinalLabels; +exports.backport = exports.getFinalLabels = exports.getFailedBackportCommentBody = exports.isBettererConflict = exports.LABEL_NO_CHANGELOG = exports.LABEL_ADD_TO_CHANGELOG = exports.BETTERER_RESULTS_PATH = void 0; const core_1 = require("@actions/core"); const exec_1 = require("@actions/exec"); const github_1 = require("@actions/github"); @@ -389,4 +388,5 @@ function getFinalLabels(originalLabels, labelsToAdd) { } return result; } +exports.getFinalLabels = getFinalLabels; //# sourceMappingURL=backport.js.map \ No newline at end of file