diff --git a/.github/workflows/check-dev-build-with-cloud-deploy-action.yml b/.github/workflows/check-dev-build-with-cloud-deploy-action.yml new file mode 100644 index 00000000..ea6d0b94 --- /dev/null +++ b/.github/workflows/check-dev-build-with-cloud-deploy-action.yml @@ -0,0 +1,130 @@ +name: cloud-deploy-action can drive the dev build + +# An early warning, not a blocker: the dev build must stay published so +# prisma/cloud-deploy-action can be fixed against it. The Test check on +# pull requests runs the same test and blocks the release instead. +# See docs/oss/release-automation.md. + +on: + workflow_run: + workflows: ["Publish to npm"] + types: [completed] + branches: [main] + +permissions: {} + +jobs: + cloud-deploy-action-drives-dev-build: + name: prisma/cloud-deploy-action can drive the dev build + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + env: + ACTION_REF: ${{ vars.CLOUD_DEPLOY_ACTION_REF || 'v1' }} + + steps: + # Checked on every run so a missing secret shows up at the first + # publish, not at the first incompatibility. + - name: Check that SLACK_WEBHOOK_URL is set + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + run: | + if [ -z "$SLACK_WEBHOOK_URL" ]; then + echo "::error::The SLACK_WEBHOOK_URL secret is not set, so this check cannot report a failure." + exit 1 + fi + + # publish.yml uploads this artifact only after it really published. + - name: Find the published dev version + id: dev + env: + GH_TOKEN: ${{ github.token }} + PUBLISH_RUN_ID: ${{ github.event.workflow_run.id }} + run: | + artifact=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$PUBLISH_RUN_ID/artifacts" \ + --jq '.artifacts[] | select(.name == "published-dev-version") | .id') + if [ -z "$artifact" ]; then + published=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$PUBLISH_RUN_ID/jobs" \ + --jq '[.jobs[].steps[] | select(.name == "Publish the dev version")] | first | .conclusion') + if [ "$published" = success ]; then + echo "::error::Publish run $PUBLISH_RUN_ID published a dev build but has no published-dev-version artifact." + exit 1 + fi + echo "Publish run $PUBLISH_RUN_ID published no dev build (its publish step: ${published:-absent}). Nothing to check." + exit 0 + fi + gh run download "$PUBLISH_RUN_ID" --repo "$GITHUB_REPOSITORY" --name published-dev-version --dir "$RUNNER_TEMP/published-dev-version" + version=$(cat "$RUNNER_TEMP/published-dev-version/version") + if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+-[0-9A-Za-z.-]+$ ]]; then + echo "::error::The published-dev-version artifact holds \"$version\", which is not a version." + exit 1 + fi + echo "version=$version" >> "$GITHUB_OUTPUT" + + - name: Checkout + if: ${{ steps.dev.outputs.version != '' }} + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + with: + persist-credentials: false + + - name: Set up Node.js + if: ${{ steps.dev.outputs.version != '' }} + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 + with: + node-version-file: .node-version + + - name: Install prisma@${{ steps.dev.outputs.version }} + if: ${{ steps.dev.outputs.version != '' }} + env: + VERSION: ${{ steps.dev.outputs.version }} + run: npm install --prefix "$RUNNER_TEMP/prisma-dev" --no-save --no-package-lock "prisma@$VERSION" + + - name: Check out prisma/cloud-deploy-action + if: ${{ steps.dev.outputs.version != '' }} + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + with: + repository: prisma/cloud-deploy-action + ref: ${{ env.ACTION_REF }} + path: .reference/cloud-deploy-action + persist-credentials: false + + - name: prisma/cloud-deploy-action can drive prisma@${{ steps.dev.outputs.version }} + id: drive + if: ${{ steps.dev.outputs.version != '' }} + run: | + code=0 + node scripts/check-cloud-deploy-action.mjs .reference/cloud-deploy-action "$RUNNER_TEMP/prisma-dev/node_modules/.bin/prisma" || code=$? + echo "exit-code=$code" >> "$GITHUB_OUTPUT" + exit "$code" + + - name: Alert Slack + if: ${{ failure() }} + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + DRIVE_EXIT_CODE: ${{ steps.drive.outputs.exit-code }} + VERSION: ${{ steps.dev.outputs.version }} + PUBLISH_RUN_URL: ${{ github.event.workflow_run.html_url }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + if [ -z "$SLACK_WEBHOOK_URL" ]; then + echo "::error::The SLACK_WEBHOOK_URL secret is not set, so this failure was not reported." + exit 1 + fi + # Exit codes of scripts/check-cloud-deploy-action.mjs. + case "$DRIVE_EXIT_CODE" in + 1) + text="The dev build prisma@$VERSION fails prisma/cloud-deploy-action's test: the released action ($ACTION_REF) cannot drive it. The fix is a prisma/cloud-deploy-action release whose rule covers these dev builds and the next CLI version. Until then the next prisma CLI release is blocked, and this alert repeats on each merge to prisma-cli's main. See docs/oss/release-automation.md#changing-the-deploy-arguments-on-purpose." + ;; + 2) + text="The check that prisma/cloud-deploy-action can drive prisma@$VERSION could not run, so the dev build was not tested: the action's test file or the installed prisma executable is missing, or the action's deploy cases did not run. The run log says which." + ;; + *) + text="The check that prisma/cloud-deploy-action can drive the dev build could not run, so the dev build was not tested: the published dev version artifact is missing or unreadable, npm install failed, or a checkout failed. Publish run: $PUBLISH_RUN_URL" + ;; + esac + payload=$(jq -n --arg text "$text" --arg run "$RUN_URL" '{text: "\($text)\nRun: \($run)"}') + curl --fail-with-body --silent --show-error \ + -H "Content-Type: application/json" \ + --data "$payload" \ + "$SLACK_WEBHOOK_URL" diff --git a/.github/workflows/pr-quality.yml b/.github/workflows/pr-quality.yml index c2fd9efb..00fe8f15 100644 --- a/.github/workflows/pr-quality.yml +++ b/.github/workflows/pr-quality.yml @@ -182,3 +182,21 @@ jobs: env: PUBLISH_CHANNEL: dev run: pnpm check:conformance + + # prisma/cloud-deploy-action runs `prisma deploy` with fixed + # arguments. `v1` is the moving tag users run, so a CLI change that + # breaks those arguments fails here, before it can be released. See + # docs/oss/release-automation.md for CLOUD_DEPLOY_ACTION_REF. + - name: Build the prisma package + run: pnpm exec turbo run build --filter=prisma + + - name: Check out prisma/cloud-deploy-action + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + with: + repository: prisma/cloud-deploy-action + ref: ${{ vars.CLOUD_DEPLOY_ACTION_REF || 'v1' }} + path: .reference/cloud-deploy-action + persist-credentials: false + + - name: prisma/cloud-deploy-action can drive this CLI + run: node scripts/check-cloud-deploy-action.mjs .reference/cloud-deploy-action packages/prisma/dist/prisma.js diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index b06460c8..c813f864 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,4 +1,4 @@ -name: Publish to npm +name: Publish to npm # check-dev-build-with-cloud-deploy-action.yml finds this workflow by this name # Every run publishes a dev build. A run also publishes a release when # the push changed the root `version`, or a dispatch asked for one. The @@ -104,6 +104,7 @@ jobs: NPM_CONFIG_PROVENANCE: "true" run: bash scripts/publish-packages.sh latest @prisma/cli-engine + # check-dev-build-with-cloud-deploy-action.yml reads this step's result by its name. - name: Publish the dev version if: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.dry-run != 'true' }} env: @@ -121,6 +122,30 @@ jobs: "@prisma/cli@$DEV_VERSION" \ "prisma@$DEV_VERSION" + # Read by check-dev-build-with-cloud-deploy-action.yml, which runs as + # its own workflow so its result never holds or fails this one. + - name: Record the published dev version + if: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.dry-run != 'true' }} + continue-on-error: true + timeout-minutes: 2 + env: + DEV_VERSION: ${{ steps.version.outputs.devVersion }} + run: | + mkdir -p "$RUNNER_TEMP/dev-version" + printf '%s\n' "$DEV_VERSION" > "$RUNNER_TEMP/dev-version/version" + + - name: Upload the published dev version + if: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.dry-run != 'true' }} + continue-on-error: true + timeout-minutes: 2 + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: published-dev-version + path: ${{ runner.temp }}/dev-version/version + if-no-files-found: error + overwrite: true + retention-days: 7 + # --- The release: only when the committed version changed. --- - name: Restore the committed versions diff --git a/docs/oss/release-automation.md b/docs/oss/release-automation.md index 6675eccb..81bc3869 100644 --- a/docs/oss/release-automation.md +++ b/docs/oss/release-automation.md @@ -8,6 +8,8 @@ The unified CLI is assembled from three repositories. This page explains how a n | `prisma/composer` | `@prisma/composer-cli` (and the `@prisma/composer` library) | a dependency of the shell | | `prisma/prisma` | `@prisma/orm-toolchain` | a dependency of the shell | +One more repository depends on this CLI instead of feeding it: `prisma/cloud-deploy-action`, whose test this repository runs (see [Keeping `prisma/cloud-deploy-action` working](#keeping-prismacloud-deploy-action-working)). + ## The rule A **release** of the CLI depends only on **released** versions of the products. A **dev build** of the CLI depends on the products' **dev builds** (operator ruling 2026-08-17). Nothing in between, and nothing hand-maintained. @@ -15,7 +17,7 @@ A **release** of the CLI depends only on **released** versions of the products. ## What happens when a product publishes 1. The product repository's publish workflow finishes and sends a `repository_dispatch` event of type `product-published` to `prisma/prisma-cli`. -2. [`update-product-versions.yml`](../../.github/workflows/update-product-versions.yml) runs [`scripts/update-product-versions.mjs`](../../scripts/update-product-versions.mjs), which asks the registry for each watched package's version at its **release** dist-tag and rewrites `packages/cli/package.json` and `packages/prisma/package.json` where they differ. Every watched package publishes its releases under `latest`, pre-release versions included: they are all new packages with no pre-8 audience to protect. Holding `latest` back behind `next` matters only for the bare `prisma` name, which this repository publishes and which still serves Prisma 7 there until the deliberate cutover. Each package carries a list of candidate tags rather than one name, most-preferred first, so a product moving where it publishes does not silently freeze the CLI on an old version — and a package that publishes none of its candidates fails the run instead of looking like "nothing to update". +2. [`update-product-versions.yml`](../../.github/workflows/update-product-versions.yml) runs [`scripts/update-product-versions.mjs`](../../scripts/update-product-versions.mjs), which asks the registry for each watched package's version at its **release** dist-tag and rewrites `packages/cli/package.json` and `packages/prisma/package.json` where they differ. Every watched package publishes its releases under `latest`, pre-release versions included: they are all new packages with no pre-8 audience to protect. The bare `prisma` name, which this repository publishes, was the one exception: its `latest` served Prisma 7 until the 2026-08-25 cutover, and since then every release of it publishes under `latest` too (see [versioning.md](versioning.md)). Each package carries a list of candidate tags rather than one name, most-preferred first, so a product moving where it publishes does not silently freeze the CLI on an old version — and a package that publishes none of its candidates fails the run instead of looking like "nothing to update". 3. If any pin changed, or `main` holds pins the last release (its `v` tag) did not ship, the workflow bumps the CLI to its next version with `pnpm bump-version` (which also refreshes `pnpm-lock.yaml`), opens a pull request titled `Release : follow the products' released versions: ...`, and arms auto-merge. Every earlier open pull request from this workflow, and every open `Bump to version` release pull request, is closed first: each bumps the same version, so only one can merge. 4. The pull request runs the full quality and conformance checks. When they pass and CodeRabbit approves it (the `main` ruleset requires one approving review), it merges, and that push to `main` changes the root version, so [`publish.yml`](../../.github/workflows/publish.yml) publishes the release under `latest` with the committed product versions, plus the usual `dev` build. @@ -23,6 +25,39 @@ A product release reaches a released CLI with no person in the loop, as long as A **daily schedule** runs the same comparison, so a lost dispatch event delays an update by at most a day rather than indefinitely. `workflow_dispatch` runs it on demand. +## Keeping `prisma/cloud-deploy-action` working + +[`prisma/cloud-deploy-action`](https://github.com/prisma/cloud-deploy-action) runs `prisma deploy` with arguments it builds itself. When `prisma@8.0.0-rc.30` renamed `--stage` to `--environment`, every user's preview deploy failed for a week, because nothing here ran the action's arguments. Two checks now run the action's own test against this CLI, both through [`scripts/check-cloud-deploy-action.mjs`](../../scripts/check-cloud-deploy-action.mjs): + +- **The pull request check.** The required `Test` check in [`pr-quality.yml`](../../.github/workflows/pr-quality.yml) builds `prisma` from the pull request and runs the test against it. That CLI uses the products' released versions, which is what the next release ships. A CLI the released action cannot drive cannot merge, so it cannot be released. +- **The dev-build check.** [`check-dev-build-with-cloud-deploy-action.yml`](../../.github/workflows/check-dev-build-with-cloud-deploy-action.yml) runs after each publish from `main`, installs the dev build that run published, and runs the test against it. That CLI uses the products' dev builds, which is what a later release will ship. It runs at the next merge to this repository's `main` after a product publishes a dev build, not at the moment the product publishes. It never holds or fails the publish. On any failure it posts to Slack through the `SLACK_WEBHOOK_URL` secret. The message comes from the script's exit code: 1 means the action's test ran and the action cannot drive the dev build; 2, or a failure in an earlier step, means the check itself could not run. Every run checks the secret in its first step and fails if it is not set. + +The pull request check is part of the `Test` job because that check is already required on `main`. It should move to its own required job the next time the `main` ruleset is changed. + +### What this repository relies on in the action + +- The test is `tests/prisma-cli.test.mjs`, and it needs no install: it imports only `../cli.mjs` and Node built-ins. +- The test reads the executable to test from the `PRISMA_CLI_BINS` environment variable. +- The test reads the CLI's version from the output of `prisma --version`, which must start with the version number. +- The script finds the two deploy cases by their titles (`... accepts the deploy arguments for production` and for a named environment) and exits 2 unless both ran. Without an executable the test runs no cases and passes, so a renamed variable or renamed titles would otherwise go unnoticed. Any failed case makes it exit 1. The destroy case may be skipped, because CLIs without a `destroy` command skip it. +- The test reads this CLI's error codes: `CLI.INVALID_ARGUMENTS`, `CLI.COMMAND_MOVED` or `CLI.UNKNOWN_COMMAND` for rejected arguments, and `CLI.CREDENTIALS_REQUIRED` or `CLI.CONFIG_SECTION_INVALID` as the first check after the arguments are parsed. + +Both checks use the action at `v1`, its moving release tag, which is what users run. The repository variable `CLOUD_DEPLOY_ACTION_REF` replaces `v1` in both. + +### Changing the `deploy` arguments on purpose + +The action chooses the arguments from the CLI's version number. It does not ask the CLI. For example, `takesEnvironmentFlag` in its `cli.mjs` sends `--environment` to 8.0.0-rc.30 and later, and `--stage` to earlier versions. A dev build carries the version of the last release plus `-dev.N` (for example `8.0.0-rc.34-dev.812`), and the action's current rule treats it as that release. So the action cannot tell a dev build with changed arguments from the unchanged release with the same base version. + +When a product changes the arguments: + +1. The product publishes a dev build with the change. At the next merge to `main` here, the CLI's dev build picks it up, and the dev-build check fails and alerts Slack. +2. The dev-build check stays red, and alerts again on each merge to `main`, until an action release has a rule that covers those dev builds. +3. The product releases the change, and the release pull request bumps the CLI to a new version. It passes only once the action is released, and `v1` moved, with a rule that sends the new arguments to that version (for example "rc.35 and later"). Then re-run its `Test` check. + +When the change is made in this repository itself, the pull request's CLI reports the last release's version, so the action sends it the old arguments and the check fails. Release the action first with a rule for the next version, and bump the version (`pnpm bump-version`) in the same pull request. + +Making the action detect the arguments without relying on the version number is tracked in [prisma/cloud-deploy-action#27](https://github.com/prisma/cloud-deploy-action/issues/27). + ## The shared secret: `DEPLOY_GITHUB_TOKEN` One fine-grained personal access token, stored under the same secret name in all three repositories. @@ -76,6 +111,9 @@ A workflow step that must run should fail when its secret or token is missing, n | Checks pass but the pull request does not merge | The `main` ruleset requires one approving review, which CodeRabbit gives; it commented or was rate-limited instead | Approve the pull request by hand | | `main` holds product pins no release shipped | A release took the version number before the follow pull request merged | The next run (the daily schedule at the latest) compares `main`'s pins with the last release's tag and opens a release for them; dispatch the workflow to do it now | | A published version never appears on the registry | npm accepted the publish but the version is not resolvable | The publish run polls for it and fails if it never appears; if that fails, re-dispatch the workflow — an already-published version is treated as done, so only the missing one publishes | +| A pull request fails "prisma/cloud-deploy-action can drive this CLI" | The pull request changes the arguments `prisma deploy` accepts, and the released action does not send the new ones to this version | Release the action with a rule for this version and move its `v1` tag, then re-run the check (see [Changing the `deploy` arguments on purpose](#changing-the-deploy-arguments-on-purpose)) | +| Every pull request fails "prisma/cloud-deploy-action can drive this CLI" or "Check out prisma/cloud-deploy-action" | The action's `v1` is broken, or GitHub cannot fetch `prisma/cloud-deploy-action` | Set the repository variable `CLOUD_DEPLOY_ACTION_REF` to a known-good tag of the action and re-run; delete the variable once `v1` works again. If that does not help, an administrator can bypass the `main` ruleset to merge an urgent fix | +| The dev-build check fails at "Check that SLACK_WEBHOOK_URL is set" | The secret is absent from this repository | Add a Slack incoming-webhook URL as the `SLACK_WEBHOOK_URL` repository secret | | A release publish fails on the dev-build check | The committed pins are dev builds — a dev stamp was committed by mistake, or a product has no usable release | Run `node scripts/update-product-versions.mjs --channel release`; if that changes nothing, the product must publish a real release | ## The state this replaced diff --git a/package.json b/package.json index c38ddcaa..f303c854 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "lint:fix": "biome check . --write", "bump-version": "node scripts/bump-version.ts", "test": "turbo run test", - "test:scripts": "node --test scripts/determine-version-utils.test.ts scripts/set-version-utils.test.ts scripts/bump-cli-engine-version-utils.test.ts scripts/resolve-package-version.test.mjs scripts/update-product-versions.test.mjs scripts/verify-published.test.mjs scripts/node-engines.test.mjs", + "test:scripts": "node --test scripts/determine-version-utils.test.ts scripts/set-version-utils.test.ts scripts/bump-cli-engine-version-utils.test.ts scripts/resolve-package-version.test.mjs scripts/update-product-versions.test.mjs scripts/verify-published.test.mjs scripts/node-engines.test.mjs scripts/check-cloud-deploy-action.test.mjs", "typecheck": "turbo run typecheck", "prisma-cli": "tsx packages/cli/src/bin.ts", "prisma": "tsx packages/cli/src/bin.ts", diff --git a/scripts/check-cloud-deploy-action-utils.mjs b/scripts/check-cloud-deploy-action-utils.mjs new file mode 100644 index 00000000..1735c031 --- /dev/null +++ b/scripts/check-cloud-deploy-action-utils.mjs @@ -0,0 +1,57 @@ +const DEPLOY_CASE = /accepts the deploy arguments for (.+)$/; + +/** The test ran and the action cannot drive the CLI. */ +export const EXIT_INCOMPATIBLE = 1; +/** The check could not run, so it says nothing about the CLI. */ +export const EXIT_NOT_RUN = 2; + +/** + * Judges a finished run of the action's test. `incompatible` means a case + * failed; `not-run` means a required deploy case is missing from the run. + * + * @param {readonly { name: string, outcome: "pass" | "fail" | "skip" }[]} results + * @returns {{ verdict: "pass" | "incompatible" | "not-run", problems: string[] }} + */ +export function judgeRun(results) { + const problems = results + .filter((result) => result.outcome === "fail") + .map((result) => `Failed: ${result.name}`); + const ranDeployTargets = results + .filter((result) => result.outcome !== "skip") + .map((result) => DEPLOY_CASE.exec(result.name)?.[1]) + .filter((target) => target !== undefined); + const missing = []; + if (!ranDeployTargets.includes("production")) { + missing.push('"... accepts the deploy arguments for production"'); + } + if (!ranDeployTargets.some((target) => target !== "production")) { + missing.push( + '"... accepts the deploy arguments for ", with an environment other than production', + ); + } + for (const title of missing) { + problems.push( + `No test titled ${title} ran. This script finds the action's deploy cases by these titles, so the action's test titles may have changed. See docs/oss/release-automation.md.`, + ); + } + if (missing.length > 0) return { verdict: "not-run", problems }; + if (problems.length > 0) return { verdict: "incompatible", problems }; + return { verdict: "pass", problems }; +} + +/** + * A crash of the check is not a verdict on the CLI, so it must not exit + * with Node's default 1, which means "incompatible" here. + * + * @param {() => Promise} main + */ +export async function exitCodeOf(main) { + try { + return await main(); + } catch (error) { + console.error( + `::error::The check could not run: ${error instanceof Error ? error.stack : error}`, + ); + return EXIT_NOT_RUN; + } +} diff --git a/scripts/check-cloud-deploy-action.mjs b/scripts/check-cloud-deploy-action.mjs new file mode 100644 index 00000000..322133ea --- /dev/null +++ b/scripts/check-cloud-deploy-action.mjs @@ -0,0 +1,94 @@ +#!/usr/bin/env node + +// Runs prisma/cloud-deploy-action's own CLI test against one `prisma` +// executable. The test passes with zero cases when it gets no executable +// (for example after a rename of PRISMA_CLI_BINS in the action), so this +// also requires the deploy cases to have run. +// +// Exit codes: 0 the action can drive the executable, 1 it cannot, +// 2 the check could not run. +// +// Usage: node scripts/check-cloud-deploy-action.mjs + +import { accessSync, constants, existsSync, statSync } from "node:fs"; +import { join, resolve } from "node:path"; +import { run } from "node:test"; +import { spec } from "node:test/reporters"; +import { + EXIT_INCOMPATIBLE, + EXIT_NOT_RUN, + exitCodeOf, + judgeRun, +} from "./check-cloud-deploy-action-utils.mjs"; + +const TEST_FILE = "tests/prisma-cli.test.mjs"; + +function isExecutableFile(path) { + try { + accessSync(path, constants.X_OK); + return statSync(path).isFile(); + } catch { + return false; + } +} + +async function main() { + const [actionDir, bin] = process.argv.slice(2); + if (!(actionDir && bin)) { + console.error( + "Usage: node scripts/check-cloud-deploy-action.mjs ", + ); + return EXIT_NOT_RUN; + } + const testFile = resolve(actionDir, TEST_FILE); + if (!existsSync(testFile)) { + console.error( + `::error::${join(actionDir, TEST_FILE)} does not exist. prisma/cloud-deploy-action must keep its CLI test at this path. The check could not run.`, + ); + return EXIT_NOT_RUN; + } + const executable = resolve(bin); + if (!isExecutableFile(executable)) { + console.error( + `::error::The prisma executable ${executable} is missing or not executable. The check could not run.`, + ); + return EXIT_NOT_RUN; + } + + process.env.PRISMA_CLI_BINS = executable; + /** @type {{ name: string, outcome: "pass" | "fail" | "skip" }[]} */ + const results = []; + const stream = run({ files: [testFile], cwd: resolve(actionDir) }); + stream.on("test:pass", ({ name, skip }) => { + results.push({ name, outcome: skip ? "skip" : "pass" }); + }); + stream.on("test:fail", ({ name }) => { + results.push({ name, outcome: "fail" }); + }); + for await (const chunk of stream.compose(spec)) { + process.stdout.write(chunk); + } + + const { verdict, problems } = judgeRun(results); + for (const problem of problems) { + console.error(`::error::${problem}`); + } + if (verdict === "not-run") { + console.error( + `::error::The check could not run: prisma/cloud-deploy-action's deploy cases did not run against ${executable}.`, + ); + return EXIT_NOT_RUN; + } + if (verdict === "incompatible") { + console.error( + `::error::prisma/cloud-deploy-action cannot drive ${executable}: its test failed.`, + ); + return EXIT_INCOMPATIBLE; + } + console.log( + `prisma/cloud-deploy-action's test passes against ${executable}.`, + ); + return 0; +} + +process.exit(await exitCodeOf(main)); diff --git a/scripts/check-cloud-deploy-action.test.mjs b/scripts/check-cloud-deploy-action.test.mjs new file mode 100644 index 00000000..e9d8881c --- /dev/null +++ b/scripts/check-cloud-deploy-action.test.mjs @@ -0,0 +1,127 @@ +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync, symlinkSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { after, describe, it } from "node:test"; +import { fileURLToPath } from "node:url"; +import { exitCodeOf, judgeRun } from "./check-cloud-deploy-action-utils.mjs"; + +const production = "prisma 8.0.0 accepts the deploy arguments for production"; +const named = "prisma 8.0.0 accepts the deploy arguments for pr-7"; +const destroy = "prisma 8.0.0 accepts the destroy arguments"; + +describe("judgeRun", () => { + it("passes when both deploy cases pass and the destroy case is skipped", () => { + assert.deepEqual( + judgeRun([ + { name: production, outcome: "pass" }, + { name: named, outcome: "pass" }, + { name: destroy, outcome: "skip" }, + ]), + { verdict: "pass", problems: [] }, + ); + }); + + it("reports a run with no cases as not run, as when the action renames PRISMA_CLI_BINS", () => { + const { verdict, problems } = judgeRun([ + { name: "tests/prisma-cli.test.mjs", outcome: "pass" }, + ]); + assert.equal(verdict, "not-run"); + assert.equal(problems.length, 2); + assert.ok( + problems[0].includes("accepts the deploy arguments for production"), + ); + assert.ok(problems[0].includes("test titles may have changed")); + }); + + it("reports a skipped deploy case as not run", () => { + const { verdict, problems } = judgeRun([ + { name: production, outcome: "pass" }, + { name: named, outcome: "skip" }, + ]); + assert.equal(verdict, "not-run"); + assert.ok(problems[0].includes("")); + }); + + it("reports a failed deploy case as incompatible", () => { + assert.deepEqual( + judgeRun([ + { name: production, outcome: "fail" }, + { name: named, outcome: "pass" }, + ]), + { verdict: "incompatible", problems: [`Failed: ${production}`] }, + ); + }); + + it("reports any other failed case as incompatible", () => { + assert.deepEqual( + judgeRun([ + { name: production, outcome: "pass" }, + { name: named, outcome: "pass" }, + { name: destroy, outcome: "fail" }, + ]), + { verdict: "incompatible", problems: [`Failed: ${destroy}`] }, + ); + }); +}); + +describe("exitCodeOf", () => { + it("returns what the check returns", async () => { + assert.equal(await exitCodeOf(async () => 1), 1); + }); + + it("reports a crash as a check that could not run", async () => { + const crash = async () => { + throw new Error("boom"); + }; + assert.equal(await exitCodeOf(crash), 2); + }); +}); + +describe("check-cloud-deploy-action.mjs", () => { + const scripts = dirname(fileURLToPath(import.meta.url)); + const actionDir = join(scripts, "fixtures/cloud-deploy-action"); + const rejecting = join(scripts, "fixtures/rejecting-prisma"); + const accepting = join(scripts, "fixtures/accepting-prisma"); + const linkRoot = mkdtempSync(join(tmpdir(), "check-cloud-deploy-action-")); + after(() => rmSync(linkRoot, { recursive: true, force: true })); + + // Inside `node --test`, run() in the child would otherwise skip its files. + const { NODE_TEST_CONTEXT: _, ...env } = process.env; + + function check(script, bin) { + return spawnSync(process.execPath, [script, actionDir, bin], { + encoding: "utf8", + env, + }); + } + + const script = join(scripts, "check-cloud-deploy-action.mjs"); + + it("exits 0 for a CLI that accepts the arguments", () => { + const result = check(script, accepting); + assert.equal(result.status, 0, result.stderr); + }); + + it("exits 1 for a CLI that rejects the arguments", () => { + const result = check(script, rejecting); + assert.equal(result.status, 1, result.stderr); + assert.ok(result.stderr.includes("cannot drive")); + }); + + it("exits 2 when the executable is missing", () => { + const result = check(script, join(scripts, "fixtures/no-such-prisma")); + assert.equal(result.status, 2, result.stderr); + }); + + it("runs when started through a symbolic link", () => { + const linked = join(linkRoot, "scripts"); + symlinkSync(scripts, linked); + const result = check( + join(linked, "check-cloud-deploy-action.mjs"), + rejecting, + ); + assert.equal(result.status, 1, result.stderr); + }); +}); diff --git a/scripts/fixtures/accepting-prisma b/scripts/fixtures/accepting-prisma new file mode 100755 index 00000000..4d60ecce --- /dev/null +++ b/scripts/fixtures/accepting-prisma @@ -0,0 +1,2 @@ +#!/usr/bin/env node +console.log(process.argv[2] === "--version" ? "8.0.0" : "CLI.CREDENTIALS_REQUIRED"); diff --git a/scripts/fixtures/cloud-deploy-action/tests/prisma-cli.test.mjs b/scripts/fixtures/cloud-deploy-action/tests/prisma-cli.test.mjs new file mode 100644 index 00000000..89333bff --- /dev/null +++ b/scripts/fixtures/cloud-deploy-action/tests/prisma-cli.test.mjs @@ -0,0 +1,25 @@ +// A small stand-in for prisma/cloud-deploy-action's tests/prisma-cli.test.mjs +// with the same contract: PRISMA_CLI_BINS and the deploy case titles. +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { test } from "node:test"; + +const REJECTED_ARGUMENTS = /CLI\.INVALID_ARGUMENTS/; +const bins = (process.env.PRISMA_CLI_BINS ?? "").split(",").filter(Boolean); + +for (const bin of bins) { + const version = spawnSync(bin, ["--version"], { + encoding: "utf8", + }).stdout.trim(); + for (const environment of ["", "pr-7"]) { + test(`prisma ${version} accepts the deploy arguments for ${environment || "production"}`, () => { + const args = [ + "deploy", + "module.ts", + ...(environment ? ["--environment", environment] : []), + ]; + const { stdout } = spawnSync(bin, args, { encoding: "utf8" }); + assert.doesNotMatch(stdout, REJECTED_ARGUMENTS); + }); + } +} diff --git a/scripts/fixtures/rejecting-prisma b/scripts/fixtures/rejecting-prisma new file mode 100755 index 00000000..8a5d79f9 --- /dev/null +++ b/scripts/fixtures/rejecting-prisma @@ -0,0 +1,2 @@ +#!/usr/bin/env node +console.log(process.argv[2] === "--version" ? "8.0.0" : "CLI.INVALID_ARGUMENTS");