-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__test-action-checkout-issue-comment.yml
More file actions
106 lines (89 loc) · 3.98 KB
/
Copy path__test-action-checkout-issue-comment.yml
File metadata and controls
106 lines (89 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
name: Internal - Tests for checkout action (issue_comment simulation)
on:
workflow_call:
permissions:
contents: read
pull-requests: read
jobs:
test-checkout-issue-comment:
name: Checkout action on simulated issue_comment
runs-on: ubuntu-latest
steps:
- name: Arrange - Checkout repository
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
with:
persist-credentials: false
- name: Arrange - Get PR information for simulation
id: pr-info
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const assert = require('node:assert/strict');
const prNumber = 570;
assert.ok(prNumber, 'Could not determine PR number from static test configuration');
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
core.setOutput('head-sha', pr.data.head.sha);
core.setOutput('head-ref', pr.data.head.ref);
core.setOutput('pr-number', prNumber);
core.info(`PR #${prNumber} Head SHA: ${pr.data.head.sha}`);
core.info(`PR #${prNumber} Head Ref: ${pr.data.head.ref}`);
- name: Act - Simulate issue_comment event context and checkout
id: checkout
uses: ./actions/checkout
with:
ref: refs/pull/${{ steps.pr-info.outputs.pr-number }}/head
persist-credentials: true
- name: Assert - Verify correct PR SHA was checked out
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
EXPECTED_SHA: ${{ steps.pr-info.outputs.head-sha }}
with:
script: |
const assert = require('node:assert/strict');
const { execSync } = require('child_process');
const currentSha = execSync('git rev-parse HEAD').toString().trim();
const expectedSha = process.env.EXPECTED_SHA;
core.info(`Current SHA: ${currentSha}`);
core.info(`Expected PR Head SHA: ${expectedSha}`);
try {
assert.strictEqual(
currentSha,
expectedSha,
`Checked out SHA (${currentSha}) does not match PR head SHA (${expectedSha})`
);
} catch (error) {
core.setFailed(error.message);
return;
}
core.info('Verified: Checked out correct PR head SHA');
- name: Assert - Verify not on main branch
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
EXPECTED_BRANCH: ${{ steps.pr-info.outputs.head-ref }}
with:
script: |
const assert = require('node:assert/strict');
const { execSync } = require('child_process');
const currentBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
const expectedBranch = process.env.EXPECTED_BRANCH;
core.info(`Current branch/ref: ${currentBranch}`);
core.info(`Expected branch: ${expectedBranch}`);
try {
assert.notStrictEqual(currentBranch, 'main', 'Checked out main branch instead of PR branch');
assert.notStrictEqual(currentBranch, 'master', 'Checked out master branch instead of PR branch');
assert.ok(
currentBranch === 'HEAD' || currentBranch === expectedBranch,
`Checked out unexpected ref ${currentBranch}; expected HEAD or ${expectedBranch}`
);
} catch (error) {
core.setFailed(error.message);
return;
}
if (currentBranch === 'HEAD') {
core.warning('Repository is in detached HEAD state; this is expected when checking out refs/pull/<n>/head.');
}
core.info('Verified: Not on main/master branch');