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
50 changes: 50 additions & 0 deletions .github/workflows/bump-sentry-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Bump @sentry/api

# Keeps one pull request that pins @sentry/api, which getsentry/sentry releases
# from static/packages/api, to its newest npm release, and moves it to each new
# release. test.yml then checks the CLI, MCP and shared packages against it, so
# this repository validates each release as an outside consumer.
on:
schedule:
- cron: "41 7 * * *"
workflow_dispatch:

permissions:
contents: read

# A manual run and the daily run must not both push the same branch.
concurrency:
group: bump-sentry-api
cancel-in-progress: false

jobs:
bump:
name: Bump @sentry/api
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
# A pull request opened with github.token does not start test.yml, so
# the bump pushes and opens it with the release bot's token.
- name: Get auth token
id: token
uses: actions/create-github-app-token@5d869da34e18e7287c1daad50e0b8ea0f506ce69 # v1.11.0
with:
app-id: ${{ vars.SENTRY_RELEASE_BOT_CLIENT_ID }}
private-key: ${{ secrets.SENTRY_RELEASE_BOT_PRIVATE_KEY }}

- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
token: ${{ steps.token.outputs.token }}
# The bump compares the open PR's branch with main.
fetch-depth: 0

- uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4

- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
with:
node-version: "22.23.4"

- name: Bump @sentry/api
env:
GH_TOKEN: ${{ steps.token.outputs.token }}
run: node scripts/bump-sentry-api.mjs
21 changes: 21 additions & 0 deletions docs/operations/github-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ Runs the label publisher's regression tests when its workflow or tests change.
Covers label replacement, stale revisions, failed classifications, and concurrent
label creation.

### bump-sentry-api.yml

Every day and on manual dispatch, `scripts/bump-sentry-api.mjs` keeps one PR
that pins the catalog's `@sentry/api` to its newest npm release, on the
`bot/bump-sentry-api` branch. It also updates the package's
`minimumReleaseAgeExclude` entry so the fresh release installs. `test.yml` runs
on that PR, so the CLI, MCP and shared packages are checked against each release
that getsentry/sentry publishes. The release bot's token opens the PR, because a
PR opened with `github.token` does not start other workflows.

- With no open bump PR, it opens one.
- When a newer release ships while the PR is open, it rebuilds the branch from
main with that version, force-pushes it, and retitles the PR. It never opens a
second one.
- The branch belongs to the script. If someone pushes their own commit to it,
the script stops updating the PR and logs a warning, so the commit is not
lost. Merging main into the branch, such as with "Update branch", doesn't
count. Rebase it onto the new release by hand, or close it and let the next
run open a fresh PR.
- The PR is never merged automatically.

## Package CI checks

