Skip to content

Commit

Permalink
feat: rerun-pr-checks (#159)
Browse files Browse the repository at this point in the history
* feat: rerun-pr-checks

* docs: rerun-pr-checks

* chore: tests

* chore: workflow name

* chore: updates based on pr comments

* chore: remove label after test

* chore: run test on PR instead of PR_target

* chore: use ref from api query instead of context

* fix: typo in test

* chore: grab head ref instead

* use bluebird map

* use octokit request library

* authenticate

* console.log error

* catch error for workflows already running

* stop

* rerun pr target workflows too and fix test

* fix workflow

* chore: comitting generated dist

Co-authored-by: dadajian <[email protected]>
Co-authored-by: danadajian <[email protected]>
  • Loading branch information
3 people authored Mar 25, 2022
1 parent 13a6b94 commit 8ed8eb5
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 11 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/rerun-pr-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Rerun PR Checks

on:
pull_request:
types: [ labeled ]

jobs:
test:
if: ${{ github.event.label.name == 'RE-RUN PR CHECKS' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}

- uses: ./
with:
helper: rerun-pr-checks
github_token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}

- uses: ./
with:
helper: remove-label
label: RE-RUN PR CHECKS
github_token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ Additionally, the following parameters can be used for additional control over t
### [remove-pr-from-merge-queue](.github/workflows/remove-pr-from-merge-queue.yml)
* Removes a PR from the merge queue if it has a stale failing status check. A PR check is considered stale if it is older than the provided number of `seconds`.

### [rerun-pr-checks](.github/workflows/rerun-pr-checks.yml)
* Reruns all of the latest workflow checks on a pull request (helpful if they were cancelled for some reason, either manually or due to rate limiting, for example).

### [set-commit-status](.github/workflows/set-commit-status.yml)
* Sets a [commit status](https://github.blog/2012-09-04-commit-status-api/)

Expand Down
114 changes: 114 additions & 0 deletions dist/766.index.js

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

1 change: 1 addition & 0 deletions dist/766.index.js.map

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

2 changes: 1 addition & 1 deletion dist/832.index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2601,7 +2601,7 @@ var isPlainObject = __webpack_require__(3287);
var nodeFetch = _interopDefault(__webpack_require__(467));
var requestError = __webpack_require__(537);

const VERSION = "5.6.2";
const VERSION = "5.6.3";

function getBufferResponse(response) {
return response.arrayBuffer();
Expand Down
2 changes: 1 addition & 1 deletion dist/832.index.js.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18986,6 +18986,18 @@ var map = {
832,
974
],
"./rerun-pr-checks": [
6766,
832,
710,
766
],
"./rerun-pr-checks.ts": [
6766,
832,
710,
766
],
"./set-commit-status": [
2209,
832,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@actions/github": "5.0.0",
"@adobe/node-fetch-retry": "2.2.0",
"@octokit/rest": "18.12.0",
"@octokit/request": "5.6.3",
"axios": "0.26.1",
"bluebird": "3.7.2",
"codeowners-utils": "1.0.2",
Expand Down
66 changes: 66 additions & 0 deletions src/helpers/rerun-pr-checks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import * as core from '@actions/core';
import { context } from '@actions/github';
import { map } from 'bluebird';
import { octokit } from '../octokit';
import { request } from '@octokit/request';

export const rerunPrChecks = async () => {
/** grab owner in case of fork branch */
const {
data: {
head: {
user: { login: owner },
sha: latestHash,
ref: branch
}
}
} = await octokit.pulls.get({
pull_number: context.issue.number,
...context.repo
});
const workflowRunResponses = await map(['pull_request', 'pull_request_target'], event =>
octokit.actions.listWorkflowRunsForRepo({
branch,
...context.repo,
owner,
event,
per_page: 100
})
);
const workflowRuns = workflowRunResponses.map(response => response.data.workflow_runs).flat();
if (!workflowRuns.length) {
core.info(`No workflow runs found on branch ${branch} on ${owner}/${context.repo.repo}`);
return;
}
const latestWorkflowRuns = workflowRuns.filter(({ head_sha }) => head_sha === latestHash);
core.info(`The latest runs on this branch are ${latestWorkflowRuns.map(run => run.name)}, triggering reruns...`);

return map(latestWorkflowRuns, async ({ id, name }) => {
core.info(`- Rerunning ${name}`);
await request('POST /repos/{owner}/{repo}/actions/runs/{run_id}/rerun', {
owner,
repo: context.repo.repo,
run_id: id,
headers: {
authorization: `token ${core.getInput('github_token')}`
}
}).catch(error => {
if (error.status === 403) {
core.info(`${name} is already running.`);
}
});
});
};
Loading

0 comments on commit 8ed8eb5

Please sign in to comment.