Skip to content

Commit

Permalink
Feature/add optional review message (#200)
Browse files Browse the repository at this point in the history
Custom review messages

Co-authored-by: Harry Marr <harry.marr@gmail.com>
  • Loading branch information
ghaith96 and hmarr committed Aug 20, 2022
1 parent a9bede8 commit 289663b
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 16 deletions.
38 changes: 28 additions & 10 deletions README.md
Expand Up @@ -20,30 +20,28 @@ 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:
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 }}
- 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:
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Expand Up @@ -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'
6 changes: 4 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/approve.test.ts
Expand Up @@ -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" });

Expand Down
4 changes: 3 additions & 1 deletion src/approve.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`);
Expand Down
26 changes: 24 additions & 2 deletions src/main.test.ts
Expand Up @@ -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 () => {
Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 289663b

Please sign in to comment.