diff --git a/README.md b/README.md index 4666c36..d98f466 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/action.yml b/action.yml index 5821a09..92117e0 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/dist/index.js b/dist/index.js index 30eab5d..01ae5f8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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) { @@ -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(); diff --git a/src/approve.test.ts b/src/approve.test.ts index c34e5c0..e20e115 100644 --- a/src/approve.test.ts +++ b/src/approve.test.ts @@ -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()); diff --git a/src/approve.ts b/src/approve.ts index be91989..bf94340 100644 --- a/src/approve.ts +++ b/src/approve.ts @@ -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) { diff --git a/src/main.ts b/src/main.ts index f66c20b..fec0ac5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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();