Skip to content

Commit e0a8f58

Browse files
andykenwardCopilot
andauthored
ci: number test (#737)
* feat: pr-number Co-authored-by: Copilot <copilot@github.com> * chore: build * chore: changeset * docs: Co-authored-by: Copilot <copilot@github.com> * ci: deploy skip on fork Co-authored-by: Copilot <copilot@github.com> --------- Co-authored-by: Copilot <copilot@github.com>
1 parent 4562240 commit e0a8f58

13 files changed

Lines changed: 212 additions & 73 deletions

File tree

.changeset/wide-pears-drive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"github-actions-cloudflare-pages": minor
3+
---
4+
5+
feat: input pr-number

.github/workflows/deploy.yml

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22

33
name: deploy
44
on:
5-
workflow_run:
6-
workflows:
7-
- test
8-
types:
9-
- completed
5+
pull_request:
6+
branches:
7+
- main
108

119
concurrency:
1210
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
1311

1412
jobs:
13+
fork:
14+
if: ${{ github.event.pull_request.head.repo.fork }}
15+
permissions:
16+
contents: read
17+
runs-on: ubuntu-latest
18+
timeout-minutes: 1
19+
steps:
20+
- name: Fork PR notice
21+
run: echo 'Fork pull request detected; skipping deploy job that requires repository secrets.'
22+
1523
deploy:
16-
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch != 'main' }}
24+
if: ${{ !github.event.pull_request.head.repo.fork }}
1725
permissions:
1826
actions: read
1927
contents: read
@@ -22,19 +30,7 @@ jobs:
2230
runs-on: ubuntu-latest
2331
timeout-minutes: 5
2432
steps:
25-
- if: github.event_name == 'workflow_run'
26-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
27-
with:
28-
repository: ${{ github.event.workflow_run.head_repository.full_name }}
29-
ref: ${{ github.event.workflow_run.head_sha }}
30-
- if: github.event_name != 'workflow_run'
31-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
32-
- name: Get PR number
33-
run: |
34-
echo "PR Number: ${{ github.event.number }}"
35-
- name: Get PR number other
36-
run: |
37-
echo "PR Number: ${{ github.event.pull_request.number }}"
33+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
3834
- id: 'cloudflare-pages'
3935
uses: ./
4036
with:

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ github-token:
6161
github-environment:
6262
description: 'GitHub environment to deploy to. You need to manually create this for the github repo'
6363
required: true
64+
pr-number:
65+
description: 'GitHub pull request number to comment on. If not set, the action auto-detects from the event payload.'
66+
required: false
6467
working-directory:
6568
description: 'Directory to run wrangler cli from'
6669
required: false
@@ -136,7 +139,7 @@ jobs:
136139

137140
### Fork pull requests with `workflow_run`
138141

139-
When pull requests come from forks, the initial `pull_request` workflow may not have access to secrets. Use a second workflow triggered by `workflow_run` to deploy from the original repository context after approval.
142+
When pull requests come from forks, the initial `pull_request` workflow may not have access to secrets. Use a second workflow triggered by `workflow_run` to deploy from the original repository context after approval, and set the `pr-number` input so the action can resolve the correct pull request to comment on.
140143

141144
```yaml
142145
name: Deploy PR Preview (Fork Safe)
@@ -168,9 +171,10 @@ jobs:
168171
directory: dist
169172
github-token: ${{ secrets.GITHUB_TOKEN }}
170173
github-environment: preview
174+
pr-number: # The PR number
171175
```
172176

173-
This action supports the `workflow_run` event and will use the `workflow_run` head commit SHA and branch for deployment metadata and PR comments.
177+
This action supports the `workflow_run` event and will use the `workflow_run` head commit SHA and branch for deployment metadata.
174178

175179
## Comment Example
176180

__tests__/common/github/comment.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
QueryPullRequestNodeIdByBranch
1212
} from '@/common/github/comment.js'
1313
import * as Context from '@/common/github/context.js'
14+
import * as CommonInputs from '@/common/inputs.js'
1415
import RESPONSE_DEPLOYMENTS from '@/responses/api.cloudflare.com/pages/deployments/deployments.response.json' with {type: 'json'}
1516
import {setMockApi} from '@/tests/helpers/api.js'
1617
import {EVENT_NAMES} from '@/types/github/workflow-events.js'
@@ -207,6 +208,102 @@ describe(addComment, () => {
207208
})
208209
})
209210

