|
| 1 | +# Keeps Dependabot PRs compatible with the Changeset check in repo.yaml by |
| 2 | +# generating (and force-refreshing) a changeset from the PR's diff. |
| 3 | +# |
| 4 | +# Side effect: once this workflow pushes, Dependabot considers the branch |
| 5 | +# externally modified and stops auto-rebasing the PR. That's fine — grouped |
| 6 | +# updates arrive as fresh PRs weekly, and `@dependabot recreate` still works |
| 7 | +# (the recreated branch re-triggers this workflow). |
| 8 | +name: Dependabot Changeset |
| 9 | + |
| 10 | +on: |
| 11 | + pull_request: |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write # push the changeset commit |
| 15 | + actions: write # dispatch repo.yaml on the new head |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: '${{ github.workflow }}-${{ github.head_ref }}' |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + changeset: |
| 23 | + name: Generate changeset |
| 24 | + runs-on: ubuntu-latest |
| 25 | + # Only same-repo PRs authored by dependabot. PR author (not actor) so a |
| 26 | + # human reopening/rebasing the PR still gets a changeset regenerated. |
| 27 | + if: > |
| 28 | + github.event.pull_request.user.login == 'dependabot[bot]' && |
| 29 | + github.event.pull_request.head.repo.full_name == github.repository |
| 30 | + steps: |
| 31 | + - uses: actions/checkout@v4 |
| 32 | + with: |
| 33 | + # The event's head commit, not the merge ref (so the push below is a |
| 34 | + # fast-forward of the PR branch) and not the branch name (so a ref |
| 35 | + # that moved after the event was validated makes the push fail |
| 36 | + # loudly instead of silently building on an unvetted commit). |
| 37 | + ref: ${{ github.event.pull_request.head.sha }} |
| 38 | + fetch-depth: 0 # needed for git diff against base branch |
| 39 | + |
| 40 | + - name: Generate and push changeset |
| 41 | + id: generate |
| 42 | + env: # env indirection: never interpolate PR-controlled strings into the script |
| 43 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 44 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 45 | + BASE_REF: ${{ github.event.pull_request.base.ref }} |
| 46 | + HEAD_REF: ${{ github.event.pull_request.head.ref }} |
| 47 | + run: | |
| 48 | + # Dependabot only touches package.json files and the root lockfile, |
| 49 | + # and the lockfile lives outside every workspace package, so the |
| 50 | + # changed non-private package.json files are exactly the packages |
| 51 | + # `changeset status` would report as changed. Private / versionless |
| 52 | + # packages (services/*, root) cannot be versioned by changesets. |
| 53 | + pkgs=$(git diff --name-only "origin/$BASE_REF"...HEAD -- '*/package.json' | |
| 54 | + while read -r f; do |
| 55 | + [ -f "$f" ] && jq -r 'if .private == true or .version == null then empty else .name end' "$f" |
| 56 | + done | sort -u) |
| 57 | +
|
| 58 | + # Always (re)compute our changeset from the current diff: grouped PRs |
| 59 | + # are re-created with a different set of bumped packages every week, |
| 60 | + # so a changeset from an earlier run may list stale packages — or |
| 61 | + # need to disappear entirely if no versionable package changed. |
| 62 | + changeset=".changeset/dependabot-${PR_NUMBER}.md" |
| 63 | + if [ -z "$pkgs" ]; then |
| 64 | + rm -f "$changeset" |
| 65 | + else |
| 66 | + { |
| 67 | + echo '---' |
| 68 | + while read -r name; do echo "'$name': patch"; done <<<"$pkgs" |
| 69 | + echo '---' |
| 70 | + echo |
| 71 | + printf '%s\n' "$PR_TITLE" |
| 72 | + } >"$changeset" |
| 73 | + fi |
| 74 | +
|
| 75 | + git add .changeset |
| 76 | + if git diff --cached --quiet; then |
| 77 | + echo 'Changeset up to date; nothing to push.' |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | + # Commit as the github-actions[bot] app account, matching the |
| 81 | + # identity changesets/action uses for "Version packages" commits. |
| 82 | + git config user.name 'github-actions[bot]' |
| 83 | + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' |
| 84 | + git commit -m 'Sync changeset with Dependabot update' |
| 85 | + git push origin "HEAD:$HEAD_REF" |
| 86 | + echo 'pushed=true' >>"$GITHUB_OUTPUT" |
| 87 | +
|
| 88 | + # A push made with GITHUB_TOKEN does not re-trigger pull_request |
| 89 | + # workflows, so the new head commit would show no checks. Explicit |
| 90 | + # workflow_dispatch is exempt from that rule and attaches its check |
| 91 | + # runs to the new head commit. |
| 92 | + - name: Re-run CI on the new head |
| 93 | + if: steps.generate.outputs.pushed == 'true' |
| 94 | + env: |
| 95 | + GH_TOKEN: ${{ github.token }} |
| 96 | + HEAD_REF: ${{ github.event.pull_request.head.ref }} |
| 97 | + run: gh workflow run repo.yaml --repo "$GITHUB_REPOSITORY" --ref "$HEAD_REF" |
0 commit comments