Skip to content

Commit 9e072cf

Browse files
committed
feat(bugs): fix test-on-approval pr event bugs
1 parent 4069100 commit 9e072cf

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed

.github/workflows/test-on-approval.yml

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ on:
2828
# Workflow changes
2929
- ".github/workflows/test-*.yml"
3030

31-
# Prevent resource conflicts - only one workflow instance per PR
31+
# Prevent resource conflicts - only one workflow instance per branch
3232
concurrency:
33-
group: infra-tests-${{ github.event.pull_request.number }}
33+
group: infra-tests-${{ github.ref_name }}
3434
cancel-in-progress: false # Don't cancel running tests
3535

3636
permissions:
@@ -52,6 +52,40 @@ jobs:
5252
uses: actions/github-script@v7
5353
with:
5454
script: |
55+
// Handle different event types
56+
let currentSha, prNumber;
57+
58+
if (context.eventName === 'pull_request_review') {
59+
currentSha = context.payload.pull_request.head.sha;
60+
prNumber = context.payload.pull_request.number;
61+
} else if (context.eventName === 'push') {
62+
currentSha = context.sha;
63+
// For push events, we need to find the PR
64+
const { data: prs } = await github.rest.pulls.list({
65+
owner: context.repo.owner,
66+
repo: context.repo.repo,
67+
head: `${context.repo.owner}:${context.ref.replace('refs/heads/', '')}`,
68+
state: 'open'
69+
});
70+
71+
if (prs.length === 0) {
72+
console.log('⚠️ No open PR found for this push event - skipping infrastructure tests');
73+
console.log('💡 Create a PR to enable approval-gated testing');
74+
core.setOutput('existing-count', 0);
75+
core.setOutput('pr-number', '');
76+
core.setOutput('current-sha', currentSha);
77+
core.setOutput('no-pr', 'true');
78+
return 0;
79+
}
80+
81+
prNumber = prs[0].number;
82+
console.log(`📋 Found PR #${prNumber} for push to ${context.ref}`);
83+
} else {
84+
console.log(`⚠️ Unexpected event type: ${context.eventName}`);
85+
core.setOutput('existing-count', 0);
86+
return 0;
87+
}
88+
5589
const { data: runs } = await github.rest.actions.listWorkflowRuns({
5690
owner: context.repo.owner,
5791
repo: context.repo.repo,
@@ -60,7 +94,6 @@ jobs:
6094
per_page: 100
6195
});
6296
63-
const currentSha = context.payload.pull_request.head.sha;
6497
const prRuns = runs.workflow_runs.filter(run =>
6598
run.head_sha === currentSha &&
6699
run.id !== context.runId // Exclude current run
@@ -70,20 +103,27 @@ jobs:
70103
console.log('Existing run details:', prRuns.map(r => `${r.id} (${r.status})`));
71104
core.setOutput('existing-count', prRuns.length);
72105
106+
// Store for later steps
107+
core.setOutput('pr-number', prNumber);
108+
core.setOutput('current-sha', currentSha);
109+
73110
return prRuns.length;
74111
75112
- name: Check if PR is approved (with debouncing)
76113
id: approval
114+
if: ${{ steps.existing-runs.outputs.no-pr != 'true' }}
77115
uses: actions/github-script@v7
78116
with:
79117
script: |
80118
// Wait a few seconds to catch rapid multiple approvals/pushes
81119
await new Promise(resolve => setTimeout(resolve, 5000));
82120
121+
const prNumber = ${{ steps.existing-runs.outputs.pr-number }};
122+
83123
const { data: reviews } = await github.rest.pulls.listReviews({
84124
owner: context.repo.owner,
85125
repo: context.repo.repo,
86-
pull_number: context.payload.pull_request.number,
126+
pull_number: prNumber,
87127
});
88128
89129
// Get the latest review from each reviewer
@@ -101,7 +141,7 @@ jobs:
101141
102142
const isApproved = approvedReviews.length > 0;
103143
104-
console.log(`PR #${context.payload.pull_request.number} approval status:`);
144+
console.log(`PR #${prNumber} approval status:`);
105145
console.log(`- Total unique reviewers: ${reviewers.size}`);
106146
console.log(`- Approved by: ${approvedReviews.map(r => r.user.login).join(', ')}`);
107147
console.log(`- Final approval status: ${isApproved}`);
@@ -111,13 +151,16 @@ jobs:
111151
112152
- name: Check for infrastructure changes
113153
id: changes
154+
if: ${{ steps.existing-runs.outputs.no-pr != 'true' }}
114155
uses: actions/github-script@v7
115156
with:
116157
script: |
158+
const prNumber = ${{ steps.existing-runs.outputs.pr-number }};
159+
117160
const { data: files } = await github.rest.pulls.listFiles({
118161
owner: context.repo.owner,
119162
repo: context.repo.repo,
120-
pull_number: context.payload.pull_request.number,
163+
pull_number: prNumber,
121164
});
122165
123166
const changedClouds = [];
@@ -187,6 +230,14 @@ jobs:
187230
uses: actions/github-script@v7
188231
with:
189232
script: |
233+
// Skip if no PR exists
234+
if ('${{ steps.existing-runs.outputs.no-pr }}' === 'true') {
235+
console.log('⏸️ Skipping: No PR exists for this branch');
236+
console.log('💡 Create a PR to enable infrastructure tests');
237+
core.setOutput('should-run', false);
238+
return;
239+
}
240+
190241
const existingRuns = ${{ steps.existing-runs.outputs.existing-count }};
191242
const isApproved = '${{ steps.approval.outputs.approved }}' === 'true';
192243
const hasChanges = '${{ steps.changes.outputs.changes }}' !== '';

0 commit comments

Comments
 (0)