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
107 changes: 101 additions & 6 deletions .github/scripts/npm-release.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,36 @@ export function releaseVersion(version, tag, pr, sha) {

export async function resolveRelease(github, context) {
if (context.eventName === "issue_comment")
return resolveCommentBeta(github, context);
return resolveCommentBeta(github, context.payload, context.repo);
if (context.eventName === "workflow_dispatch") {
const inputs = context.payload.inputs;
if (
!/^[1-9]\d*$/.test(inputs?.["comment-id"]) ||
!/^[a-f0-9]{40}$/.test(inputs?.["head-sha"])
) {
throw new Error("Invalid beta dispatch inputs");
}
const { data: comment } = await github.rest.issues.getComment({
...context.repo,
comment_id: Number(inputs["comment-id"]),
});
const number = Number(
comment.issue_url.match(/\/issues\/([1-9]\d*)$/)?.[1],
);
if (!number || comment.updated_at !== comment.created_at) {
throw new Error("The original /beta comment is no longer valid");
}
return resolveCommentBeta(
github,
{
action: "created",
issue: { number, pull_request: {} },
comment,
},
context.repo,
inputs["head-sha"],
);
}
const repo = context.repo;
const { data: main } = await github.rest.repos.getBranch({
...repo,
Expand Down Expand Up @@ -102,11 +131,10 @@ export async function resolveRelease(github, context) {
return { sha, ...releaseVersion(pkg.version, tag, pr, sha) };
}

async function resolveCommentBeta(github, context) {
const { action, issue, comment } = context.payload;
async function resolveCommentBeta(github, payload, repo, expectedSha) {
const { action, issue, comment } = payload;
if (action !== "created" || !issue.pull_request || comment.body !== "/beta")
return null;
const repo = context.repo;
const { data: permission } =
await github.rest.repos.getCollaboratorPermissionLevel({
...repo,
Expand All @@ -129,12 +157,43 @@ async function resolveCommentBeta(github, context) {
!pr.head.repo
)
return null;
if (expectedSha && pr.head.sha !== expectedSha) return null;
// A queued comment must not authorize code pushed after that comment.
if (Date.parse(pr.updated_at) > Date.parse(comment.created_at)) {
throw new Error(
"PR changed after /beta; post a fresh comment after CI passes",
);
}
let lastForcePush;
if (!expectedSha) {
const { repository } = await github.graphql(
`
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $number) {
headRefOid
timelineItems(last: 1, itemTypes: [HEAD_REF_FORCE_PUSHED_EVENT]) {
nodes { ... on HeadRefForcePushedEvent { createdAt afterCommit { oid } } }
}
}
}
}
`,
{ ...repo, number: issue.number },
);
const current = repository?.pullRequest;
if (
current?.headRefOid !== pr.head.sha ||
current.timelineItems.nodes.some(
(node) => Date.parse(node.createdAt) >= Date.parse(comment.created_at),
)
) {
throw new Error(
"The PR head changed at or after /beta; post a fresh comment.",
);
}
[lastForcePush] = current.timelineItems.nodes;
}
const files = await github.paginate(github.rest.pulls.listFiles, {
...repo,
pull_number: issue.number,
Expand Down Expand Up @@ -162,7 +221,12 @@ async function resolveCommentBeta(github, context) {
per_page: 100,
});
const latest = runs
.filter((run) => run.pull_requests.some((p) => p.number === pr.number))
.filter((run) =>
run.pull_requests?.length
? run.pull_requests.some((p) => p.number === pr.number)
: run.head_repository?.full_name === pr.head.repo?.full_name &&
run.head_branch === pr.head.ref,
)
.sort((a, b) => b.run_number - a.run_number)[0];
if (
!latest ||
Expand All @@ -171,6 +235,9 @@ async function resolveCommentBeta(github, context) {
) {
throw new Error("The PR head must pass Compatibility before /beta");
}
if (Date.parse(latest.created_at) >= Date.parse(comment.created_at)) {
throw new Error("The candidate CI must start before the /beta comment");
}
const { data: file } = await github.rest.repos.getContent({
...repo,
path: "package.json",
Expand All @@ -179,9 +246,37 @@ async function resolveCommentBeta(github, context) {
const pkg = JSON.parse(Buffer.from(file.content, "base64").toString("utf8"));
if (pkg.name !== "cf-workers-native")
throw new Error("Unexpected trusted package name");
if (
lastForcePush &&
lastForcePush.afterCommit?.oid !== sha &&
Date.parse(latest.created_at) <= Date.parse(lastForcePush.createdAt)
) {
throw new Error(
"The candidate predates the last head rewind; post /beta after fresh CI",
);
}
const artifactName = `npm-beta-pr${pr.number}-${sha}-${latest.run_attempt}`;
const artifacts = await github.paginate(
github.rest.actions.listWorkflowRunArtifacts,
{
...repo,
run_id: latest.id,
per_page: 100,
},
);
if (
!artifacts.some(
(artifact) => artifact.name === artifactName && !artifact.expired,
)
) {
throw new Error(
"The immutable beta candidate is missing or expired; select Re-run all jobs in Compatibility, then post /beta again",
);
}
return {
sha,
repository: pr.head.repo.full_name,
candidateRunId: latest.id,
candidateArtifact: artifactName,
pr: pr.number,
...releaseVersion(pkg.version, undefined, pr.number, sha),
};
Expand Down
76 changes: 73 additions & 3 deletions .github/scripts/npm-release.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,31 @@ function commentFixture() {
state: "open",
draft: false,
base: { ref: "main" },
head: { sha, repo: { full_name: "contributor/fork" } },
head: { sha, ref: "feature", repo: { full_name: "contributor/fork" } },
updated_at: "2026-10-19T07:00:00Z",
};
f.state.files = [];
f.state.runs[0].pull_requests = [{ number: 43 }];
Object.assign(f.state.runs[0], {
id: 12,
run_attempt: 1,
created_at: "2026-10-18",
head_branch: "feature",
head_repository: { full_name: "contributor/fork" },
pull_requests: [],
});
f.github.rest.actions.listWorkflowRunArtifacts = "artifacts";
f.state.artifacts = [{ name: `npm-beta-pr43-${sha}-1`, expired: false }];
f.github.rest.repos.getCollaboratorPermissionLevel = async () => ({
data: { permission: f.state.permission, role_name: f.state.role },
});
f.github.graphql = async () => ({
repository: {
pullRequest: {
headRefOid: f.state.pr.head.sha,
timelineItems: { nodes: f.state.forcePushes ?? [] },
},
},
});
f.github.rest.pulls = {
get: async () => ({ data: f.state.pr }),
listFiles: "files",
Expand All @@ -181,7 +198,8 @@ test("/beta supports an authorized fork head without changing latest", async ()
const plan = await resolveRelease(f.github, f.context);
assert.deepEqual(plan, {
sha,
repository: "contributor/fork",
candidateRunId: 12,
candidateArtifact: `npm-beta-pr43-${sha}-1`,
pr: 43,
version: "0.1.1-beta.pr43.shaaaaaaaaaaaaa",
channel: "beta",
Expand Down Expand Up @@ -217,8 +235,19 @@ test("/beta refuses workflow changes, unmatched PR CI and updates after the comm
]),
(f) => (f.state.files = [{ filename: ".github/scripts/npm-release.mjs" }]),
(f) => (f.state.runs[0].pull_requests = [{ number: 99 }]),
(f) => (f.state.runs[0].head_branch = "unrelated"),
(f) => (f.state.artifacts[0].name = `npm-beta-pr99-${sha}-1`),
(f) => (f.state.runs[0].conclusion = "failure"),
(f) => (f.state.runs[0].created_at = f.context.payload.comment.created_at),
(f) => (f.state.pr.updated_at = "2026-10-19T07:00:01Z"),
(f) => (f.state.forcePushes = [{ createdAt: "2026-10-19T07:00:00Z" }]),
(f) =>
(f.state.forcePushes = [
{
createdAt: "2026-10-19T06:00:00Z",
afterCommit: { oid: "b".repeat(40) },
},
]),
]) {
const f = commentFixture();
change(f);
Expand All @@ -234,11 +263,13 @@ test("publication rechecks current permission, head and CI after building", asyn
]) {
const f = commentFixture();
const plan = await resolveRelease(f.github, f.context);
dispatchComment(f);
change(f);
assert.equal(await isReleaseCurrent(f.github, f.context, plan), false);
}
const f = commentFixture();
const plan = await resolveRelease(f.github, f.context);
dispatchComment(f);
f.state.runs[0].conclusion = "failure";
await assert.rejects(isReleaseCurrent(f.github, f.context, plan));
});
Expand All @@ -249,3 +280,42 @@ test("GitHub maintain role is accepted even when the legacy permission is write"
f.state.role = "maintain";
assert.equal((await resolveRelease(f.github, f.context)).channel, "beta");
});

function dispatchComment(f) {
const comment = {
...f.context.payload.comment,
updated_at: f.context.payload.comment.created_at,
issue_url:
"https://api.github.com/repos/rdlabo-dev/cf-workers-native/issues/43",
};
f.github.rest.issues = {
getComment: async ({ comment_id }) => {
assert.equal(comment_id, 123);
return { data: comment };
},
};
f.context.eventName = "workflow_dispatch";
f.context.payload = { inputs: { "comment-id": "123", "head-sha": sha } };
return comment;
}

test("dispatch keeps the original authorized PR head", async () => {
const f = commentFixture();
const original = await resolveRelease(f.github, f.context);
const comment = dispatchComment(f);
assert.deepEqual(await resolveRelease(f.github, f.context), original);
f.state.pr.head.sha = "b".repeat(40);
assert.equal(await isReleaseCurrent(f.github, f.context, original), false);
f.state.pr.head.sha = sha;
comment.updated_at = "2026-10-20";
await assert.rejects(resolveRelease(f.github, f.context), /no longer valid/);
});

test("comment beta requires the artifact from the successful run attempt", async () => {
const f = commentFixture();
f.state.runs[0].run_attempt = 2;
await assert.rejects(
resolveRelease(f.github, f.context),
/candidate is missing or expired/,
);
});
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Compatibility

on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
push:
branches: [main]
merge_group:
Expand Down Expand Up @@ -414,6 +415,54 @@ jobs:
demo/artifacts/upstream/playwright/
retention-days: 7

beta-candidate:
if: github.event_name == 'pull_request' && github.base_ref == 'main' && github.event.pull_request.draft == false
runs-on: ubuntu-24.04
timeout-minutes: 15
permissions:
contents: read
steps:
- name: Read the base package version
id: base-version
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
with:
script: |
const { data: manifest } = await github.rest.repos.getContent({
...context.repo,
path: 'package.json',
ref: context.payload.pull_request.base.sha,
});
const pkg = JSON.parse(Buffer.from(manifest.content, 'base64').toString('utf8'));
const version = pkg.version.split('-')[0];
if (!/^\d+\.\d+\.\d+$/.test(version)) throw new Error('Invalid beta base version');
core.setOutput('version', version);
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 24
package-manager-cache: false
- name: Pack the exact PR candidate
env:
BASE_VERSION: ${{ steps.base-version.outputs.version }}
PR_NUMBER: ${{ github.event.pull_request.number }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
npm install
VERSION="${BASE_VERSION}-beta.pr${PR_NUMBER}.sha${HEAD_SHA:0:12}"
npm version "$VERSION" --no-git-tag-version --ignore-scripts --allow-same-version
npm pkg set publishConfig.tag=beta
npm pack --pack-destination "$RUNNER_TEMP"
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: npm-beta-pr${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.sha }}-${{ github.run_attempt }}
path: ${{ runner.temp }}/cf-workers-native-*.tgz
if-no-files-found: error
retention-days: 7

required:
name: CI
if: ${{ always() }}
Expand All @@ -426,6 +475,7 @@ jobs:
streams-fallback,
package,
upstream,
beta-candidate,
]
runs-on: ubuntu-24.04
timeout-minutes: 1
Expand All @@ -439,9 +489,12 @@ jobs:
BINDINGS: ${{ needs.bindings.result }}
STREAMS: ${{ needs['streams-fallback'].result }}
PACKAGE: ${{ needs.package.result }}
BETA_CANDIDATE: ${{ needs['beta-candidate'].result }}
BETA_EXPECTED: ${{ github.event_name == 'pull_request' && github.base_ref == 'main' && github.event.pull_request.draft == false && 'success' || 'skipped' }}
UPSTREAM: ${{ needs.upstream.result }}
run: |
test "$CHECKOUT" = success
test "$BETA_CANDIDATE" = "$BETA_EXPECTED"
expected=success
if [ "$REUSE" = true ]; then expected=skipped; fi
for result in "$LIBRARY" "$COMPATIBILITY" "$BINDINGS" "$STREAMS" "$PACKAGE" "$UPSTREAM"; do
Expand Down
Loading