|
| 1 | +name: Update pinned sources |
| 2 | + |
| 3 | +# Moves the specification and contract-catalog pins in site-sources.json to |
| 4 | +# the head of their main branches. The pull request it opens is built and |
| 5 | +# tested by Site checks like any other change, and merge-pinned-sources.yml |
| 6 | +# merges it once those checks pass on its exact head. |
| 7 | +on: |
| 8 | + schedule: |
| 9 | + - cron: "17 * * * *" |
| 10 | + repository_dispatch: |
| 11 | + types: [pinned-source-updated] |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: pinned-sources-update |
| 19 | + cancel-in-progress: false |
| 20 | + |
| 21 | +jobs: |
| 22 | + update: |
| 23 | + name: Open pinned source update |
| 24 | + runs-on: ubuntu-24.04 |
| 25 | + steps: |
| 26 | + - name: Check out website |
| 27 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 28 | + with: |
| 29 | + ref: main |
| 30 | + persist-credentials: false |
| 31 | + |
| 32 | + - name: Select upstream revisions |
| 33 | + id: select |
| 34 | + env: |
| 35 | + GH_TOKEN: ${{ github.token }} |
| 36 | + run: | |
| 37 | + changed=false |
| 38 | + for source in specification contracts; do |
| 39 | + repository="$(jq -r ".$source.repository" site-sources.json)" |
| 40 | + current="$(jq -r ".$source.ref" site-sources.json)" |
| 41 | + head="$(git ls-remote "https://github.com/$repository.git" refs/heads/main | cut -f1)" |
| 42 | + if [[ ! "$head" =~ ^[0-9a-f]{40}$ ]]; then |
| 43 | + echo "Could not read the main branch of $repository." >&2 |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + if [ "$head" != "$current" ]; then |
| 47 | + # A pin only moves forward along main. |
| 48 | + status="$(gh api "repos/$repository/compare/$current...$head" --jq .status)" |
| 49 | + if [ "$status" != "ahead" ]; then |
| 50 | + echo "$repository main is $status relative to the pinned $current; not moving the pin." >&2 |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + jq --arg source "$source" --arg head "$head" '.[$source].ref = $head' site-sources.json > "$RUNNER_TEMP/site-sources.json" |
| 54 | + mv "$RUNNER_TEMP/site-sources.json" site-sources.json |
| 55 | + changed=true |
| 56 | + fi |
| 57 | + echo "${source}_ref=$head" >> "$GITHUB_OUTPUT" |
| 58 | + done |
| 59 | + echo "changed=$changed" >> "$GITHUB_OUTPUT" |
| 60 | +
|
| 61 | + - name: Create website automation token |
| 62 | + if: steps.select.outputs.changed == 'true' |
| 63 | + id: app-token |
| 64 | + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 |
| 65 | + with: |
| 66 | + client-id: ${{ vars.RELEASE_AUTOMATION_CLIENT_ID }} |
| 67 | + private-key: ${{ secrets.RELEASE_AUTOMATION_APP_PRIVATE_KEY }} |
| 68 | + owner: mdbase-dev |
| 69 | + repositories: mdbase.dev |
| 70 | + permission-contents: write |
| 71 | + permission-pull-requests: write |
| 72 | + |
| 73 | + - name: Open pull request |
| 74 | + if: steps.select.outputs.changed == 'true' |
| 75 | + env: |
| 76 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 77 | + SPEC_REF: ${{ steps.select.outputs.specification_ref }} |
| 78 | + CONTRACTS_REF: ${{ steps.select.outputs.contracts_ref }} |
| 79 | + run: | |
| 80 | + spec="${SPEC_REF:0:12}" |
| 81 | + contracts="${CONTRACTS_REF:0:12}" |
| 82 | + branch="automation/sources-$spec-$contracts" |
| 83 | + title="Update pinned sources to specification $spec and contracts $contracts" |
| 84 | + existing="$(gh pr list --repo "$GITHUB_REPOSITORY" --state all --head "$branch" --json url,state --jq '.[0] // empty')" |
| 85 | + if [ -n "$existing" ]; then |
| 86 | + # Open, merged, or deliberately closed: either way this revision |
| 87 | + # pair has been handled, and the next upstream commit opens anew. |
| 88 | + echo "These revisions are already represented by $(jq -r .url <<<"$existing") ($(jq -r .state <<<"$existing"))." |
| 89 | + exit 0 |
| 90 | + fi |
| 91 | + if git ls-remote --exit-code --heads "https://github.com/$GITHUB_REPOSITORY.git" "$branch" >/dev/null 2>&1; then |
| 92 | + echo "Remote branch $branch exists without a pull request; refusing to overwrite it." >&2 |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + git switch -c "$branch" |
| 96 | + git config user.name "mdbase release automation[bot]" |
| 97 | + git config user.email "mdbase-release-automation[bot]@users.noreply.github.com" |
| 98 | + git add site-sources.json |
| 99 | + git commit -m "$title" |
| 100 | + git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 101 | + git push --set-upstream origin "$branch" |
| 102 | + body="$(printf '%s\n' \ |
| 103 | + "Moves the published sources to the head of their main branches:" \ |
| 104 | + "" \ |
| 105 | + "- specification: [mdbase-dev/mdbase-spec@$spec](https://github.com/mdbase-dev/mdbase-spec/commit/$SPEC_REF)" \ |
| 106 | + "- contracts: [mdbase-dev/mdbase-contracts@$contracts](https://github.com/mdbase-dev/mdbase-contracts/commit/$CONTRACTS_REF)" \ |
| 107 | + "" \ |
| 108 | + "Site checks build and test the site at these revisions. The pull request merges itself when they pass.")" |
| 109 | + url="$(gh pr create --repo "$GITHUB_REPOSITORY" --base main --head "$branch" --title "$title" --body "$body")" |
| 110 | + echo "Opened $url" |
| 111 | +
|
| 112 | + # Runs are serialized and always take the newest heads, so any other |
| 113 | + # open update is older and would only move the pins back. |
| 114 | + gh pr list --repo "$GITHUB_REPOSITORY" --state open --json number,headRefName \ |
| 115 | + --jq ".[] | select(.headRefName | startswith(\"automation/sources-\")) | select(.headRefName != \"$branch\") | .number" | |
| 116 | + while read -r number; do |
| 117 | + gh pr close "$number" --repo "$GITHUB_REPOSITORY" --delete-branch --comment "Superseded by $url." |
| 118 | + done |
0 commit comments