From 5c466d4f1e44d85e4b2ab6bc22bf0f5dbdfdf323 Mon Sep 17 00:00:00 2001 From: dillonstreator Date: Sat, 28 Nov 2026 13:49:52 -0600 Subject: [PATCH] ci: add changesets to Dependabot PRs automatically Dependabot doesn't write changesets, so its PRs always failed the changeset check. A pull_request_target workflow now writes a patch changeset for the packages a Dependabot PR touches and pushes it to the branch, without running any PR code. The changeset check skips Dependabot PRs because a GITHUB_TOKEN push doesn't re-trigger it. --- .github/workflows/changeset.yml | 3 + .github/workflows/dependabot-changeset.yml | 88 ++++++++++++++++++++++ CONTRIBUTING.md | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/dependabot-changeset.yml diff --git a/.github/workflows/changeset.yml b/.github/workflows/changeset.yml index f58bd3b..598b8b4 100644 --- a/.github/workflows/changeset.yml +++ b/.github/workflows/changeset.yml @@ -16,8 +16,11 @@ concurrency: jobs: changeset: # The Version Packages PR from release.yml consumes changesets, so it never has one. + # Dependabot PRs get theirs from dependabot-changeset.yml; its push uses GITHUB_TOKEN, + # which doesn't re-trigger this workflow, so checking them here would stay red. if: >- !startsWith(github.head_ref, 'changeset-release/') && + github.event.pull_request.user.login != 'dependabot[bot]' && !contains(github.event.pull_request.labels.*.name, 'no-changeset') runs-on: ubuntu-latest steps: diff --git a/.github/workflows/dependabot-changeset.yml b/.github/workflows/dependabot-changeset.yml new file mode 100644 index 0000000..4f6ea06 --- /dev/null +++ b/.github/workflows/dependabot-changeset.yml @@ -0,0 +1,88 @@ +name: Dependabot changeset + +# Dependabot doesn't know about Changesets, so its PRs would always fail the changeset +# check in changeset.yml (which skips them). This writes a patch changeset for the +# packages a Dependabot PR touches and pushes it to the PR branch. +# +# pull_request_target is needed because Dependabot-triggered pull_request runs only get a +# read-only token. It runs with write access, so it must never execute code from the PR: +# no install, build or package scripts, only git and reading package.json as data. +on: + pull_request_target: + branches: [main] + types: [opened, synchronize, reopened] + +permissions: + contents: read + +concurrency: + group: dependabot-changeset-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + changeset: + if: >- + github.event.pull_request.user.login == 'dependabot[bot]' && + github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.event.pull_request.head.ref }} + fetch-depth: 0 + + - name: Add changeset if missing + env: + HEAD_REF: ${{ github.event.pull_request.head.ref }} + PR_NUMBER: ${{ github.event.pull_request.number }} + PR_TITLE: ${{ github.event.pull_request.title }} + run: | + set -euo pipefail + git fetch --no-tags origin main + + if git diff --name-only --diff-filter=A origin/main...HEAD -- '.changeset/*.md' | grep -qv '^.changeset/README.md$'; then + echo "PR already has a changeset." + exit 0 + fi + + # Same scope changeset.yml enforces: anything under packages/*, plus root + # workspace/lockfile changes (security overrides), which ship as patch releases. + changed=$(git diff --name-only origin/main...HEAD) + dirs=$(printf '%s\n' "$changed" | sed -n 's#^packages/\([^/]*\)/.*#\1#p' | sort -u) + root=$(printf '%s\n' "$changed" | grep -E '^(pnpm-workspace\.yaml|pnpm-lock\.yaml)$' || true) + + packages=() + for dir in $dirs; do + if [ -f "packages/$dir/package.json" ]; then + packages+=("$(jq -r .name "packages/$dir/package.json")") + fi + done + # Lockfile-only bumps don't map to a package; core is in the fixed release group, + # so bumping it releases every published package. + if [ ${#packages[@]} -eq 0 ] && [ -n "$root" ]; then + packages+=("@crypt.fyi/core") + fi + if [ ${#packages[@]} -eq 0 ]; then + echo "No package or lockfile changes; no changeset needed." + exit 0 + fi + + file=".changeset/dependabot-${PR_NUMBER}.md" + { + echo '---' + for pkg in "${packages[@]}"; do + echo "'$pkg': patch" + done + echo '---' + echo + printf '%s\n' "$PR_TITLE" | sed 's/^chore(deps[^)]*): //; s/^./\U&/' + } > "$file" + cat "$file" + + git config user.name 'github-actions[bot]' + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' + git add "$file" + git commit -m "chore: add changeset for Dependabot PR #${PR_NUMBER}" + git push origin "HEAD:refs/heads/${HEAD_REF}" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9676362..b6fdd58 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -49,7 +49,7 @@ pnpm release # build, publish npm packages, tag, upload Chrome extens On `main`, `.github/workflows/release.yml` opens a Version Packages PR or runs `pnpm release`. -Pull requests must include a changeset when they change files under `packages/*` or the root `pnpm-workspace.yaml` / `pnpm-lock.yaml` (dependency and security override bumps ship as patch releases). `.github/workflows/changeset.yml` enforces this. For changes that don't need a release (docs, tests, CI), run `pnpm changeset --empty` or ask a maintainer to add the `no-changeset` label. +Pull requests must include a changeset when they change files under `packages/*` or the root `pnpm-workspace.yaml` / `pnpm-lock.yaml` (dependency and security override bumps ship as patch releases). `.github/workflows/changeset.yml` enforces this. For changes that don't need a release (docs, tests, CI), run `pnpm changeset --empty` or ask a maintainer to add the `no-changeset` label. Dependabot PRs are exempt from that check: `.github/workflows/dependabot-changeset.yml` pushes a patch changeset to them instead. ## Pull Request Process