Skip to content

Commit 80cb89e

Browse files
andykenwardCopilot
andcommitted
feat: enhance workflow_run handling to validate pull requests by branch and SHA
Co-authored-by: Copilot <copilot@github.com>
1 parent 1b030e1 commit 80cb89e

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

__tests__/common/github/comment.test.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,24 @@ describe(addComment, () => {
6969
eventName: 'workflow_run',
7070
payload: {
7171
workflow_run: {
72-
pull_requests: [{number: 2}]
72+
head_branch: 'master',
73+
head_sha: '3484a3fb816e0859fd6e1cea078d76385ff50625',
74+
pull_requests: [
75+
{
76+
number: 2,
77+
head: {
78+
ref: 'other-branch',
79+
sha: 'different-sha'
80+
}
81+
},
82+
{
83+
number: 3,
84+
head: {
85+
ref: 'master',
86+
sha: '3484a3fb816e0859fd6e1cea078d76385ff50625'
87+
}
88+
}
89+
]
7390
}
7491
}
7592
} as Readonly<WorkflowEventExtract<'workflow_run'>>)
@@ -96,7 +113,7 @@ describe(addComment, () => {
96113
variables: {
97114
owner: 'andykenward',
98115
repo: 'github-actions-cloudflare-pages',
99-
number: 2
116+
number: 3
100117
}
101118
},
102119
{
@@ -143,13 +160,49 @@ describe(addComment, () => {
143160
eventName: 'workflow_run',
144161
payload: {
145162
workflow_run: {
163+
head_branch: 'master',
164+
head_sha: '3484a3fb816e0859fd6e1cea078d76385ff50625',
146165
pull_requests: []
147166
}
148167
}
149168
} as unknown as Readonly<WorkflowEventExtract<'workflow_run'>>)
150169

151170
await expect(addComment(mockData, 'success')).rejects.toThrow(
152-
'No pull request number found in workflow_run event'
171+
'No pull request found in workflow_run event matching head branch and sha'
172+
)
173+
})
174+
175+
test('should throw when workflow_run has multiple matching pull requests', async () => {
176+
expect.assertions(1)
177+
178+
vi.spyOn(Context, 'useContextEvent').mockReturnValue({
179+
eventName: 'workflow_run',
180+
payload: {
181+
workflow_run: {
182+
head_branch: 'master',
183+
head_sha: '3484a3fb816e0859fd6e1cea078d76385ff50625',
184+
pull_requests: [
185+
{
186+
number: 2,
187+
head: {
188+
ref: 'master',
189+
sha: '3484a3fb816e0859fd6e1cea078d76385ff50625'
190+
}
191+
},
192+
{
193+
number: 3,
194+
head: {
195+
ref: 'master',
196+
sha: '3484a3fb816e0859fd6e1cea078d76385ff50625'
197+
}
198+
}
199+
]
200+
}
201+
}
202+
} as unknown as Readonly<WorkflowEventExtract<'workflow_run'>>)
203+
204+
await expect(addComment(mockData, 'success')).rejects.toThrow(
205+
'Multiple pull requests found in workflow_run event matching head branch and sha'
153206
)
154207
})
155208
})

src/common/github/comment.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,27 @@ const getNodeIdFromEvent = async () => {
6969
)
7070
}
7171
case 'workflow_run': {
72-
const pullRequestNumber = payload.workflow_run.pull_requests[0]?.number
72+
const pullRequestsMatchingHead =
73+
payload.workflow_run.pull_requests.filter(pullRequest => {
74+
return (
75+
pullRequest.head.ref === payload.workflow_run.head_branch &&
76+
pullRequest.head.sha === payload.workflow_run.head_sha
77+
)
78+
})
79+
80+
if (pullRequestsMatchingHead.length === 0) {
81+
raise(
82+
'No pull request found in workflow_run event matching head branch and sha'
83+
)
84+
}
85+
86+
if (pullRequestsMatchingHead.length > 1) {
87+
raise(
88+
'Multiple pull requests found in workflow_run event matching head branch and sha'
89+
)
90+
}
91+
92+
const pullRequestNumber = pullRequestsMatchingHead[0]?.number
7393

7494
if (!pullRequestNumber) {
7595
raise('No pull request number found in workflow_run event')

0 commit comments

Comments
 (0)