@vzn/vx-ci 0.0.642 → 0.0.643
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -10
- package/package.json +2 -2
- package/src/cache-scope.ts +77 -31
- package/src/plugin.ts +9 -8
package/README.md
CHANGED
|
@@ -55,16 +55,26 @@ github({
|
|
|
55
55
|
|
|
56
56
|
## Cache scope
|
|
57
57
|
|
|
58
|
-
On Actions (`GITHUB_ACTIONS=true`)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
On Actions (`GITHUB_ACTIONS=true`) the plugin's `config` stage names the
|
|
59
|
+
run's branch through `ctx.setBranch`, from the ref (`GITHUB_REF`,
|
|
60
|
+
`GITHUB_REF_NAME`, `GITHUB_HEAD_REF`, `GITHUB_REPOSITORY`) and the
|
|
61
|
+
`GITHUB_EVENT_PATH` payload. A push to the default branch names no scope (reads and writes
|
|
62
|
+
the default keys). A push to, or a pull request from, a branch names
|
|
63
|
+
`<owner>/<repo>:<branch>` of the repository the branch lives in, so a
|
|
64
|
+
fork's pull request names the fork; so does a `pull_request_target` or
|
|
65
|
+
`workflow_run` run on a PR's behalf (by `GITHUB_EVENT_NAME`, though its
|
|
66
|
+
ref is main's), and an `issue_comment` on a PR, which names no branch,
|
|
67
|
+
`pr-<n>`. A pull request also names its base commit
|
|
68
|
+
(`pull_request.base.sha`), so vx diffs the merge checkout from it with
|
|
69
|
+
no git call. A scoped run's changed tasks read the scope's remote entry,
|
|
70
|
+
the rest the default keys, and every task it builds is saved to the
|
|
71
|
+
remote under the scope; the local cache never knows a scope. A
|
|
72
|
+
PR never writes what main reads, and a laptop on the branch derives the
|
|
73
|
+
same scope. Whether a run writes at all is `cache.policy`'s call.
|
|
74
|
+
`VX_CACHE_SCOPE` overrides it. This is a client-side convention: the
|
|
75
|
+
real boundary is a cache token the server limits, so give PR jobs one
|
|
76
|
+
that cannot write the default keys (`docs/security.md` § Cache
|
|
77
|
+
poisoning).
|
|
68
78
|
|
|
69
79
|
## How it works
|
|
70
80
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vzn/vx-ci",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.643",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "GitHub Actions integration for vx — a telemetry plugin that writes the run as a GitHub Actions job summary.",
|
|
6
6
|
"keywords": [
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@vzn/vx": "^0.0.
|
|
36
|
+
"@vzn/vx": "^0.0.643"
|
|
37
37
|
},
|
|
38
38
|
"repository": {
|
|
39
39
|
"type": "git",
|
package/src/cache-scope.ts
CHANGED
|
@@ -1,49 +1,92 @@
|
|
|
1
|
-
// The cache scope a GitHub Actions run
|
|
2
|
-
// the default branch writes the default keys,
|
|
1
|
+
// The cache scope and base commit a GitHub Actions run names through
|
|
2
|
+
// `setBranch`: the default branch writes the default keys, a branch its own
|
|
3
|
+
// scope, the one a laptop on that branch reads (owner, 2026-10-17).
|
|
3
4
|
import { readFile } from 'node:fs/promises'
|
|
5
|
+
import { branchCacheScope } from '@vzn/vx'
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
|
-
*
|
|
7
|
-
* default branch, outside Actions, or when the ref
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
8
|
+
* What an Actions run knows of its branch (owner, 2026-10-17). `scope`:
|
|
9
|
+
* none on a push to the default branch, outside Actions, or when the ref
|
|
10
|
+
* is unknown; else `<owner>/<repo>:<branch>`, the repository and branch a
|
|
11
|
+
* pull request comes from, or the branch or tag pushed. A fork's pull
|
|
12
|
+
* request names the fork, a scope a laptop on the base repository never
|
|
13
|
+
* derives. An event that runs in the default branch's context on a pull
|
|
14
|
+
* request's behalf (`pull_request_target`, `issue_comment` on a PR, a
|
|
15
|
+
* `workflow_run` a PR started) is the PR's, whatever `GITHUB_REF` says: a
|
|
16
|
+
* workflow that checks out the PR's head there would otherwise write the
|
|
17
|
+
* default keys. `issue_comment` names no branch, so it writes `pr-<n>`.
|
|
18
|
+
* `base`: a pull request's base commit (`pull_request.base.sha`), what its
|
|
19
|
+
* merge checkout is compared with, no git call needed.
|
|
13
20
|
*/
|
|
14
|
-
export async function
|
|
21
|
+
export async function githubBranch(
|
|
15
22
|
env: Readonly<Record<string, string | undefined>>,
|
|
16
|
-
): Promise<string
|
|
17
|
-
if (env['GITHUB_ACTIONS'] !== 'true') return
|
|
23
|
+
): Promise<{ scope?: string; base?: string }> {
|
|
24
|
+
if (env['GITHUB_ACTIONS'] !== 'true') return {}
|
|
18
25
|
const ref = env['GITHUB_REF'] ?? ''
|
|
19
|
-
const pr = /^refs\/pull\/(\d+)\//.exec(ref)
|
|
20
|
-
if (pr !== null) return `pr-${pr[1]}`
|
|
21
26
|
const event = await eventOf(env['GITHUB_EVENT_PATH'])
|
|
22
|
-
const
|
|
23
|
-
|
|
27
|
+
const repo = stringOf(event?.repository?.full_name) ?? env['GITHUB_REPOSITORY']
|
|
28
|
+
const base = stringOf(event?.pull_request?.base?.sha)
|
|
29
|
+
const withBase = (scope: string | undefined): { scope?: string; base?: string } => ({
|
|
30
|
+
...(scope !== undefined ? { scope } : {}),
|
|
31
|
+
...(base !== undefined ? { base } : {}),
|
|
32
|
+
})
|
|
33
|
+
if (/^refs\/pull\/\d+\//.test(ref)) {
|
|
34
|
+
const head = event?.pull_request?.head
|
|
35
|
+
const branch = env['GITHUB_HEAD_REF'] || stringOf(head?.ref)
|
|
36
|
+
if (branch !== undefined)
|
|
37
|
+
return withBase(headScope(branch, stringOf(head?.repo?.full_name), repo))
|
|
38
|
+
return withBase('pr-target')
|
|
39
|
+
}
|
|
40
|
+
const onBehalf = prOf(env['GITHUB_EVENT_NAME'], event, repo)
|
|
41
|
+
if (onBehalf !== undefined) return withBase(onBehalf)
|
|
24
42
|
const name = env['GITHUB_REF_NAME']
|
|
25
|
-
if (ref === '' || name === undefined || name === '') return
|
|
43
|
+
if (ref === '' || name === undefined || name === '' || repo === undefined) return {}
|
|
26
44
|
const branch = event?.repository?.default_branch
|
|
27
|
-
if (typeof branch === 'string' && branch !== '' && ref === `refs/heads/${branch}`)
|
|
28
|
-
|
|
29
|
-
// `cache.scope` takes letters, digits and . _ - / @; a ref may hold more.
|
|
30
|
-
return `ref-${name.replace(/[^\w.@/-]/g, '_').slice(0, 124)}`
|
|
45
|
+
if (typeof branch === 'string' && branch !== '' && ref === `refs/heads/${branch}`) return {}
|
|
46
|
+
return { scope: branchCacheScope(repo, name) }
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
interface ActionsEvent {
|
|
34
|
-
repository?: { default_branch?: unknown }
|
|
35
|
-
pull_request?: {
|
|
50
|
+
repository?: { default_branch?: unknown; full_name?: unknown }
|
|
51
|
+
pull_request?: {
|
|
52
|
+
number?: unknown
|
|
53
|
+
head?: { ref?: unknown; repo?: { full_name?: unknown } }
|
|
54
|
+
base?: { sha?: unknown }
|
|
55
|
+
}
|
|
36
56
|
issue?: { number?: unknown; pull_request?: unknown }
|
|
37
|
-
workflow_run?: {
|
|
57
|
+
workflow_run?: {
|
|
58
|
+
event?: unknown
|
|
59
|
+
head_branch?: unknown
|
|
60
|
+
head_repository?: { full_name?: unknown }
|
|
61
|
+
pull_requests?: { number?: unknown }[]
|
|
62
|
+
}
|
|
38
63
|
}
|
|
39
64
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
65
|
+
function stringOf(v: unknown): string | undefined {
|
|
66
|
+
return typeof v === 'string' && v !== '' ? v : undefined
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A head branch's scope: the repository it lives in (a fork's own), then the branch. */
|
|
70
|
+
function headScope(branch: string, headRepo: string | undefined, repo: string | undefined): string {
|
|
71
|
+
return branchCacheScope(headRepo ?? repo ?? 'unknown/unknown', branch)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The scope of an event that runs in the base branch's context for a PR; `pr-target` when its head is unknown. */
|
|
75
|
+
function prOf(
|
|
76
|
+
name: string | undefined,
|
|
77
|
+
event: ActionsEvent | undefined,
|
|
78
|
+
repo: string | undefined,
|
|
79
|
+
): string | undefined {
|
|
80
|
+
if (name === 'pull_request_target') {
|
|
81
|
+
const head = event?.pull_request?.head
|
|
82
|
+
const branch = stringOf(head?.ref)
|
|
83
|
+
return branch === undefined
|
|
84
|
+
? 'pr-target'
|
|
85
|
+
: headScope(branch, stringOf(head?.repo?.full_name), repo)
|
|
86
|
+
}
|
|
45
87
|
if (name === 'issue_comment' && event?.issue?.pull_request != null) {
|
|
46
|
-
|
|
88
|
+
const n = event.issue.number
|
|
89
|
+
return typeof n === 'number' && Number.isInteger(n) && n > 0 ? `pr-${n}` : 'pr-target'
|
|
47
90
|
}
|
|
48
91
|
const run = event?.workflow_run
|
|
49
92
|
if (
|
|
@@ -51,7 +94,10 @@ function prOf(name: string | undefined, event: ActionsEvent | undefined): string
|
|
|
51
94
|
typeof run?.event === 'string' &&
|
|
52
95
|
run.event.startsWith('pull_request')
|
|
53
96
|
) {
|
|
54
|
-
|
|
97
|
+
const branch = stringOf(run.head_branch)
|
|
98
|
+
return branch === undefined
|
|
99
|
+
? 'pr-target'
|
|
100
|
+
: headScope(branch, stringOf(run.head_repository?.full_name), repo)
|
|
55
101
|
}
|
|
56
102
|
return undefined
|
|
57
103
|
}
|
package/src/plugin.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
type VxPlugin,
|
|
14
14
|
} from '@vzn/vx'
|
|
15
15
|
import type { FetchFn } from './checks.js'
|
|
16
|
-
import {
|
|
16
|
+
import { githubBranch } from './cache-scope.js'
|
|
17
17
|
|
|
18
18
|
// Every run evaluates `.vx/workspace.ts`; the sink, the summary renderer
|
|
19
19
|
// and the Checks API client load only where a summary file is set. The
|
|
@@ -40,9 +40,11 @@ export interface GithubPluginOptions {
|
|
|
40
40
|
/** Check-run name. Default: `'vx'`. */
|
|
41
41
|
checkName?: string
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
43
|
+
* Name the run's cache scope and base commit from the Actions event: a
|
|
44
|
+
* push to the default branch keeps the default keys, a branch or its pull
|
|
45
|
+
* request writes to `<owner>/<repo>:<branch>` (a fork's names the fork),
|
|
46
|
+
* and a pull request is compared with its base commit. `VX_CACHE_SCOPE`
|
|
47
|
+
* beats it. Default: on; `false` opts out.
|
|
46
48
|
*/
|
|
47
49
|
cacheScope?: boolean
|
|
48
50
|
/** Test seam — inject the append. Defaults to fs appendFile. */
|
|
@@ -94,10 +96,9 @@ export function github(options: GithubPluginOptions = {}): VxPlugin | undefined
|
|
|
94
96
|
throw new UserError(`github() option "${key}" must be a non-empty string, got ""`)
|
|
95
97
|
}
|
|
96
98
|
return definePlugin(import.meta, {
|
|
97
|
-
async config(
|
|
98
|
-
if (options.cacheScope === false
|
|
99
|
-
|
|
100
|
-
if (scope !== undefined) workspace.cache = { ...workspace.cache, scope }
|
|
99
|
+
async config(_workspace, ctx) {
|
|
100
|
+
if (options.cacheScope === false) return
|
|
101
|
+
ctx.setBranch(await githubBranch(process.env))
|
|
101
102
|
},
|
|
102
103
|
telemetry(ctx) {
|
|
103
104
|
const file = options.summaryFile ?? process.env['GITHUB_STEP_SUMMARY']
|