Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto approve any pull request with the pull-request-number input #186

Merged
merged 16 commits into from Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -42,6 +42,27 @@ jobs:
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:

```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
steps:
- uses: hmarr/auto-approve-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
pull-request-number: ${{ github.event.inputs.pullRequestNumber }}
```

## 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 @@ -7,6 +7,9 @@ inputs:
github-token:
description: 'The GITHUB_TOKEN secret'
required: true
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
runs:
using: 'node12'
main: 'dist/index.js'
27 changes: 18 additions & 9 deletions dist/index.js
Expand Up @@ -5866,24 +5866,27 @@ exports.approve = void 0;
const core = __importStar(__nccwpck_require__(186));
const github = __importStar(__nccwpck_require__(438));
const request_error_1 = __nccwpck_require__(537);
function approve(token, context) {
function approve(token, context, prNumber) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const { pull_request: pr } = context.payload;
if (!pr) {
core.setFailed("Event payload missing `pull_request` key. Make sure you're " +
"triggering this action on the `pull_request` or `pull_request_target` events.");
if (!prNumber) {
prNumber = (_a = context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number;
}
if (!prNumber) {
core.setFailed("Event payload missing `pull_request` key, and no `pull-request-number` provided as input." +
"Make sure you're triggering this action on the `pull_request` or `pull_request_target` events.");
return;
}
const client = github.getOctokit(token);
core.info(`Creating approving review for pull request #${pr.number}`);
core.info(`Creating approving review for pull request #${prNumber}`);
try {
yield client.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
pull_number: prNumber,
event: "APPROVE",
});
core.info(`Approved pull request #${pr.number}`);
core.info(`Approved pull request #${prNumber}`);
}
catch (error) {
if (error instanceof request_error_1.RequestError) {
Expand Down Expand Up @@ -5963,7 +5966,13 @@ const approve_1 = __nccwpck_require__(609);
function run() {
return __awaiter(this, void 0, void 0, function* () {
const token = core.getInput("github-token", { required: true });
yield approve_1.approve(token, github.context);
const prNumber = parseInt(core.getInput("pull-request-number"), 10);
if (!Number.isNaN(prNumber)) {
yield approve_1.approve(token, github.context, prNumber);
}
else {
yield approve_1.approve(token, github.context);
}
});
}
run();
Expand Down
12 changes: 12 additions & 0 deletions src/approve.test.ts
Expand Up @@ -23,6 +23,18 @@ test("when a review is successfully created", async () => {
);
});

test("when a review is successfully created using pull-request-number", async () => {
nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(200, { id: 1 });

await approve("gh-tok", new Context(), 101);

expect(core.info).toHaveBeenCalledWith(
expect.stringContaining("Approved pull request #101")
);
});

test("without a pull request", async () => {
await approve("gh-tok", new Context());

Expand Down
23 changes: 15 additions & 8 deletions src/approve.ts
Expand Up @@ -3,27 +3,34 @@ import * as github from "@actions/github";
import { RequestError } from "@octokit/request-error";
import { Context } from "@actions/github/lib/context";

export async function approve(token: string, context: Context) {
const { pull_request: pr } = context.payload;
if (!pr) {
export async function approve(
token: string,
context: Context,
prNumber?: number
) {
if (!prNumber) {
prNumber = context.payload.pull_request?.number;
}

if (!prNumber) {
core.setFailed(
"Event payload missing `pull_request` key. Make sure you're " +
"triggering this action on the `pull_request` or `pull_request_target` events."
"Event payload missing `pull_request` key, and no `pull-request-number` provided as input." +
"Make sure you're triggering this action on the `pull_request` or `pull_request_target` events."
);
return;
}

const client = github.getOctokit(token);

core.info(`Creating approving review for pull request #${pr.number}`);
core.info(`Creating approving review for pull request #${prNumber}`);
try {
await client.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
pull_number: prNumber,
event: "APPROVE",
});
core.info(`Approved pull request #${pr.number}`);
core.info(`Approved pull request #${prNumber}`);
} catch (error) {
if (error instanceof RequestError) {
switch (error.status) {
Expand Down
7 changes: 6 additions & 1 deletion src/main.ts
Expand Up @@ -4,7 +4,12 @@ import { approve } from "./approve";

async function run() {
const token = core.getInput("github-token", { required: true });
await approve(token, github.context);
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);
}
}

run();