Skip to content

Commit 99ae923

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

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

.github/workflows/context.yaml

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

0 commit comments

Comments
 (0)