Add a check for one workspace package to that package's `package.json`, not to
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"scripts": {
"docs:check": "node scripts/check-doc-links.mjs",
"test:ci-projects": "node --test scripts/ci-projects.test.mjs scripts/check-workspace-deps.test.mjs scripts/cli-nightly-version.test.mjs scripts/cli-nightly-watchdog.test.mjs scripts/codemod-version-changed.test.mjs scripts/cloudflare-deployment.test.mjs scripts/deploy-workflow.test.mjs scripts/migrate-cloudflare-token.test.mjs scripts/oxc-config.test.mjs scripts/cli-lint-rules.test.mjs scripts/workflow-policy.test.mjs",
"test:ci-projects": "node --test scripts/ci-projects.test.mjs scripts/check-workspace-deps.test.mjs scripts/cli-nightly-version.test.mjs scripts/cli-nightly-watchdog.test.mjs scripts/bump-sentry-api.test.mjs scripts/codemod-version-changed.test.mjs scripts/cloudflare-deployment.test.mjs scripts/deploy-workflow.test.mjs scripts/migrate-cloudflare-token.test.mjs scripts/oxc-config.test.mjs scripts/cli-lint-rules.test.mjs scripts/workflow-policy.test.mjs",
"dev": "pnpm --filter '@sentry/mcp-cloudflare...' --if-present run build && dotenv -e .env -e .env.local -- pnpm --parallel --filter @sentry/mcp-cloudflare --filter @sentry/mcp-core --filter @sentry/mcp-server-mocks --if-present run dev",
"dev:stdio": "pnpm --filter '@sentry/mcp-core...' --if-present run build 1>&2 && dotenv -e .env -e .env.local -- pnpm --silent --dir packages/cli run cli mcp",
"build": "dotenv -e .env -e .env.local -- pnpm -r --filter '!sentry' --filter '!sentry-cli-docs' --if-present run build",
Expand Down
196 changes: 196 additions & 0 deletions scripts/bump-sentry-api.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import { execFileSync } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
import { pathToFileURL } from "node:url";

/**
* Keeps one pull request that pins the workspace catalog's `@sentry/api` to
* its newest npm release, so test.yml checks the CLI, MCP and shared packages
* against each release. getsentry/sentry publishes the package from
* `static/packages/api`.
*
* The PR's branch belongs to this script: each new release rebuilds it from
* main. When someone has pushed their own commit to it, the script leaves the
* branch alone and warns instead, so the commit is not lost.
*/

const PACKAGE = "@sentry/api";
const WORKSPACE = "pnpm-workspace.yaml";
const BRANCH = "bot/bump-sentry-api";
const BOT_NAME = "github-actions[bot]";
const BOT_EMAIL = "41898282+github-actions[bot]@users.noreply.github.com";

const CATALOG_ENTRY = /^(\s+"@sentry\/api":\s*)(\S+)\s*$/m;
const RELEASE_AGE_ENTRY = /^(\s+-\s*"@sentry\/api@)([^"]+)("\s*)$/m;

/**
* Return pnpm-workspace.yaml with `@sentry/api` pinned to `version`, in the
* catalog and in `minimumReleaseAgeExclude`, which lets a fresh release
* install. Throws when either entry is missing.
*/
export function pinSentryApi(workspace, version) {
for (const [pattern, name] of [
[CATALOG_ENTRY, "catalog"],
[RELEASE_AGE_ENTRY, "minimumReleaseAgeExclude"],
]) {
const count = workspace.match(new RegExp(pattern.source, "gm"))?.length;
if (count !== 1) {
throw new Error(
`${WORKSPACE} needs one ${name} entry for ${PACKAGE}, found ${count ?? 0}`,
);
}
}
const catalog = CATALOG_ENTRY.exec(workspace);
return {
current: catalog[2],
workspace: workspace
.replace(CATALOG_ENTRY, (_, key) => `${key}${version}`)
.replace(
RELEASE_AGE_ENTRY,
(_, start, _old, end) => `${start}${version}${end}`,
),
};
}

/** Whether `version` is a stable release, such as 2.5.1. */
export function isRelease(version) {
return /^\d+\.\d+\.\d+$/.test(version);
}

function run(command, args, options = {}) {
return (
execFileSync(command, args, {
encoding: "utf8",
timeout: 600_000,
stdio: ["ignore", "pipe", "inherit"],
...options,
})
// execFileSync returns null when stdout is inherited.
?.trim()
);
}

/**
* What to do about the bump PR. `main` and `branch` are the versions pinned
* on main and on the open bump PR's branch (undefined without an open PR);
* `foreignCommits` counts the branch's commits not made by this script.
*/
export function planBump({ latest, main, branch, foreignCommits = 0 }) {
if (main === latest) return "none";
if (branch === undefined) return "open";
if (branch === latest) return "none";
return foreignCommits > 0 ? "keep" : "update";
}

/** The pinned version in pnpm-workspace.yaml `source`. */
function pinnedVersion(source) {
return pinSentryApi(source, "0.0.0").current;
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

function main() {
const latest = run("npm", ["view", PACKAGE, "version"]);
if (!isRelease(latest)) {
throw new Error(`Expected a release version of ${PACKAGE}, got: ${latest}`);
}
const { current, workspace } = pinSentryApi(
readFileSync(WORKSPACE, "utf8"),
latest,
);

const pr = run("gh", [
"pr",
"list",
"--head",
BRANCH,
"--state",
"open",
"--json",
"number",
"--jq",
".[0].number // empty",
]);
// The remote branch's commit, or empty when it does not exist. Pushing with
// it as the lease fails if the branch moves while this runs.
const remote = run("git", [
"ls-remote",
"origin",
`refs/heads/${BRANCH}`,
]).split(/\s/)[0];
let branch;
let foreignCommits = 0;
if (pr && remote) {
run("git", ["fetch", "--quiet", "origin", BRANCH, "main"]);
branch = pinnedVersion(run("git", ["show", `${remote}:${WORKSPACE}`]));
// Merging main into the branch, such as with GitHub's "Update branch",
// adds nothing the rebuild from main would lose, so merges don't count.
foreignCommits = run("git", [
"log",
"--no-merges",
"--format=%ae",
`origin/main..${remote}`,
])
.split("\n")
.filter((email) => email && email !== BOT_EMAIL).length;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}

const plan = planBump({ latest, main: current, branch, foreignCommits });
if (plan === "none") {
console.log(
`${PACKAGE} ${latest} is already pinned${branch ? ` in #${pr}` : ""}.`,
);
return;
}
if (plan === "keep") {
console.log(
`::warning::#${pr} has commits not made by this script, so it stays on ${PACKAGE} ${branch}. Rebase it onto ${latest} by hand, or close it to let the next run open a new one.`,
);
return;
}

console.log(`Pinning ${PACKAGE} ${latest} (main has ${current}).`);
writeFileSync(WORKSPACE, workspace);
run("pnpm", ["install", "--lockfile-only", "--ignore-scripts"], {
stdio: ["ignore", "inherit", "inherit"],
});
Comment thread
sentry-warden[bot] marked this conversation as resolved.
Resolved

const title = `chore(deps): Bump ${PACKAGE} to ${latest}`;
const body = `Pins ${PACKAGE} to [${latest}](https://github.com/getsentry/sentry/releases/tag/api@${latest}), so CI checks this repository against the release. \`bump-sentry-api.yml\` updates this PR when a newer release ships and never merges it.`;
run("git", ["checkout", "-B", BRANCH]);
run("git", ["add", WORKSPACE, "pnpm-lock.yaml"]);
run("git", ["commit", "-m", title], {
env: {
...process.env,
GIT_AUTHOR_NAME: BOT_NAME,
GIT_AUTHOR_EMAIL: BOT_EMAIL,
GIT_COMMITTER_NAME: BOT_NAME,
GIT_COMMITTER_EMAIL: BOT_EMAIL,
},
});
// A leftover branch from a closed PR is overwritten too.
run("git", [
"push",
`--force-with-lease=refs/heads/${BRANCH}:${remote}`,
"origin",
`HEAD:refs/heads/${BRANCH}`,
]);
if (plan === "update") {
run("gh", ["pr", "edit", pr, "--title", title, "--body", body]);
} else {
run("gh", [
"pr",
"create",
"--head",
BRANCH,
"--title",
title,
"--body",
body,
]);
}
}

if (
process.argv[1] &&
import.meta.url === pathToFileURL(process.argv[1]).href
) {
main();
}
47 changes: 47 additions & 0 deletions scripts/bump-sentry-api.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import { isRelease, pinSentryApi, planBump } from "./bump-sentry-api.mjs";

const workspace = readFileSync(
new URL("../pnpm-workspace.yaml", import.meta.url),
"utf8",
);

test("pins the catalog and the release-age exclusion together", () => {
const { current, workspace: pinned } = pinSentryApi(workspace, "9.8.7");
assert.match(current, /^\d+\.\d+\.\d+$/);
assert.match(pinned, /^ {2}"@sentry\/api": 9\.8\.7$/m);
assert.match(pinned, /^ {2}- "@sentry\/api@9\.8\.7"$/m);
assert.equal(pinned.split("9.8.7").length - 1, 2);
// Nothing else changes.
assert.equal(pinned.replaceAll("9.8.7", current), workspace);
});

test("fails when an entry is missing or repeated", () => {
const withoutExclusion = workspace.replace(/^ +- "@sentry\/api@.*\n/m, "");
assert.throws(
() => pinSentryApi(withoutExclusion, "9.8.7"),
/minimumReleaseAgeExclude/,
);
const repeated = workspace.replace(/^( +"@sentry\/api": .*\n)/m, "$1$1");
assert.throws(() => pinSentryApi(repeated, "9.8.7"), /found 2/);
});

test("accepts only stable releases", () => {
assert.equal(isRelease("2.5.1"), true);
assert.equal(isRelease("3.0.0-rc.0"), false);
assert.equal(isRelease(""), false);
});

test("keeps one bump PR on the newest release", () => {
const latest = "2.6.0";
assert.equal(planBump({ latest, main: "2.6.0" }), "none");
assert.equal(planBump({ latest, main: "2.5.0" }), "open");
assert.equal(planBump({ latest, main: "2.5.0", branch: "2.6.0" }), "none");
assert.equal(planBump({ latest, main: "2.5.0", branch: "2.5.3" }), "update");
assert.equal(
planBump({ latest, main: "2.5.0", branch: "2.5.3", foreignCommits: 1 }),
"keep",
);
});
Loading