Skip to content

Commit 67f7da1

Browse files
Count runs for this PR
... with fail safe
1 parent 7d42b9e commit 67f7da1

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

.github/workflows/context.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,50 @@ jobs:
1818
run: |
1919
echo "$GITHUB_CONTEXT"
2020
echo "$GITHUB_CONTEXT" | jq -c '.event.pull_request.commits'
21+
- name: Count test runs for this PR
22+
if: ${{ !cancelled() }}
23+
id: count
24+
uses: actions/github-script@v9
25+
with:
26+
script: |
27+
const pr = context.payload.pull_request;
28+
29+
if (!pr) {
30+
core.setFailed("This workflow must be triggered by a pull request.");
31+
return;
32+
}
33+
34+
const pr_number = pr.number;
35+
const workflow_name = context.workflow;
36+
const event_match = "pull_request";
37+
console.log(pr_number);
38+
console.log(workflow_name);
39+
40+
if (!pr_number) {
41+
console.log("No pull request number found in context.");
42+
core.setOutput("count", 0);
43+
return;
44+
}
45+
46+
const runs = await github.rest.actions.listWorkflowRunsForRepo({
47+
owner: context.repo.owner,
48+
repo: context.repo.repo,
49+
event: event_match,
50+
per_page: 100
51+
});
52+
// console.log(runs);
53+
54+
const filtered = runs.data.workflow_runs.filter(run =>
55+
run.name === workflow_name &&
56+
run.event === event_match &&
57+
run.pull_requests?.some(p => p.number === pr_number)
58+
);
59+
// console.log(filtered);
60+
console.log(filtered.length);
61+
62+
core.setOutput("count", filtered.length);
63+
- name: Dump job context
64+
run: echo "Test run count = ${{ steps.count.outputs.count }}"
2165
- name: Dump job context
2266
env:
2367
JOB_CONTEXT: ${{ toJson(job) }}

0 commit comments

Comments
 (0)