Skip to content

Commit ec22a30

Browse files
committed
Use pull request and workflow list to trigger rerun.
1 parent 065bd26 commit ec22a30

3 files changed

Lines changed: 32 additions & 27 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"@actions/core": "^1.6.0",
3131
"@actions/github": "^5.1.1",
3232
"axios": "^0.27.2",
33-
"json-stringify-safe": "^5.0.1"
33+
"json-stringify-safe": "^5.0.1",
34+
"luxon": "^3.4.2"
3435
},
3536
"engines": {
3637
"node": "^16.0"

src/index.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,50 @@
11
const core = require('@actions/core');
22
const github = require('@actions/github');
3-
const stringify = require('json-stringify-safe');
3+
const { DateTime } = require('luxon');
44

5-
async function run() {
5+
async function runAction() {
66
// Attempt to load credentials from the GitHub OIDC provider.
77
const githubSecretAccessToken = core.getInput('github_token');
88
if (!githubSecretAccessToken || githubSecretAccessToken === '{{ secrets.GITHUB_TOKEN }}') {
9+
// GitHub core library has a critical failure if we don't escape the $, seems like a malicious attack vector they aren't handling correctly
10+
// eslint-disable-next-line no-useless-escape
911
core.setFailed("Missing use with configuration in the github action, please add to the github workflow: 'github_token: ${{ secrets.GITHUB_TOKEN }}'");
1012
core.getInput('github_token', { required: true });
1113
throw Error('InvalidInput');
1214
}
1315

1416
// https://docs.github.com/en/actions/learn-github-actions/contexts#example-contents-of-the-github-contex
15-
const { repository, repository_owner: owner, ref_name: currentRef, ref_type: triggerType } = github.context;
16-
if (!triggerType) {
17-
core.info(stringify(github));
18-
core.setFailed('No trigger type set');
19-
throw Error('InvalidInput');
20-
}
21-
if (triggerType !== 'branch') {
22-
core.info(`Skipping check because trigger type is not branch. Trigger Type: ${triggerType}, Ref: ${currentRef}`);
23-
return;
24-
}
17+
const currentRef = github.context.payload.ref.replace(/^refs\/heads\//, '');
18+
const workflowTarget = github.context.workflow;
19+
core.info(`Branch Ref: ${currentRef}, Workflow: ${workflowTarget}`);
20+
const [owner, repo] = github.context.payload.repository.full_name.split('/');
2521

2622
const octokit = github.getOctokit(githubSecretAccessToken);
27-
const branches = await octokit.rest.repos.listBranches({
28-
owner: owner,
29-
repo: repository
23+
24+
const openPullRequestsResponse = await octokit.rest.pulls.list({ owner, repo, state: 'open', base: currentRef, per_page: 100 });
25+
core.info(`Relevant open pull requests: [${openPullRequestsResponse.data.map(pr => pr.number).join(', ')}]`);
26+
27+
const prMap = openPullRequestsResponse.data.reduce((acc, pr) => { acc[pr.number] = true; return acc; }, {});
28+
29+
const workflowRuns = await octokit.rest.actions.listWorkflowRuns({
30+
owner, repo, workflow_id: workflowTarget, event: 'pull_request', created: `>= ${DateTime.utc().minus({ month: 1 }).toISODate()}`, per_page: 100
3031
});
31-
// https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event
32-
const filteredBranches = branches.data.filter(branch => branch.name !== currentRef);
33-
await Promise.all(filteredBranches.map(async branch => {
32+
33+
const workflowRunsForAffectedPrs = workflowRuns.data.workflow_runs.filter(run => run.pull_requests?.some(pr => prMap[pr.number]));
34+
await Promise.all(workflowRunsForAffectedPrs.map(async run => {
3435
try {
35-
await octokit.rest.repos.createDispatchEvent({
36-
owner: owner,
37-
repo: repository,
38-
workflow_id: github.workflow,
39-
ref: branch
36+
await core.info(`Attempting to rerun: ${run.id}`);
37+
await octokit.rest.actions.reRunWorkflow({
38+
owner, repo, run_id: run.id
4039
});
4140
} catch (error) {
42-
core.error(`Failed to automatically trigger branch ${branch}: ${error.code} - ${error.message}`);
41+
core.error(`Failed to automatically retrigger pull request: ${run.pull_requests?.map(pr => pr.number).join(',')}: ${error.status} - ${error.message}`);
4342
}
4443
}));
4544
}
4645

47-
exports.run = run;
46+
exports.run = runAction;
4847

4948
if (require.main === module) {
50-
run();
49+
runAction();
5150
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,6 +2424,11 @@ lru-cache@^6.0.0:
24242424
dependencies:
24252425
yallist "^4.0.0"
24262426

2427+
luxon@^3.4.2:
2428+
version "3.4.2"
2429+
resolved "https://registry.yarnpkg.com/luxon/-/luxon-3.4.2.tgz#f5bcab779f3d6a943ee7c8621c2b416bc10abd24"
2430+
integrity sha512-uBoAVCVcajsrqy3pv7eo5jEUz1oeLmCcnMv8n4AJpT5hbpN9lUssAXibNElpbLce3Mhm9dyBzwYLs9zctM/0tA==
2431+
24272432
make-dir@^3.0.0:
24282433
version "3.1.0"
24292434
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"

0 commit comments

Comments
 (0)