211+
describe('pr-number input', () => {
212+
test('should use pr-number to resolve pull request node id', async () => {
213+
expect.assertions(1)
214+
215+
vi.spyOn(CommonInputs, 'useCommonInputs').mockReturnValueOnce({
216+
cloudflareApiToken: 'mock-cloudflare-api-token',
217+
gitHubApiToken: 'mock-github-token',
218+
gitHubEnvironment: undefined,
219+
prNumber: '123',
220+
wranglerVersion: 'mock-wrangler-version'
221+
})
222+
223+
vi.spyOn(Context, 'useContextEvent').mockReturnValue({
224+
eventName: 'workflow_dispatch',
225+
payload: {}
226+
} as Readonly<WorkflowEventExtract<'workflow_dispatch'>>)
227+
228+
vi.spyOn(Context, 'useContext').mockReturnValue({
229+
event: {
230+
eventName: 'workflow_dispatch',
231+
payload: {} as never
232+
},
233+
repo: {
234+
owner: 'andykenward',
235+
repo: 'github-actions-cloudflare-pages',
236+
node_id: 'repo_node_id'
237+
},
238+
branch: undefined,
239+
sha: 'mock-github-sha',
240+
graphqlEndpoint: 'https://api.github.com/graphql',
241+
ref: 'refs/heads/feature-branch'
242+
} as unknown as ReturnType<typeof Context.useContext>)
243+
244+
mockApi.interceptGithub(
245+
{
246+
query: QueryPullRequestNodeId,
247+
variables: {
248+
owner: 'andykenward',
249+
repo: 'github-actions-cloudflare-pages',
250+
number: 123
251+
}
252+
},
253+
{
254+
data: {
255+
repository: {
256+
pullRequest: {
257+
id: 'MDExOlB1bGxSZXF1ZXN0Mjc5MTQ3NDM3'
258+
}
259+
}
260+
}
261+
}
262+
)
263+
264+
mockApi.interceptGithub(
265+
{
266+
query: MutationAddComment,
267+
variables: {
268+
subjectId: 'MDExOlB1bGxSZXF1ZXN0Mjc5MTQ3NDM3',
269+
body: '## Cloudflare Pages Deployment\n**Event Name:** workflow_dispatch\n**Environment:** production\n**Project:** cloudflare-pages-action\n**Built with commit:** mock-github-sha\n**Preview URL:** https://206e215c.cloudflare-pages-action-a5z.pages.dev\n**Branch Preview URL:** https://unknown-branch.cloudflare-pages-action-a5z.pages.dev\n\n### Wrangler Output\nsuccess'
270+
}
271+
},
272+
{
273+
data: {
274+
addComment: {
275+
commentEdge: {
276+
node: {
277+
id: '1'
278+
}
279+
}
280+
}
281+
}
282+
}
283+
)
284+
285+
const comment = await addComment(mockData, 'success')
286+
287+
expect(comment).toBe('1')
288+
})
289+
290+
test('should throw for invalid pr-number input', async () => {
291+
expect.assertions(1)
292+
293+
vi.spyOn(CommonInputs, 'useCommonInputs').mockReturnValueOnce({
294+
cloudflareApiToken: 'mock-cloudflare-api-token',
295+
gitHubApiToken: 'mock-github-token',
296+
gitHubEnvironment: undefined,
297+
prNumber: 'abc',
298+
wranglerVersion: 'mock-wrangler-version'
299+
})
300+
301+
await expect(addComment(mockData, 'success')).rejects.toThrow(
302+
'Invalid pr-number input: abc'
303+
)
304+
})
305+
})
306+
210307
describe('eventName: workflow_dispatch', () => {
211308
test('should add comment', async () => {
212309
expect.assertions(1)

__tests__/common/inputs.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('common', () => {
5555
cloudflareApiToken: 'mock-cloudflare-api-token',
5656
gitHubApiToken: 'mock-github-token',
5757
gitHubEnvironment: 'mock-github-environment',
58+
prNumber: undefined,
5859
wranglerVersion: 'mock-wrangler-version'
5960
})
6061
})
@@ -71,6 +72,7 @@ describe('common', () => {
7172
cloudflareApiToken: 'mock-cloudflare-api-token',
7273
gitHubApiToken: 'mock-github-token',
7374
gitHubEnvironment: undefined,
75+
prNumber: undefined,
7476
wranglerVersion: packageJson.devDependencies.wrangler
7577
})
7678
})

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ inputs:
2424
github-environment:
2525
description: 'GitHub environment to deploy to. You need to manually create this for the github repo'
2626
required: true
27+
pr-number:
28+
description: 'GitHub pull request number to comment on. If not set, the action auto-detects from the event payload.'
29+
required: false
2730
working-directory:
2831
description: 'Directory to run wrangler cli from'
2932
required: false

0 commit comments

Comments
 (0)