Files
carbon-lang/.github/workflows/dependabot_pr_description.yaml
T
Chandler Carruth 80a4a4d005 Fix detection of dependabot-authored descriptions (#8083)
Instead of looking for a marker within the description, look at the
editor directly using the `gh` API querying capabilities. This lets us
detect when we have the initial description, as well as when the
description is updated by dependabot rebasing or recreating a PR.

Also prunes URL prefixes that aren't needed.

Assisted-by: Antigravity with Opus
2026-11-03 21:55:47 +00:00

88 lines
3.6 KiB
YAML

# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# Replaces Dependabot's PR description with a short markdown list of the updated
# dependencies. We squash-merge using the PR description as the commit message,
# and Dependabot's descriptions are mostly HTML: release notes, commit lists,
# and command help. The original description remains in the PR's edit history.
name: 'Dependabot PR description'
on:
pull_request:
types: [opened, reopened, synchronize, edited]
# Dependabot can push and edit the description in quick succession.
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions: {}
jobs:
rewrite:
if: github.event.pull_request.user.login == 'dependabot[bot]'
runs-on: ubuntu-latest
permissions:
contents: read # For dependabot/fetch-metadata to read the PR's commit.
pull-requests: write # For `gh pr edit`.
steps:
- name: Harden Runner
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
disable-sudo: true
egress-policy: block
# prettier-ignore
allowed-endpoints: >
api.github.com:443
- id: metadata
uses: dependabot/fetch-metadata@25dd0e34f4fe68f24cc83900b1fe3fe149efef98 # v3.1.0
- name: Replace the description
env:
DEPENDENCIES: ${{ steps.metadata.outputs.updated-dependencies-json }}
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_URL: ${{ github.event.pull_request.html_url }}
run: |
# Only replace a description that Dependabot wrote: either nobody has
# edited it, or Dependabot last did, such as when rebasing. This
# leaves human edits and our own earlier rewrites alone. Don't look
# for markers in the text, because Dependabot truncates descriptions
# that are too long.
# shellcheck disable=SC2016 # The `$`s are GraphQL variables.
editor="$(gh api graphql \
-f owner="${GITHUB_REPOSITORY%/*}" \
-f name="${GITHUB_REPOSITORY#*/}" \
-F number="$PR_NUMBER" \
-f query='
query($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $number) { editor { login } }
}
}' \
--jq '.data.repository.pullRequest.editor.login // ""')"
if [[ -n "$editor" && "$editor" != "dependabot" ]]; then
echo "The description was last edited by $editor, so leaving it."
exit 0
fi
# One short line per dependency. GitHub hard-wraps squash commit
# messages at 72 columns, so avoid tables and long links, and drop
# the URL prefix from pre-commit hook names.
body="$(jq -r '
unique_by([.dependencyName, .prevVersion, .newVersion])[]
| "- `\(.dependencyName | ltrimstr("https://github.com/"))` "
+ (.prevVersion | ltrimstr("v")) + " -> "
+ (.newVersion | ltrimstr("v"))
+ if .updateType == "version-update:semver-major"
then " (major)" else "" end
' <<<"$DEPENDENCIES")"
if [[ -z "$body" ]]; then
echo "Found no updated dependencies, so leaving it alone."
exit 0
fi
gh pr edit "$PR_URL" --repo "$GITHUB_REPOSITORY" --body "$body"