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
54 changes: 51 additions & 3 deletions .github/scripts/release-tag.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'

// A pre-push hook in a linked worktree exports GIT_DIR. Simulate that with a
// sentinel repo, then build the child env once without the hook's git
// variables. Every spawn in this file uses that env, so the sentinel must
// stay unchanged.
const sentinel = mkdtempSync(join(tmpdir(), 'virtual-git-sentinel-'))
process.env.GIT_DIR = join(sentinel, '.git')
const hookGitVars = [
'GIT_DIR',
'GIT_WORK_TREE',
'GIT_INDEX_FILE',
'GIT_COMMON_DIR',
]
const childEnv = { ...process.env }
for (const key of hookGitVars) delete childEnv[key]
const sentinelGit = (args) => {
const result = spawnSync('git', args, {
cwd: sentinel,
encoding: 'utf8',
env: childEnv,
})
assert.equal(result.status, 0, result.stderr)
return result.stdout.trim()
}
sentinelGit(['init', '-q', '-b', 'sentinel'])
sentinelGit(['config', 'user.name', 'sentinel'])
sentinelGit(['config', 'user.email', 'sentinel@example.invalid'])
sentinelGit(['commit', '-q', '--allow-empty', '-m', 'sentinel'])
sentinelGit(['tag', 'sentinel-tag'])
const sentinelRefs = sentinelGit(['for-each-ref'])

