diff --git a/.github/workflows/regenerate-platform-setup.yml b/.github/workflows/regenerate-platform-setup.yml new file mode 100644 index 000000000..3baa99ea2 --- /dev/null +++ b/.github/workflows/regenerate-platform-setup.yml @@ -0,0 +1,48 @@ +name: Regenerate platform setup data + +# Regenerates the create_project platform setup data from Sentry's platform +# list and the published docs tree, and keeps one PR with any changes on the +# bot/regenerate-platform-setup branch. scripts/platform-setup-pr.mjs decides +# whether to open, update, or leave that PR alone. +on: + schedule: + - cron: "23 8 * * *" + workflow_dispatch: + +permissions: + contents: read + +# A dispatch and the daily run must not both push the same branch. +concurrency: + group: regenerate-platform-setup + cancel-in-progress: false + +jobs: + regenerate: + name: Regenerate platform setup data + if: github.repository == 'getsentry/toolkit' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + # The release bot pushes and opens the PR, because a PR opened with + # github.token does not start test.yml. + - 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 }} + - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version: "22.23.4" + cache: pnpm + - run: pnpm install --frozen-lockfile + - run: pnpm --filter @sentry/mcp-core run generate-platform-setup + - name: Open or update the regeneration PR + env: + GH_TOKEN: ${{ steps.token.outputs.token }} + run: node scripts/platform-setup-pr.mjs diff --git a/docs/operations/github-actions.md b/docs/operations/github-actions.md index 7eca589f4..4ee807866 100644 --- a/docs/operations/github-actions.md +++ b/docs/operations/github-actions.md @@ -137,6 +137,23 @@ workflows. a fresh PR. - The PR is never merged automatically. +### regenerate-platform-setup.yml + +Keeps one PR with the regenerated `create_project` platform setup data, on the +`bot/regenerate-platform-setup` branch. It runs daily and on manual dispatch +from `main`, runs `pnpm --filter @sentry/mcp-core run generate-platform-setup`, +and hands the result to `scripts/platform-setup-pr.mjs`. The release bot's +token pushes the branch and opens the PR, so `test.yml` runs on it. + +- When the generated data matches `main`, or matches the open PR, it does + nothing. +- With no open PR, it commits the data on a branch from `main`, force-pushes + it, and opens a PR. A leftover branch from a closed PR is overwritten. +- When the open PR has older data, it rebuilds the branch from `main` and + force-pushes it. It never opens a second PR, and commits pushed to the + branch by hand are lost. +- The PR is never merged automatically. + ## Package CI checks Add a check for one workspace package to that package's `package.json`, not to diff --git a/package.json b/package.json index 63441ae69..01dca3993 100644 --- a/package.json +++ b/package.json @@ -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/platform-setup-pr.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", diff --git a/packages/mcp-core/scripts/generate-platform-setup.ts b/packages/mcp-core/scripts/generate-platform-setup.ts index 0f4b81d90..ddf6b4512 100644 --- a/packages/mcp-core/scripts/generate-platform-setup.ts +++ b/packages/mcp-core/scripts/generate-platform-setup.ts @@ -14,8 +14,9 @@ * Usage (from packages/mcp-core): * pnpm run generate-platform-setup * - * The output is committed. Rerun this when Sentry adds a platform or the docs - * change which signals a platform supports. + * The output is committed. The regenerate-platform-setup.yml workflow reruns + * this daily and opens a PR when Sentry adds a platform or the docs change + * which signals a platform supports. */ import { writeFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; diff --git a/scripts/platform-setup-pr.mjs b/scripts/platform-setup-pr.mjs new file mode 100644 index 000000000..725aa2b7b --- /dev/null +++ b/scripts/platform-setup-pr.mjs @@ -0,0 +1,118 @@ +import { execFileSync } from "node:child_process"; +import { readFileSync } from "node:fs"; +import { pathToFileURL } from "node:url"; + +/** + * Keeps one pull request with the regenerated `create_project` platform setup + * data, for `regenerate-platform-setup.yml`. Run it from a checkout of the + * default branch after `pnpm --filter @sentry/mcp-core run + * generate-platform-setup`. Each change rebuilds the PR's branch from the + * default branch and force-pushes it. + */ + +const DATA_FILE = + "packages/mcp-core/src/tools/support/project-setup/platforms.json"; +const BRANCH = "bot/regenerate-platform-setup"; +const TITLE = "chore(mcp-core): Regenerate create_project platform setup data"; +const BODY = `Regenerates \`create_project\` platform setup data from Sentry's platform list and the published docs tree with \`pnpm --filter @sentry/mcp-core run generate-platform-setup\`. + +The \`regenerate-platform-setup.yml\` workflow keeps this PR. Each run that finds new changes rebuilds this branch from main and force-pushes it, so commits pushed here by hand are lost.`; +const BOT_NAME = "github-actions[bot]"; +const BOT_EMAIL = "41898282+github-actions[bot]@users.noreply.github.com"; + +/** + * What to do about the regeneration PR. `generated` is the freshly generated + * data, `base` the data on the default branch, and `branch` the data on the + * open PR's branch (undefined without an open PR). + */ +export function planRegeneration({ generated, base, branch }) { + if (generated === base) return "none"; + if (branch === undefined) return "open"; + if (branch === generated) return "none"; + return "update"; +} + +function run(command, args, env = process.env) { + return execFileSync(command, args, { + encoding: "utf8", + env, + stdio: ["ignore", "pipe", "inherit"], + }); +} + +function main() { + const generated = readFileSync(DATA_FILE, "utf8"); + const base = run("git", ["show", `HEAD:${DATA_FILE}`]); + const pr = run("gh", [ + "pr", + "list", + "--head", + BRANCH, + "--state", + "open", + "--json", + "number", + "--jq", + ".[0].number // empty", + ]).trim(); + // 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; + if (pr && remote) { + run("git", ["fetch", "--quiet", "--depth=1", "origin", remote]); + branch = run("git", ["show", `${remote}:${DATA_FILE}`]); + } + + const plan = planRegeneration({ generated, base, branch }); + if (plan === "none") { + console.log( + `Platform setup data is unchanged${branch === undefined ? "" : ` from #${pr}`}.`, + ); + return; + } + + run("git", ["checkout", "-B", BRANCH]); + run("git", ["add", DATA_FILE]); + run("git", ["commit", "--message", TITLE], { + ...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") { + console.log(`Updated #${pr} with new platform setup data.`); + return; + } + const url = run("gh", [ + "pr", + "create", + "--head", + BRANCH, + "--title", + TITLE, + "--body", + BODY, + ]).trim(); + console.log(`Opened ${url} with new platform setup data.`); +} + +if ( + process.argv[1] && + import.meta.url === pathToFileURL(process.argv[1]).href +) { + main(); +} diff --git a/scripts/platform-setup-pr.test.mjs b/scripts/platform-setup-pr.test.mjs new file mode 100644 index 000000000..eca92e34e --- /dev/null +++ b/scripts/platform-setup-pr.test.mjs @@ -0,0 +1,35 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { planRegeneration } from "./platform-setup-pr.mjs"; + +test("does nothing when the default branch already has the data", () => { + assert.equal( + planRegeneration({ generated: "a", base: "a", branch: undefined }), + "none", + ); + assert.equal( + planRegeneration({ generated: "a", base: "a", branch: "b" }), + "none", + ); +}); + +test("opens a PR for new data when none is open", () => { + assert.equal( + planRegeneration({ generated: "b", base: "a", branch: undefined }), + "open", + ); +}); + +test("leaves the open PR alone when it already has the data", () => { + assert.equal( + planRegeneration({ generated: "b", base: "a", branch: "b" }), + "none", + ); +}); + +test("updates the open PR when the data changed again", () => { + assert.equal( + planRegeneration({ generated: "c", base: "a", branch: "b" }), + "update", + ); +});