|
| 1 | +name: Auto approve dependabot pull request |
| 2 | +description: "Automatically approves dependabot pull requests." |
| 3 | +inputs: |
| 4 | + AUTOMERGE_APP_ID: |
| 5 | + description: "GitHub App ID for approving pull requests" |
| 6 | + required: true |
| 7 | + AUTOMERGE_PEM: |
| 8 | + description: "Private key for the GitHub App in PEM format" |
| 9 | + required: true |
| 10 | +runs: |
| 11 | + using: "composite" |
| 12 | + steps: |
| 13 | + - name: Create GitHub App Token |
| 14 | + uses: actions/create-github-app-token@f8d387b68d61c58ab83c6c016672934102569859 |
| 15 | + id: generate-token |
| 16 | + with: |
| 17 | + app-id: "${{ inputs.AUTOMERGE_APP_ID }}" |
| 18 | + private-key: "${{ inputs.AUTOMERGE_PEM }}" |
| 19 | + |
| 20 | + - name: Find eligible pull requests |
| 21 | + id: find-prs |
| 22 | + shell: bash |
| 23 | + env: |
| 24 | + REPO: ${{ github.repository }} |
| 25 | + GH_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 26 | + run: | |
| 27 | + set -euo pipefail |
| 28 | +
|
| 29 | + mapfile -t pr_numbers < <(gh pr list --repo "$REPO" --state open --json number --jq '.[].number') |
| 30 | +
|
| 31 | + eligible_pr_urls=() |
| 32 | +
|
| 33 | + for pr_number in "${pr_numbers[@]}"; do |
| 34 | + read -r pr_url head_sha < <(gh pr view "$pr_number" --repo "$REPO" --json url,headRefOid --jq '[.url, .headRefOid] | @tsv') |
| 35 | + read -r author_login is_verified is_cross_repository < <(gh api "/repos/$REPO/commits/$head_sha" --jq '[.author.login // "", (.commit.verification.verified // false), (.commit.verification.reason // "")] | @tsv') |
| 36 | +
|
| 37 | + if [[ "$is_cross_repository" == "false" ]] && [[ "$is_verified" == "true" ]] && [[ "$author_login" == "dependabot[bot]" || "$author_login" == "eps-create-pull-request[bot]" ]]; then |
| 38 | + eligible_pr_urls+=("$pr_url") |
| 39 | + fi |
| 40 | + done |
| 41 | +
|
| 42 | + echo "Found ${#eligible_pr_urls[@]} eligible pull request(s) for approval and merging." |
| 43 | + printf '%s\n' "${eligible_pr_urls[@]}" |
| 44 | +
|
| 45 | + { |
| 46 | + echo "pr_urls<<EOF" |
| 47 | + printf '%s\n' "${eligible_pr_urls[@]}" |
| 48 | + echo "EOF" |
| 49 | + } >> "$GITHUB_OUTPUT" |
| 50 | +
|
| 51 | + - name: Approve and merge updates |
| 52 | + shell: bash |
| 53 | + run: | |
| 54 | + set -euo pipefail |
| 55 | +
|
| 56 | + while IFS= read -r PR_URL; do |
| 57 | + [[ -z "$PR_URL" ]] && continue |
| 58 | +
|
| 59 | + gh pr review "$PR_URL" --approve -b "I'm **approving** this pull request" |
| 60 | + gh pr merge --auto --squash "$PR_URL" |
| 61 | + done <<< "${PR_URLS}" |
| 62 | + env: |
| 63 | + GH_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 64 | + PR_URLS: ${{ steps.find-prs.outputs.pr_urls }} |
0 commit comments