Skip to content

Commit b7d2d7c

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

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

.github/workflows/context.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,43 @@ 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_number = context.payload.pull_request.number;
28+
const workflow_name = context.workflow;
29+
const event_match = "pull_request";
30+
console.log(pr_number);
31+
console.log(workflow_name);
32+
33+
if (!pr_number) {
34+
console.log("No pull request number found in context.");
35+
core.setOutput("count", 0);
36+
return;
37+
}
38+
39+
const runs = await github.rest.actions.listWorkflowRunsForRepo({
40+
owner: context.repo.owner,
41+
repo: context.repo.repo,
42+
event: event_match,
43+
per_page: 100
44+
});
45+
# console.log(runs);
46+
47+
const filtered = runs.data.workflow_runs.filter(run =>
48+
run.name === workflow_name &&
49+
run.event === event_match &&
50+
run.pull_requests?.some(p => p.number === pr_number)
51+
);
52+
# console.log(filtered);
53+
console.log(filtered.length);
54+
55+
core.setOutput("count", filtered.length);
56+
- name: Dump job context
57+
run: echo "Test run count = ${{ steps.count.outputs.count }}"
2158
- name: Dump job context
2259
env:
2360
JOB_CONTEXT: ${{ toJson(job) }}

0 commit comments

Comments
 (0)