Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
elif [ '${{ github.event_name }}' == 'workflow_dispatch' ]
then
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
echo "value=skipped" >> $GITHUB_OUTPUT
else
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
id: docs-change
run: |
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

- name: check for release
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_request_stats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
fetch-depth: 25

- 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

- uses: actions/download-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy-examples.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
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
CHANGED_EXAMPLES="examples/image-component" # always deploy as a workaround

Expand Down
64 changes: 3 additions & 61 deletions scripts/get-changed-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,25 @@
import fs from 'fs/promises'
import execa from 'execa'
import path from 'path'
import { getDiffRevision, getGitInfo } from './git-info.mjs'

/**
* Detects changed tests files by comparing the current branch with `origin/canary`
* Returns tests separated by test mode (dev/prod), as well as the corresponding commit hash
* that the current branch is pointing to
*/
export default async function getChangedTests() {
let eventData = {}

/** @type import('execa').Options */
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 =
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')
const { branchName, remoteUrl, commitSha, isCanary } = await getGitInfo()

if (isCanary) {
console.log(`Skipping flake detection for canary`)
return { devTests: [], prodTests: [] }
}

let diffRevision
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 diffRevision = await getDiffRevision()

const changesResult = await execa(
`git diff ${diffRevision} --name-only`,
Expand Down
79 changes: 79 additions & 0 deletions scripts/git-info.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// @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'
}
}
61 changes: 18 additions & 43 deletions scripts/run-for-change.js → scripts/run-for-change.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { promisify } = require('util')
const { exec: execOrig, spawn } = require('child_process')
// @ts-check
import { promisify } from 'util'
import { exec as execOrig, spawn } from 'child_process'
import { getDiffRevision, getGitInfo } from './git-info.mjs'

const exec = promisify(execOrig)

Expand Down Expand Up @@ -55,43 +57,16 @@ const CHANGE_ITEM_GROUPS = {
}

async function main() {
let eventData = {}

try {
eventData = require(process.env.GITHUB_EVENT_PATH)['pull_request'] || {}
} catch (_) {}

const branchName =
eventData?.head?.ref ||
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: '' }
}
)
const { branchName, remoteUrl, isCanary } = await getGitInfo()
const diffRevision = await getDiffRevision()

const changesResult = await exec(
`git diff ${diffRevision} --name-only`
).catch((err) => {
console.error(err)
return { stdout: '' }
})

console.error({ branchName, remoteUrl, isCanary, changesResult })
const changedFilesOutput = changesResult.stdout

Expand All @@ -102,7 +77,7 @@ async function main() {

if (!type) {
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')
Expand Down Expand Up @@ -131,7 +106,7 @@ async function main() {
)
}
let changedFilesCount = 0
let changedDirectories = []
let changedDirectories = new Set()

// always run for canary if flag is enabled
if (alwaysCanary && branchName === 'canary') {
Expand All @@ -151,7 +126,7 @@ async function main() {
const matchesItem = changeItems.some((item) => {
const found = file.startsWith(item)
if (found) {
changedDirectories.push(item)
changedDirectories.add(item)
}
return found
})
Expand All @@ -176,7 +151,7 @@ async function main() {

if (hasMatchingChange) {
if (listChangedDirectories) {
console.log(changedDirectories.join('\n'))
console.log(Array.from(changedDirectories).join('\n'))
return
}
const cmd = spawn(execArgs[0], execArgs.slice(1))
Expand Down
Loading