From 7bb0c2e088b489b6018ca2f8be11e86b745f02d9 Mon Sep 17 00:00:00 2001 From: ghaith jardaneh <17298910+ghaith96@users.noreply.github.com> Date: Thu, 7 Jul 2022 21:33:39 +0000 Subject: [PATCH 1/5] add optional review message input --- action.yml | 3 +++ dist/index.js | 14 ++++++-------- src/approve.ts | 4 +++- src/main.ts | 11 +++++------ 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/action.yml b/action.yml index 92117e0..c3d48f3 100644 --- a/action.yml +++ b/action.yml @@ -10,6 +10,9 @@ inputs: pull-request-number: description: '(optional) The ID of a pull request to auto-approve. By default, this action tries to use the pull_request event payload.' required: false + review-message: + description: '(optional) The message of the pull request review.' + required: false runs: using: 'node12' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index f5c3ac7..4fece2e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8341,7 +8341,7 @@ exports.approve = void 0; const core = __importStar(__nccwpck_require__(2186)); const github = __importStar(__nccwpck_require__(5438)); const request_error_1 = __nccwpck_require__(537); -function approve(token, context, prNumber) { +function approve(token, context, prNumber, reviewMessage) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { if (!prNumber) { @@ -8384,6 +8384,7 @@ function approve(token, context, prNumber) { owner: context.repo.owner, repo: context.repo.repo, pull_number: prNumber, + body: reviewMessage, event: "APPROVE", }); core.info(`Approved pull request #${prNumber}`); @@ -8491,16 +8492,13 @@ const core = __importStar(__nccwpck_require__(2186)); const github = __importStar(__nccwpck_require__(5438)); const approve_1 = __nccwpck_require__(6609); function run() { + var _a, _b; return __awaiter(this, void 0, void 0, function* () { try { const token = core.getInput("github-token", { required: true }); - const prNumber = parseInt(core.getInput("pull-request-number"), 10); - if (!Number.isNaN(prNumber)) { - yield (0, approve_1.approve)(token, github.context, prNumber); - } - else { - yield (0, approve_1.approve)(token, github.context); - } + const reviewMessage = core.getInput("review-message"); + const prNumber = (_a = parseInt(core.getInput("pull-request-number"), 10)) !== null && _a !== void 0 ? _a : (_b = github.context.payload.pull_request) === null || _b === void 0 ? void 0 : _b.number; + yield (0, approve_1.approve)(token, github.context, prNumber, reviewMessage); } catch (error) { if (error instanceof Error) { diff --git a/src/approve.ts b/src/approve.ts index 9903ee9..8e4f20d 100644 --- a/src/approve.ts +++ b/src/approve.ts @@ -7,7 +7,8 @@ import { GitHub } from "@actions/github/lib/utils"; export async function approve( token: string, context: Context, - prNumber?: number + prNumber?: number, + reviewMessage?: string ) { if (!prNumber) { prNumber = context.payload.pull_request?.number; @@ -67,6 +68,7 @@ export async function approve( owner: context.repo.owner, repo: context.repo.repo, pull_number: prNumber, + body: reviewMessage, event: "APPROVE", }); core.info(`Approved pull request #${prNumber}`); diff --git a/src/main.ts b/src/main.ts index fca4a3f..d69417a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,12 +5,11 @@ import { approve } from "./approve"; async function run() { try { const token = core.getInput("github-token", { required: true }); - const prNumber: number = parseInt(core.getInput("pull-request-number"), 10); - if (!Number.isNaN(prNumber)) { - await approve(token, github.context, prNumber); - } else { - await approve(token, github.context); - } + const reviewMessage = core.getInput("review-message"); + const prNumber: number = + parseInt(core.getInput("pull-request-number"), 10) ?? + github.context.payload.pull_request?.number; + await approve(token, github.context, prNumber, reviewMessage); } catch (error) { if (error instanceof Error) { core.setFailed(error.message); From 84733b77819eddb0ac1b7608f4a6565fb3cf0d57 Mon Sep 17 00:00:00 2001 From: ghaith jardaneh <17298910+ghaith96@users.noreply.github.com> Date: Thu, 7 Jul 2022 21:33:49 +0000 Subject: [PATCH 2/5] add unit test --- src/approve.test.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/approve.test.ts b/src/approve.test.ts index 41890be..1f5a2c7 100644 --- a/src/approve.test.ts +++ b/src/approve.test.ts @@ -64,6 +64,28 @@ test("a review is successfully created with an Actions token", async () => { ); }); +test("when a review is successfully created with message", async () => { + nock("https://api.github.com").get("/user").reply(403, {}); + + nock("https://api.github.com") + .get("/repos/hmarr/test/pulls/101") + .reply(200, { head: { sha: "24c5451bbf1fb09caa3ac8024df4788aff4d4974" } }); + + nock("https://api.github.com") + .get("/repos/hmarr/test/pulls/101/reviews") + .reply(200, []); + + nock("https://api.github.com") + .post("/repos/hmarr/test/pulls/101/reviews") + .reply(200, { id: 1 }); + + await approve("gh-tok", ghContext(), undefined, "Review body"); + + expect(core.info).toHaveBeenCalledWith( + expect.stringContaining("Approved pull request #101") + ); +}); + test("when a review is successfully created using pull-request-number", async () => { nock("https://api.github.com").get("/user").reply(200, { login: "hmarr" }); From 05927086362b3e5869ab9564ef637e1bf8428ca1 Mon Sep 17 00:00:00 2001 From: ghaith jardaneh <17298910+ghaith96@users.noreply.github.com> Date: Thu, 7 Jul 2022 21:33:59 +0000 Subject: [PATCH 3/5] update docs --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index f0a4b31..1f792a6 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,30 @@ jobs: pull-request-number: ${{ github.event.inputs.pullRequestNumber }} ``` +Optionally, You can provide a message for the review: + +```yaml +name: Auto approve + +on: + workflow_dispatch: + inputs: pullRequestNumber + description: Pull request number to auto-approve + required: false + +jobs: + auto-approve: + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - uses: hmarr/auto-approve-action@v2 + if: github.actor == 'dependabot[bot]' + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + review-message: "Auto approved automated PR" +``` + ## Why? GitHub lets you prevent merges of unapproved pull requests. However, it's occasionally useful to selectively circumvent this restriction - for instance, some people want Dependabot's automated pull requests to not require approval. From b1adba46b9c0ec8f85a2c913f569d79ef61f20d1 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Sat, 20 Aug 2022 14:17:29 +0100 Subject: [PATCH 4/5] Prettier --- src/main.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 1a034a7..297ed66 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,7 +6,12 @@ export async function run() { try { const token = core.getInput("github-token", { required: true }); const reviewMessage = core.getInput("review-message"); - await approve(token, github.context, prNumber(), reviewMessage || undefined); + await approve( + token, + github.context, + prNumber(), + reviewMessage || undefined + ); } catch (error) { if (error instanceof Error) { core.setFailed(error.message); @@ -36,4 +41,4 @@ function prNumber(): number { if (require.main === module) { run(); -} \ No newline at end of file +} From 5d797a8f2854a50d8d2a70dc4d8726f19bb5d9e6 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Sat, 20 Aug 2022 14:34:24 +0100 Subject: [PATCH 5/5] Remove unnecessary input in README example --- README.md | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 1f792a6..766f305 100644 --- a/README.md +++ b/README.md @@ -20,19 +20,17 @@ jobs: permissions: pull-requests: write steps: - - uses: hmarr/auto-approve-action@v2 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} + - uses: hmarr/auto-approve-action@v2 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} ``` - Combine with an `if` clause to only auto-approve certain users. For example, to auto-approve [Dependabot][dependabot] pull requests, use: ```yaml name: Auto approve -on: - pull_request +on: pull_request jobs: auto-approve: @@ -40,10 +38,10 @@ jobs: permissions: pull-requests: write steps: - - uses: hmarr/auto-approve-action@v2 - if: github.actor == 'dependabot[bot]' - with: - github-token: ${{ secrets.GITHUB_TOKEN }} + - uses: hmarr/auto-approve-action@v2 + if: github.actor == 'dependabot[bot]' + with: + github-token: ${{ secrets.GITHUB_TOKEN }} ``` If you want to use this action from a workflow file that doesn't run on the `pull_request` or `pull_request_target` events, use the `pull-request-number` input: @@ -74,11 +72,7 @@ Optionally, You can provide a message for the review: ```yaml name: Auto approve -on: - workflow_dispatch: - inputs: pullRequestNumber - description: Pull request number to auto-approve - required: false +on: pull_request jobs: auto-approve: @@ -86,11 +80,11 @@ jobs: permissions: pull-requests: write steps: - - uses: hmarr/auto-approve-action@v2 - if: github.actor == 'dependabot[bot]' - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - review-message: "Auto approved automated PR" + - uses: hmarr/auto-approve-action@v2 + if: github.actor == 'dependabot[bot]' + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + review-message: "Auto approved automated PR" ``` ## Why?