Repository navigation
ci: Keep one rolling PR on the newest @sentry/api release #2291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5f1394a
ci: Open a PR for each new @sentry/api release
sentry-junior[bot] a2f034d
fix(ci): Keep open @sentry/api bump PRs and survive inherited stdout
sentry-junior[bot] 84d4d7a
ci: Keep one rolling PR on the newest @sentry/api release
sentry-junior[bot] ea60d58
fix(ci): Don't count merges from main as hand-made bump commits
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| 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; | ||
|
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"], | ||
| }); | ||
|
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(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.