diff --git a/.github/workflows/merge-pinned-sources.yml b/.github/workflows/merge-pinned-sources.yml new file mode 100644 index 0000000..e6173be --- /dev/null +++ b/.github/workflows/merge-pinned-sources.yml @@ -0,0 +1,92 @@ +name: Merge pinned source update + +# Merges only the automated pinned-source pull request, after Site checks pass +# on its exact head. Every other pull request keeps its normal merge path. +on: + workflow_run: + workflows: [Site checks] + types: [completed] + +permissions: + contents: read + +concurrency: + group: pinned-sources-merge + cancel-in-progress: false + +jobs: + merge: + name: Merge verified pinned source update + if: >- + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' && + github.event.workflow_run.head_repository.full_name == github.repository && + startsWith(github.event.workflow_run.head_branch, 'automation/sources-') + runs-on: ubuntu-24.04 + steps: + - name: Create website automation token + id: app-token + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 + with: + client-id: ${{ vars.RELEASE_AUTOMATION_CLIENT_ID }} + private-key: ${{ secrets.RELEASE_AUTOMATION_APP_PRIVATE_KEY }} + owner: mdbase-dev + repositories: mdbase.dev + permission-contents: write + permission-pull-requests: write + + - name: Merge the exact checked head + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + BRANCH: ${{ github.event.workflow_run.head_branch }} + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} + run: | + fields=number,author,baseRefName,files,headRefOid,isCrossRepository,mergeStateStatus,state,title + pr="$(gh pr list --repo "$GITHUB_REPOSITORY" --state open --head "$BRANCH" --json "$fields" --jq '.[0] // empty')" + if [ -z "$pr" ]; then + echo "No open pinned source update for $BRANCH." + exit 0 + fi + if [ "$(jq -r .headRefOid <<<"$pr")" != "$HEAD_SHA" ]; then + echo "$BRANCH moved after these checks; its own Site checks run decides." + exit 0 + fi + if [[ ! "$BRANCH" =~ ^automation/sources-([0-9a-f]{12})-([0-9a-f]{12})$ ]]; then + echo "Unexpected branch name $BRANCH." >&2 + exit 1 + fi + spec="${BASH_REMATCH[1]}" + contracts="${BASH_REMATCH[2]}" + jq -e --arg title "Update pinned sources to specification $spec and contracts $contracts" ' + .author.login == "app/mdbase-dev-release-automation" and + .baseRefName == "main" and + .isCrossRepository == false and + .title == $title and + ([.files[].path] | sort) == ["site-sources.json"] + ' <<<"$pr" >/dev/null || { + echo "This pull request is not an automated pinned-source update." >&2 + exit 1 + } + + # Only the specification and contracts pins may differ from the + # branch point, and they must be the revisions the branch names. + base="$(gh api "repos/$GITHUB_REPOSITORY/compare/main...$HEAD_SHA" --jq .merge_base_commit.sha)" + gh api "repos/$GITHUB_REPOSITORY/contents/site-sources.json?ref=$base" --jq .content | base64 -d > "$RUNNER_TEMP/base.json" + gh api "repos/$GITHUB_REPOSITORY/contents/site-sources.json?ref=$HEAD_SHA" --jq .content | base64 -d > "$RUNNER_TEMP/head.json" + jq -e -n --slurpfile base "$RUNNER_TEMP/base.json" --slurpfile head "$RUNNER_TEMP/head.json" \ + --arg spec "$spec" --arg contracts "$contracts" ' + ($base[0] | del(.specification.ref, .contracts.ref)) == ($head[0] | del(.specification.ref, .contracts.ref)) and + ($head[0].specification.ref | test("^[0-9a-f]{40}$") and startswith($spec)) and + ($head[0].contracts.ref | test("^[0-9a-f]{40}$") and startswith($contracts)) + ' >/dev/null || { + echo "site-sources.json changes more than the named specification and contracts pins." >&2 + exit 1 + } + + number="$(jq -r .number <<<"$pr")" + # main is strict, so a behind branch needs fresh checks on its new head. + if [ "$(jq -r .mergeStateStatus <<<"$pr")" = "BEHIND" ]; then + gh pr update-branch "$number" --repo "$GITHUB_REPOSITORY" + exit 0 + fi + gh pr merge "$number" --repo "$GITHUB_REPOSITORY" --squash --match-head-commit "$HEAD_SHA" --delete-branch diff --git a/.github/workflows/update-pinned-sources.yml b/.github/workflows/update-pinned-sources.yml new file mode 100644 index 0000000..732f423 --- /dev/null +++ b/.github/workflows/update-pinned-sources.yml @@ -0,0 +1,118 @@ +name: Update pinned sources + +# Moves the specification and contract-catalog pins in site-sources.json to +# the head of their main branches. The pull request it opens is built and +# tested by Site checks like any other change, and merge-pinned-sources.yml +# merges it once those checks pass on its exact head. +on: + schedule: + - cron: "17 * * * *" + repository_dispatch: + types: [pinned-source-updated] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: pinned-sources-update + cancel-in-progress: false + +jobs: + update: + name: Open pinned source update + runs-on: ubuntu-24.04 + steps: + - name: Check out website + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: main + persist-credentials: false + + - name: Select upstream revisions + id: select + env: + GH_TOKEN: ${{ github.token }} + run: | + changed=false + for source in specification contracts; do + repository="$(jq -r ".$source.repository" site-sources.json)" + current="$(jq -r ".$source.ref" site-sources.json)" + head="$(git ls-remote "https://github.com/$repository.git" refs/heads/main | cut -f1)" + if [[ ! "$head" =~ ^[0-9a-f]{40}$ ]]; then + echo "Could not read the main branch of $repository." >&2 + exit 1 + fi + if [ "$head" != "$current" ]; then + # A pin only moves forward along main. + status="$(gh api "repos/$repository/compare/$current...$head" --jq .status)" + if [ "$status" != "ahead" ]; then + echo "$repository main is $status relative to the pinned $current; not moving the pin." >&2 + exit 1 + fi + jq --arg source "$source" --arg head "$head" '.[$source].ref = $head' site-sources.json > "$RUNNER_TEMP/site-sources.json" + mv "$RUNNER_TEMP/site-sources.json" site-sources.json + changed=true + fi + echo "${source}_ref=$head" >> "$GITHUB_OUTPUT" + done + echo "changed=$changed" >> "$GITHUB_OUTPUT" + + - name: Create website automation token + if: steps.select.outputs.changed == 'true' + id: app-token + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 + with: + client-id: ${{ vars.RELEASE_AUTOMATION_CLIENT_ID }} + private-key: ${{ secrets.RELEASE_AUTOMATION_APP_PRIVATE_KEY }} + owner: mdbase-dev + repositories: mdbase.dev + permission-contents: write + permission-pull-requests: write + + - name: Open pull request + if: steps.select.outputs.changed == 'true' + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + SPEC_REF: ${{ steps.select.outputs.specification_ref }} + CONTRACTS_REF: ${{ steps.select.outputs.contracts_ref }} + run: | + spec="${SPEC_REF:0:12}" + contracts="${CONTRACTS_REF:0:12}" + branch="automation/sources-$spec-$contracts" + title="Update pinned sources to specification $spec and contracts $contracts" + existing="$(gh pr list --repo "$GITHUB_REPOSITORY" --state all --head "$branch" --json url,state --jq '.[0] // empty')" + if [ -n "$existing" ]; then + # Open, merged, or deliberately closed: either way this revision + # pair has been handled, and the next upstream commit opens anew. + echo "These revisions are already represented by $(jq -r .url <<<"$existing") ($(jq -r .state <<<"$existing"))." + exit 0 + fi + if git ls-remote --exit-code --heads "https://github.com/$GITHUB_REPOSITORY.git" "$branch" >/dev/null 2>&1; then + echo "Remote branch $branch exists without a pull request; refusing to overwrite it." >&2 + exit 1 + fi + git switch -c "$branch" + git config user.name "mdbase release automation[bot]" + git config user.email "mdbase-release-automation[bot]@users.noreply.github.com" + git add site-sources.json + git commit -m "$title" + git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" + git push --set-upstream origin "$branch" + body="$(printf '%s\n' \ + "Moves the published sources to the head of their main branches:" \ + "" \ + "- specification: [mdbase-dev/mdbase-spec@$spec](https://github.com/mdbase-dev/mdbase-spec/commit/$SPEC_REF)" \ + "- contracts: [mdbase-dev/mdbase-contracts@$contracts](https://github.com/mdbase-dev/mdbase-contracts/commit/$CONTRACTS_REF)" \ + "" \ + "Site checks build and test the site at these revisions. The pull request merges itself when they pass.")" + url="$(gh pr create --repo "$GITHUB_REPOSITORY" --base main --head "$branch" --title "$title" --body "$body")" + echo "Opened $url" + + # Runs are serialized and always take the newest heads, so any other + # open update is older and would only move the pins back. + gh pr list --repo "$GITHUB_REPOSITORY" --state open --json number,headRefName \ + --jq ".[] | select(.headRefName | startswith(\"automation/sources-\")) | select(.headRefName != \"$branch\") | .number" | + while read -r number; do + gh pr close "$number" --repo "$GITHUB_REPOSITORY" --delete-branch --comment "Superseded by $url." + done diff --git a/README.md b/README.md index 48e0191..dfe1c35 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,25 @@ The workflow uses a GitHub App configured as `RELEASE_AUTOMATION_CLIENT_ID` and only `contents: write` and `pull requests: write`. The App token ensures the resulting pull request triggers the ordinary protected `Site checks` workflow. +## Specification and contract updates + +The specification and contract-catalog pins follow their repositories' `main` +branches. Every hour (and on a `pinned-source-updated` repository dispatch or a +manual run), `.github/workflows/update-pinned-sources.yml` compares both pins +with the upstream heads. When either has moved forward, it opens one pull +request on an `automation/sources--` branch that changes only +those refs. `Site checks` builds and tests the site at the new revisions, and +`.github/workflows/merge-pinned-sources.yml` merges the pull request when they +pass on its exact head. The merge then deploys through the normal `main` path. +A newer update closes an older open one. If the checks fail, the pull request +stays open for a person to look at, and the next upstream commit opens a fresh +one. + +The merge workflow acts only on a pull request opened by the release App, +titled for the two revisions its branch names, and changing nothing in +`site-sources.json` except those two refs. It uses the same App credentials +as the Connect release update. + ## Development deployment ```sh diff --git a/scripts/import-spec.mjs b/scripts/import-spec.mjs index 5384931..8d40507 100644 --- a/scripts/import-spec.mjs +++ b/scripts/import-spec.mjs @@ -13,6 +13,7 @@ const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const specDir = resolve(process.env.MDBASE_SPEC_DIR ?? join(root, "..", "mdbase-spec")); const source = resolve(process.env.MDBASE_SPEC_SITE_DIST ?? join(specDir, "site", "dist")); const destination = join(root, "dist", "spec"); +const SUPPORT_FILES = ["IMPLEMENTING.md", "REFERENCE-RUNNER.md", "QUICK-REFERENCE.md"]; const siteOrigin = new URL(process.env.MDBASE_SITE_ORIGIN ?? "https://mdbase.dev").origin; required(join(root, "dist", "index.html"), "Build mdbase.dev before importing the specification"); @@ -29,7 +30,7 @@ writeFileSync( rewrite(readFileSync(join(source, "spec-v0.2.html"), "utf8"), true) ); -for (const support of ["IMPLEMENTING.md", "REFERENCE-RUNNER.md", "QUICK-REFERENCE.md"]) { +for (const support of SUPPORT_FILES) { const path = join(specDir, "v0.2", support); if (existsSync(path)) copyFileSync(path, join(destination, "v0.2", support)); } @@ -64,7 +65,17 @@ function rewrite(html, archive) { ) .replace(/([^<]+)<\/a>/g, "$1") .replace(/([^<]+)<\/a>/g, "$1") - .replace(/href="\.\/\d{2}-[^"#]+\.md#([^"]+)"/g, 'href="#$1"'); + .replace(/href="\.\/\d{2}-[^"#]+\.md#([^"]+)"/g, 'href="#$1"') + // Any other repository-relative document (release records, release + // notes) is not published on this site, so link to it on GitHub. The + // archive's support guides are copied beside it below, so they stay. + .replace( + /href="\.\/([^"#]+\.md)(#[^"]*)?"/g, + (link, path, fragment = "") => + archive && SUPPORT_FILES.includes(path) + ? link + : `href="https://github.com/mdbase-dev/mdbase-spec/blob/main/${path}${fragment}"` + ); return renderSpecPage(readFileSync(shellPath, "utf8"), content, archive); } diff --git a/scripts/sync-sources.mjs b/scripts/sync-sources.mjs index 4fbc176..637c0de 100644 --- a/scripts/sync-sources.mjs +++ b/scripts/sync-sources.mjs @@ -44,7 +44,7 @@ const runtimeSchemaSource = join( specDir, "standard-packs", "mdbase-runtime", - "0.2.0", + "0.2.1", "schemas", ); const runtimeSchemaDestination = join(root, "public", "runtime", "schemas", "v0.2"); diff --git a/site-sources.json b/site-sources.json index b65adfd..5690252 100644 --- a/site-sources.json +++ b/site-sources.json @@ -1,7 +1,7 @@ { "specification": { "repository": "mdbase-dev/mdbase-spec", - "ref": "eabae2143275a0619b02d564961fa74b8037185e" + "ref": "13531d8d2ad7344d45d0eb15124e731e07ee76c4" }, "connect": { "repository": "mdbase-dev/mdbase-connect", diff --git a/src/pages/runtime/index.astro b/src/pages/runtime/index.astro index 6f36f55..89d632b 100644 --- a/src/pages/runtime/index.astro +++ b/src/pages/runtime/index.astro @@ -153,11 +153,11 @@ steps:

- + Open the workflow JSON Schema {" ยท "} - + Inspect the complete standard pack