1818 * `head_file_present`, the step loads it from the workflow's own commit
1919 * (`git show "$WORKFLOW_SHA:<script>"`, or from the job's `.test-scope`
2020 * checkout, whose sparse directories hold it, directly or through a copy an
21- * earlier step made in `$RUNNER_TEMP`), or the step checks for it itself
22- * (`[ ! -f ... ]`). A job that does not measure the head takes the default
21+ * earlier step made in `$RUNNER_TEMP`), the step runs in the job's merge
22+ * checkout before its head checkout replaces the workspace, or the step checks
23+ * for it itself (`[ ! -f ... ]`). A job that does not measure the head takes the default
2324 * merge checkout instead, and this test does not apply to it.
2425 *
2526 * Scope: `.js`, `.mjs` and `.cjs` scripts named in a step's `run:`.
@@ -158,6 +159,24 @@ export function beforeHeadCheckout(job, step) {
158159 return head === - 1 || steps . indexOf ( step ) < head ;
159160}
160161
162+ /**
163+ * Whether the step runs in the job's merge checkout (the workflow's own
164+ * commit): a workspace checkout with no `ref` comes before the step, and the
165+ * head checkout comes after it.
166+ */
167+ export function inMergeCheckout ( job , step ) {
168+ const steps = job . steps ?? [ ] ;
169+ const merge = steps . findIndex (
170+ each =>
171+ String ( each . uses ?? '' ) . startsWith ( 'actions/checkout@' ) &&
172+ each . with ?. ref == null &&
173+ ! each . with ?. path ,
174+ ) ;
175+ return (
176+ merge !== - 1 && merge < steps . indexOf ( step ) && beforeHeadCheckout ( job , step )
177+ ) ;
178+ }
179+
161180export function unguardedScripts ( job ) {
162181 const scope = ( job . steps ?? [ ] ) . find ( step =>
163182 String ( step . uses ?? '' ) . includes ( 'test-scope' ) ,
@@ -180,6 +199,7 @@ export function unguardedScripts(job) {
180199 beforeHeadCheckout ( job , step )
181200 )
182201 continue ;
202+ if ( inMergeCheckout ( job , step ) ) continue ;
183203 // Run from a copy an earlier step took from that scope checkout.
184204 if ( copiedFromScope ( job , step , script ) ) continue ;
185205 // The step skips a head without this script on its own.
@@ -246,6 +266,24 @@ describe('pull-request jobs that check out the head', () => {
246266 }
247267 } ) ;
248268
269+ it ( 'exempts a step in the merge checkout only before the head checkout' , ( ) => {
270+ const merge = { name : 'Merge' , uses : 'actions/checkout@v7' } ;
271+ const head = {
272+ name : 'Head' ,
273+ uses : 'actions/checkout@v7' ,
274+ with : { ref : '${{ github.event.pull_request.head.sha }}' } ,
275+ } ;
276+ const before = { name : 'Before' , run : 'node .github/scripts/new.mjs' } ;
277+ const after = { name : 'After' , run : 'node .github/scripts/new.mjs' } ;
278+ expect ( unguardedScripts ( { steps : [ merge , before , head , after ] } ) ) . toEqual ( [
279+ 'After: .github/scripts/new.mjs' ,
280+ ] ) ;
281+ // With no merge checkout, the step reads no tree of the workflow.
282+ expect ( unguardedScripts ( { steps : [ before , head ] } ) ) . toEqual ( [
283+ 'Before: .github/scripts/new.mjs' ,
284+ ] ) ;
285+ } ) ;
286+
249287 it ( "exempts only the script a step's own missing-file check names" , ( ) => {
250288 const run = [
251289 'D7_TEST=.github/scripts/d7.test.mjs' ,
0 commit comments