|
| 1 | +name: dependabot-sync-bazel-backfill |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + backfill_open_prs: |
| 7 | + description: "Sync all currently open Dependabot pom.xml PRs" |
| 8 | + required: true |
| 9 | + type: boolean |
| 10 | + default: false |
| 11 | + |
| 12 | +jobs: |
| 13 | + sync-open-dependabot-prs: |
| 14 | + if: >- |
| 15 | + github.event_name == 'workflow_dispatch' && |
| 16 | + github.event.inputs.backfill_open_prs == 'true' |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + pull-requests: read |
| 21 | + env: |
| 22 | + BAZELISK_SKIP_VERSION_CHECK: "1" |
| 23 | + |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 |
| 26 | + |
| 27 | + - name: Set up Java 17 |
| 28 | + uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5 |
| 29 | + with: |
| 30 | + distribution: temurin |
| 31 | + java-version: 17 |
| 32 | + |
| 33 | + - name: Install Bazelisk |
| 34 | + run: | |
| 35 | + mkdir -p "$HOME/.local/bin" |
| 36 | + curl -fsSL \ |
| 37 | + https://github.com/bazelbuild/bazelisk/releases/download/v1.24.1/bazelisk-linux-amd64 \ |
| 38 | + -o "$HOME/.local/bin/bazel" |
| 39 | + chmod +x "$HOME/.local/bin/bazel" |
| 40 | + echo "$HOME/.local/bin" >> "$GITHUB_PATH" |
| 41 | +
|
| 42 | + - name: Preserve Bazel sync script |
| 43 | + run: | |
| 44 | + cp scripts/sync_bazel_dependencies.py "${RUNNER_TEMP}/sync_bazel_dependencies.py" |
| 45 | +
|
| 46 | + - name: Sync all open Dependabot pom.xml PRs |
| 47 | + shell: bash |
| 48 | + env: |
| 49 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 50 | + run: | |
| 51 | + set -euo pipefail |
| 52 | +
|
| 53 | + git config user.name "github-actions[bot]" |
| 54 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 55 | +
|
| 56 | + api="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}" |
| 57 | + open_prs_json='[]' |
| 58 | + page=1 |
| 59 | + while :; do |
| 60 | + page_json="$(curl -fsSL \ |
| 61 | + -u "x-access-token:${GITHUB_TOKEN}" \ |
| 62 | + -H "Accept: application/vnd.github+json" \ |
| 63 | + "${api}/pulls?state=open&per_page=100&page=${page}")" |
| 64 | + open_prs_json="$(jq -s 'add' \ |
| 65 | + <(printf '%s' "${open_prs_json}") \ |
| 66 | + <(printf '%s' "${page_json}"))" |
| 67 | + if [[ "$(jq 'length' <<< "${page_json}")" -lt 100 ]]; then |
| 68 | + break |
| 69 | + fi |
| 70 | + page=$((page + 1)) |
| 71 | + done |
| 72 | +
|
| 73 | + while IFS=$'\t' read -r number head_ref head_repo base_ref author; do |
| 74 | + if [[ "${author}" != "dependabot[bot]" ]]; then |
| 75 | + continue |
| 76 | + fi |
| 77 | + if [[ "${base_ref}" != "master" ]]; then |
| 78 | + continue |
| 79 | + fi |
| 80 | + if [[ "${head_repo}" != "${GITHUB_REPOSITORY}" ]]; then |
| 81 | + echo "Skipping #${number}; head repo ${head_repo} is not ${GITHUB_REPOSITORY}." |
| 82 | + continue |
| 83 | + fi |
| 84 | + if [[ "${head_ref}" != "dependabot/"* ]]; then |
| 85 | + echo "Skipping #${number}; branch ${head_ref} is not a Dependabot branch." |
| 86 | + continue |
| 87 | + fi |
| 88 | +
|
| 89 | + pr_files_json="$(curl -fsSL \ |
| 90 | + -u "x-access-token:${GITHUB_TOKEN}" \ |
| 91 | + -H "Accept: application/vnd.github+json" \ |
| 92 | + "${api}/pulls/${number}/files?per_page=100")" |
| 93 | + if ! jq -e '.[] | select(.filename == "pom.xml")' >/dev/null <<< "${pr_files_json}"; then |
| 94 | + echo "Skipping #${number}; root pom.xml was not changed." |
| 95 | + continue |
| 96 | + fi |
| 97 | +
|
| 98 | + echo "Processing Dependabot PR #${number} (${head_ref})" |
| 99 | + git fetch origin "${head_ref}" |
| 100 | + git checkout -B "dependabot-sync-work" "origin/${head_ref}" |
| 101 | + # Ensure each PR branch starts from a clean checkout before sync. |
| 102 | + git reset --hard |
| 103 | + git clean -fd |
| 104 | +
|
| 105 | + python3 "${RUNNER_TEMP}/sync_bazel_dependencies.py" --root "$PWD" |
| 106 | + REPIN=1 bazel run @maven//:pin |
| 107 | +
|
| 108 | + if git diff --quiet -- MODULE.bazel maven_install.json; then |
| 109 | + echo "No Bazel dependency changes needed for #${number}." |
| 110 | + continue |
| 111 | + fi |
| 112 | +
|
| 113 | + git add MODULE.bazel maven_install.json |
| 114 | + git commit -m "Sync Bazel dependencies for Dependabot update (#${number})" |
| 115 | + git push origin "HEAD:${head_ref}" |
| 116 | + done < <(jq -r '.[] | [.number, .head.ref, .head.repo.full_name, .base.ref, .user.login] | @tsv' <<< "${open_prs_json}") |
0 commit comments