diff --git a/README.md b/README.md index f0a4b31..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: @@ -69,6 +67,26 @@ jobs: pull-request-number: ${{ github.event.inputs.pullRequestNumber }} ``` +Optionally, You can provide a message for the review: + +```yaml +name: Auto approve + +on: pull_request + +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. 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 c797b88..8235393 100644 --- a/dist/index.js +++ b/dist/index.js @@ -10070,7 +10070,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) { @@ -10113,6 +10113,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}`); @@ -10224,7 +10225,8 @@ function run() { return __awaiter(this, void 0, void 0, function* () { try { const token = core.getInput("github-token", { required: true }); - yield (0, approve_1.approve)(token, github.context, prNumber()); + const reviewMessage = core.getInput("review-message"); + yield (0, approve_1.approve)(token, github.context, prNumber(), reviewMessage || undefined); } catch (error) { if (error instanceof Error) { 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" }); 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.test.ts b/src/main.test.ts index 021ec28..e3f05ba 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -35,16 +35,38 @@ afterEach(() => { process.env = originalEnv; }); +test("passes the review message to approve", async () => { + mockedGithub.context = ghContext(); + process.env["INPUT_REVIEW-MESSAGE"] = "LGTM"; + await run(); + expect(mockedApprove).toHaveBeenCalledWith( + "tok-xyz", + expect.anything(), + 101, + "LGTM" + ); +}); + test("calls approve when no PR number is provided", async () => { mockedGithub.context = ghContext(); await run(); - expect(mockedApprove).toHaveBeenCalledWith("tok-xyz", expect.anything(), 101); + expect(mockedApprove).toHaveBeenCalledWith( + "tok-xyz", + expect.anything(), + 101, + undefined + ); }); test("calls approve when a valid PR number is provided", async () => { process.env["INPUT_PULL-REQUEST-NUMBER"] = "456"; await run(); - expect(mockedApprove).toHaveBeenCalledWith("tok-xyz", expect.anything(), 456); + expect(mockedApprove).toHaveBeenCalledWith( + "tok-xyz", + expect.anything(), + 456, + undefined + ); }); test("errors when an invalid PR number is provided", async () => { diff --git a/src/main.ts b/src/main.ts index 8a685f3..297ed66 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,13 @@ import { approve } from "./approve"; export async function run() { try { const token = core.getInput("github-token", { required: true }); - await approve(token, github.context, prNumber()); + const reviewMessage = core.getInput("review-message"); + await approve( + token, + github.context, + prNumber(), + reviewMessage || undefined + ); } catch (error) { if (error instanceof Error) { core.setFailed(error.message);