const script = fileURLToPath(new URL('./release-tag.mjs', import.meta.url))
const dir = mkdtempSync(join(tmpdir(), 'virtual-release-tag-'))
const repo = join(dir, 'repo')
Expand Down Expand Up @@ -48,17 +78,24 @@ console.error('unexpected gh call:', args);
process.exit(2);
`,
)
const chmod = spawnSync('chmod', ['+x', join(bin, 'gh')], { encoding: 'utf8' })
const chmod = spawnSync('chmod', ['+x', join(bin, 'gh')], {
encoding: 'utf8',
env: childEnv,
})
assert.equal(chmod.status, 0, chmod.stderr)

const env = {
...process.env,
...childEnv,
PATH: `${bin}:${process.env.PATH}`,
GH_LOG: ghLog,
GH_MARKER: ghMarker,
}
function git(args) {
const result = spawnSync('git', args, { cwd: repo, encoding: 'utf8' })
const result = spawnSync('git', args, {
cwd: repo,
encoding: 'utf8',
env: childEnv,
})
assert.equal(result.status, 0, result.stderr)
return result.stdout.trim()
}
Expand All @@ -84,6 +121,7 @@ try {
const init = spawnSync('git', ['init', '-q', '-b', 'rigel-release'], {
cwd: repo,
encoding: 'utf8',
env: childEnv,
})
assert.equal(init.status, 0, init.stderr)
git(['config', 'user.name', 'test'])
Expand Down Expand Up @@ -183,3 +221,13 @@ try {
} finally {
rmSync(dir, { recursive: true, force: true })
}
try {
assert.equal(
sentinelGit(['for-each-ref']),
sentinelRefs,
'FAIL GIT_DIR leaked into child git',
)
console.log('ok GIT_DIR from a hook leaves the sentinel repo unchanged')
} finally {
rmSync(sentinel, { recursive: true, force: true })
}
57 changes: 51 additions & 6 deletions .github/scripts/workflow-contract.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'

// A pre-push hook in a linked worktree exports GIT_DIR. Simulate that with a
// sentinel repo, then build the child env once without the hook's git
// variables. Every spawn in this file uses that env, so the sentinel must
// stay unchanged.
const sentinel = mkdtempSync(join(tmpdir(), 'virtual-git-sentinel-'))
process.env.GIT_DIR = join(sentinel, '.git')
const hookGitVars = [
'GIT_DIR',
'GIT_WORK_TREE',
'GIT_INDEX_FILE',
'GIT_COMMON_DIR',
]
const childEnv = { ...process.env }
for (const key of hookGitVars) delete childEnv[key]
const sentinelGit = (args) => {
const result = spawnSync('git', args, {
cwd: sentinel,
encoding: 'utf8',
env: childEnv,
})
assert.equal(result.status, 0, result.stderr)
return result.stdout.trim()
}
sentinelGit(['init', '-q', '-b', 'sentinel'])
sentinelGit(['config', 'user.name', 'sentinel'])
sentinelGit(['config', 'user.email', 'sentinel@example.invalid'])
sentinelGit(['commit', '-q', '--allow-empty', '-m', 'sentinel'])
sentinelGit(['tag', 'sentinel-tag'])
const sentinelRefs = sentinelGit(['for-each-ref'])

const scriptsDir = fileURLToPath(new URL('.', import.meta.url))
const root = fileURLToPath(new URL('../..', import.meta.url))
const publisherPath = join(root, '.github/workflows/publish-rigelbuild.yml')
Expand Down Expand Up @@ -43,7 +73,7 @@ try {
const dispatch = spawnSync(process.execPath, [resolverPath], {
encoding: 'utf8',
env: {
...process.env,
...childEnv,
EVENT_NAME: 'workflow_dispatch',
REF: 'refs/heads/rigel-release',
GITHUB_OUTPUT: outputPath,
Expand Down Expand Up @@ -82,13 +112,18 @@ passes(
const guardDir = mkdtempSync(join(tmpdir(), 'virtual-publisher-guard-'))
try {
const runGit = (args) => {
const result = spawnSync('git', args, { cwd: guardDir, encoding: 'utf8' })
const result = spawnSync('git', args, {
cwd: guardDir,
encoding: 'utf8',
env: childEnv,
})
assert.equal(result.status, 0, result.stderr)
return result.stdout.trim()
}
const init = spawnSync('git', ['init', '-q', '-b', 'rigel-release'], {
cwd: guardDir,
encoding: 'utf8',
env: childEnv,
})
assert.equal(init.status, 0, init.stderr)
runGit(['config', 'user.name', 'test'])
Expand All @@ -108,7 +143,7 @@ try {
cwd: guardDir,
encoding: 'utf8',
env: {
...process.env,
...childEnv,
EVENT_NAME: 'push',
REF: 'refs/tags/v3.0.0-rigel.1',
GITHUB_OUTPUT: join(guardDir, 'accepted-output'),
Expand All @@ -128,7 +163,7 @@ try {
cwd: guardDir,
encoding: 'utf8',
env: {
...process.env,
...childEnv,
EVENT_NAME: 'push',
REF: 'refs/tags/v3.0.0-rigel.2',
GITHUB_OUTPUT: join(guardDir, 'rejected-output'),
Expand All @@ -151,7 +186,7 @@ try {
cwd: guardDir,
encoding: 'utf8',
env: {
...process.env,
...childEnv,
EVENT_NAME: 'push',
REF: 'refs/tags/v3.0.0-rigel.3',
GITHUB_OUTPUT: join(guardDir, 'detached-output'),
Expand All @@ -174,7 +209,7 @@ try {
cwd: guardDir,
encoding: 'utf8',
env: {
...process.env,
...childEnv,
EVENT_NAME: 'push',
REF: 'refs/tags/v3.0.0-rc.1',
GITHUB_OUTPUT: join(guardDir, 'bad-shape-output'),
Expand All @@ -186,3 +221,13 @@ try {
} finally {
rmSync(guardDir, { recursive: true, force: true })
}
try {
assert.equal(
sentinelGit(['for-each-ref']),
sentinelRefs,
'FAIL GIT_DIR leaked into child git',
)
console.log('ok GIT_DIR from a hook leaves the sentinel repo unchanged')
} finally {
rmSync(sentinel, { recursive: true, force: true })
}
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ permissions:
jobs:
test:
name: Test
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
Expand Down
15 changes: 15 additions & 0 deletions hk.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Git hooks managed by hk (https://hk.jdx.dev).
amends "package://github.com/jdx/hk/releases/download/v2.11.1/hk@2.11.1#/Config.pkl"

default_branch = "origin/rigel-release"

hooks {
["pre-push"] {
steps {
["fork-release-contracts"] = new Step {
glob = List(".github/workflows/**", ".github/scripts/**")
check = "node .github/scripts/workflow-contract.test.mjs && node .github/scripts/release-tag.test.mjs"
}
}
}
}
Loading