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 6 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
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -42,6 +42,24 @@ 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

jobs:
auto-approve:
runs-on: ubuntu-latest
steps:
- uses: hmarr/auto-approve-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
pull-request-number: 1234
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the hard-coded example number, perhaps this snippet could use workflow_dispatch inputs so we could change this to a dynamic value.

Alternatively, if you're looking to run this workflow after another Actions workflow that generates a pull request, workflow_run might be the trigger you're looking for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Alternatively, if you're looking to run this workflow after another Actions workflow that generates a pull request, workflow_run might be the trigger you're looking for?

Thanks for the suggestion! Actually, I currently have my automerge workflow happening in response to the pull_request event, and it is successfully triggered after the workflow that generates a pull request. In case you're wondering why I submitted this PR, I am effectively trying to make a "subroutine" that:

  1. Generates a new version
  2. Creates a PR to update the main branch with that version
  3. Merges the PR so that the main branch is updated.

And I want that to happen all in one workflow, so that I can use https://github.com/softprops/turnstyle to prevent race conditions and merge conflicts. I'll use that subroutine in concert with https://github.com/convictional/trigger-workflow-and-wait to synchronously create a new version from any workflow that needs to do that. I'm hopeful that this will DRY up my workflows and simplify the handling of race conditions 😁

```

## 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'
22 changes: 13 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, pr_number) {
var _a, _b;
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 (!pr_number) {
pr_number = ((_b = (_a = context === null || context === void 0 ? void 0 : context.payload) === null || _a === void 0 ? void 0 : _a.pull_request) === null || _b === void 0 ? void 0 : _b.number) || 0;
}
if (!pr_number) {
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 #${pr_number}`);
try {
yield client.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
pull_number: pr_number,
event: "APPROVE",
});
core.info(`Approved pull request #${pr.number}`);
core.info(`Approved pull request #${pr_number}`);
}
catch (error) {
if (error instanceof request_error_1.RequestError) {
Expand Down Expand Up @@ -5963,7 +5966,8 @@ 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 pr_number = JSON.parse(core.getInput("pull-request-number") || "0");
yield approve_1.approve(token, github.context, pr_number);
});
}
run();
Expand Down