Unverified Commit 5c045fb2 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[ci] Improve change detection logic in `run-for-change` script (#85619)

This PR extracts the existing git change detection logic from the `get-changed-tests` script into a separate module. This allows reusing the same logic in the `run-for-change` script, ensuring consistent behavior across the two scripts to determine changed files. Specifically, this ensures that in stacked PRs, the docs-only change detection (and the test-only change detection in the upstack PR) works correctly by diffing against the appropriate base branch.
parent 7d9c1259
...@@ -57,7 +57,7 @@ jobs: ...@@ -57,7 +57,7 @@ jobs:
elif [ '${{ github.event_name }}' == 'workflow_dispatch' ] elif [ '${{ github.event_name }}' == 'workflow_dispatch' ]
then then
echo "value=force-preview" >> $GITHUB_OUTPUT echo "value=force-preview" >> $GITHUB_OUTPUT
elif [[ $(node scripts/run-for-change.js --not --type docs --exec echo 'false') != 'false' ]]; elif [[ $(node scripts/run-for-change.mjs --not --type docs --exec echo 'false') != 'false' ]];
then then
echo "value=skipped" >> $GITHUB_OUTPUT echo "value=skipped" >> $GITHUB_OUTPUT
else else
......
...@@ -33,7 +33,7 @@ jobs: ...@@ -33,7 +33,7 @@ jobs:
id: docs-change id: docs-change
run: | run: |
echo "DOCS_ONLY<<EOF" >> $GITHUB_OUTPUT; echo "DOCS_ONLY<<EOF" >> $GITHUB_OUTPUT;
echo "$(node scripts/run-for-change.js --not --type docs --exec echo 'false')" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.mjs --not --type docs --exec echo 'false')" >> $GITHUB_OUTPUT;
echo 'EOF' >> $GITHUB_OUTPUT echo 'EOF' >> $GITHUB_OUTPUT
- name: check for release - name: check for release
......
...@@ -46,7 +46,7 @@ jobs: ...@@ -46,7 +46,7 @@ jobs:
fetch-depth: 25 fetch-depth: 25
- name: Check non-docs only change - name: Check non-docs only change
run: echo "DOCS_CHANGE<<EOF" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')" >> $GITHUB_OUTPUT; echo 'EOF' >> $GITHUB_OUTPUT run: echo "DOCS_CHANGE<<EOF" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.mjs --not --type docs --exec echo 'nope')" >> $GITHUB_OUTPUT; echo 'EOF' >> $GITHUB_OUTPUT
id: docs-change id: docs-change
- uses: actions/download-artifact@v4 - uses: actions/download-artifact@v4
......
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
#CHANGED_EXAMPLES=$(node scripts/run-for-change.js --type deploy-examples --listChangedDirectories) #CHANGED_EXAMPLES=$(node scripts/run-for-change.mjs --type deploy-examples --listChangedDirectories)
##### TODO: fix the script above so it can work for stable releases which reach back multiple commits ##### TODO: fix the script above so it can work for stable releases which reach back multiple commits
CHANGED_EXAMPLES="examples/image-component" # always deploy as a workaround CHANGED_EXAMPLES="examples/image-component" # always deploy as a workaround
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import fs from 'fs/promises' import fs from 'fs/promises'
import execa from 'execa' import execa from 'execa'
import path from 'path' import path from 'path'
import { getDiffRevision, getGitInfo } from './git-info.mjs'
/** /**
* Detects changed tests files by comparing the current branch with `origin/canary` * Detects changed tests files by comparing the current branch with `origin/canary`
...@@ -9,76 +10,17 @@ import path from 'path' ...@@ -9,76 +10,17 @@ import path from 'path'
* that the current branch is pointing to * that the current branch is pointing to
*/ */
export default async function getChangedTests() { export default async function getChangedTests() {
let eventData = {}
/** @type import('execa').Options */ /** @type import('execa').Options */
const EXECA_OPTS = { shell: true } const EXECA_OPTS = { shell: true }
/** @type import('execa').Options */
const EXECA_OPTS_STDIO = { ...EXECA_OPTS, stdio: 'inherit' }
try {
eventData =
JSON.parse(
await fs.readFile(process.env.GITHUB_EVENT_PATH || '', 'utf8')
)['pull_request'] || {}
} catch (_) {}
const branchName =
eventData?.head?.ref ||
process.env.GITHUB_REF_NAME ||
(await execa('git rev-parse --abbrev-ref HEAD', EXECA_OPTS)).stdout
const remoteUrl = const { branchName, remoteUrl, commitSha, isCanary } = await getGitInfo()
eventData?.head?.repo?.full_name ||
process.env.GITHUB_REPOSITORY ||
(await execa('git remote get-url origin', EXECA_OPTS)).stdout
const commitSha =
eventData?.head?.sha ||
process.env.GITHUB_SHA ||
(await execa('git rev-parse HEAD', EXECA_OPTS)).stdout
const isCanary =
branchName.trim() === 'canary' && remoteUrl.includes('vercel/next.js')
if (isCanary) { if (isCanary) {
console.log(`Skipping flake detection for canary`) console.log(`Skipping flake detection for canary`)
return { devTests: [], prodTests: [] } return { devTests: [], prodTests: [] }
} }
let diffRevision const diffRevision = await getDiffRevision()
if (
process.env.GITHUB_ACTIONS === 'true' &&
process.env.GITHUB_EVENT_NAME === 'pull_request'
) {
// GH Actions for `pull_request` run on the merge commit so HEAD~1:
// 1. includes all changes in the PR
// e.g. in
// A-B-C-main - F
// \ /
// D-E-branch
// GH actions for `branch` runs on F, so a diff for HEAD~1 includes the diff of D and E combined
// 2. Includes all changes of the commit for pushes
diffRevision = 'HEAD~1'
} else {
try {
await execa(
'git remote set-branches --add origin canary',
EXECA_OPTS_STDIO
)
await execa('git fetch origin canary --depth=20', EXECA_OPTS_STDIO)
} catch (err) {
console.error(await execa('git remote -v', EXECA_OPTS_STDIO))
console.error(`Failed to fetch origin/canary`, err)
}
// TODO: We should diff against the merge base with origin/canary not directly against origin/canary.
// A --- B ---- origin/canary
// \
// \-- C ---- HEAD
// `git diff origin/canary` includes B and C
// But we should only include C.
diffRevision = 'origin/canary'
}
const changesResult = await execa( const changesResult = await execa(
`git diff ${diffRevision} --name-only`, `git diff ${diffRevision} --name-only`,
......
// @ts-check
import fs from 'fs/promises'
import { promisify } from 'util'
import { exec as execOrig } from 'child_process'
const exec = promisify(execOrig)
/**
* Gets git repository information from the environment
* @returns {Promise<{branchName: string, remoteUrl: string, commitSha: string, isCanary: boolean}>}
*/
export async function getGitInfo() {
let eventData = {}
try {
eventData =
JSON.parse(
await fs.readFile(process.env.GITHUB_EVENT_PATH || '', 'utf8')
)['pull_request'] || {}
} catch (_) {}
const branchName =
eventData?.head?.ref ||
process.env.GITHUB_REF_NAME ||
(await exec('git rev-parse --abbrev-ref HEAD')).stdout.trim()
const remoteUrl =
eventData?.head?.repo?.full_name ||
process.env.GITHUB_REPOSITORY ||
(await exec('git remote get-url origin')).stdout.trim()
const commitSha =
eventData?.head?.sha ||
process.env.GITHUB_SHA ||
(await exec('git rev-parse HEAD')).stdout.trim()
const isCanary =
branchName === 'canary' && remoteUrl.includes('vercel/next.js')
return { branchName, remoteUrl, commitSha, isCanary }
}
/**
* Determines the appropriate git diff revision based on the environment
* @returns {Promise<string>} The git revision to diff against
*/
export async function getDiffRevision() {
if (
process.env.GITHUB_ACTIONS === 'true' &&
process.env.GITHUB_EVENT_NAME === 'pull_request'
) {
// GH Actions for `pull_request` run on the merge commit so HEAD~1:
// 1. includes all changes in the PR
// e.g. in
// A-B-C-main - F
// \ /
// D-E-branch
// GH actions for `branch` runs on F, so a diff for HEAD~1 includes the diff of D and E combined
// 2. Includes all changes of the commit for pushes
return 'HEAD~1'
} else {
try {
await exec('git remote set-branches --add origin canary')
await exec('git fetch origin canary --depth=20')
} catch (err) {
const remoteInfo = await exec('git remote -v')
console.error(remoteInfo.stdout)
console.error(remoteInfo.stderr)
console.error(`Failed to fetch origin/canary`, err)
}
// TODO: We should diff against the merge base with origin/canary not directly against origin/canary.
// A --- B ---- origin/canary
// \
// \-- C ---- HEAD
// `git diff origin/canary` includes B and C
// But we should only include C.
return 'origin/canary'
}
}
const { promisify } = require('util') // @ts-check
const { exec: execOrig, spawn } = require('child_process') import { promisify } from 'util'
import { exec as execOrig, spawn } from 'child_process'
import { getDiffRevision, getGitInfo } from './git-info.mjs'
const exec = promisify(execOrig) const exec = promisify(execOrig)
...@@ -55,43 +57,16 @@ const CHANGE_ITEM_GROUPS = { ...@@ -55,43 +57,16 @@ const CHANGE_ITEM_GROUPS = {
} }
async function main() { async function main() {
let eventData = {} const { branchName, remoteUrl, isCanary } = await getGitInfo()
const diffRevision = await getDiffRevision()
try {
eventData = require(process.env.GITHUB_EVENT_PATH)['pull_request'] || {} const changesResult = await exec(
} catch (_) {} `git diff ${diffRevision} --name-only`
).catch((err) => {
const branchName = console.error(err)
eventData?.head?.ref || return { stdout: '' }
process.env.GITHUB_REF_NAME || })
(await exec('git rev-parse --abbrev-ref HEAD')).stdout
const remoteUrl =
eventData?.head?.repo?.full_name ||
process.env.GITHUB_REPOSITORY ||
(await exec('git remote get-url origin')).stdout
const isCanary =
branchName.trim() === 'canary' && remoteUrl.includes('vercel/next.js')
try {
await exec('git remote set-branches --add origin canary')
await exec('git fetch origin canary --depth=20')
} catch (err) {
console.error(await exec('git remote -v'))
console.error(`Failed to fetch origin/canary`, err)
}
// if we are on the canary branch only diff current commit
const toDiff = isCanary
? `${process.env.GITHUB_SHA || 'canary'}~`
: 'origin/canary...'
const changesResult = await exec(`git diff ${toDiff} --name-only`).catch(
(err) => {
console.error(err)
return { stdout: '' }
}
)
console.error({ branchName, remoteUrl, isCanary, changesResult }) console.error({ branchName, remoteUrl, isCanary, changesResult })
const changedFilesOutput = changesResult.stdout const changedFilesOutput = changesResult.stdout
...@@ -102,7 +77,7 @@ async function main() { ...@@ -102,7 +77,7 @@ async function main() {
if (!type) { if (!type) {
throw new Error( throw new Error(
`Missing "--type" flag, e.g. "node run-for-change.js --type docs"` `Missing "--type" flag, e.g. "node run-for-change.mjs --type docs"`
) )
} }
const execArgIndex = process.argv.indexOf('--exec') const execArgIndex = process.argv.indexOf('--exec')
...@@ -131,7 +106,7 @@ async function main() { ...@@ -131,7 +106,7 @@ async function main() {
) )
} }
let changedFilesCount = 0 let changedFilesCount = 0
let changedDirectories = [] let changedDirectories = new Set()
// always run for canary if flag is enabled // always run for canary if flag is enabled
if (alwaysCanary && branchName === 'canary') { if (alwaysCanary && branchName === 'canary') {
...@@ -151,7 +126,7 @@ async function main() { ...@@ -151,7 +126,7 @@ async function main() {
const matchesItem = changeItems.some((item) => { const matchesItem = changeItems.some((item) => {
const found = file.startsWith(item) const found = file.startsWith(item)
if (found) { if (found) {
changedDirectories.push(item) changedDirectories.add(item)
} }
return found return found
}) })
...@@ -176,7 +151,7 @@ async function main() { ...@@ -176,7 +151,7 @@ async function main() {
if (hasMatchingChange) { if (hasMatchingChange) {
if (listChangedDirectories) { if (listChangedDirectories) {
console.log(changedDirectories.join('\n')) console.log(Array.from(changedDirectories).join('\n'))
return return
} }
const cmd = spawn(execArgs[0], execArgs.slice(1)) const cmd = spawn(execArgs[0], execArgs.slice(1))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment