-
Notifications
You must be signed in to change notification settings - Fork 7
Conditionally auto-approve dependabot PRs #1117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
52c5050
Conditionally auto-approve dependabot PRs
cadmiumcat 5fb02d0
Use multiple jobs
cadmiumcat c0b9896
Add missing output on `validate_this_is_an_allowed_dependency`
sarahseewhy ebef87d
Uncomment automerge
sarahseewhy ea3f00a
Add step to wait for passing checks
sarahseewhy 9becaa6
Incorporate PR comments
sarahseewhy aa794d0
Add workflow linter
sarahseewhy 2c9a215
Address actionlint linting issues
sarahseewhy cfc05e0
Limit permissions and allowed package updates
cadmiumcat 515f8b8
Don't wait for checks if the dependency is not allowed
cadmiumcat e5bbfb4
Fix review apps action
cadmiumcat 6591fcb
Use v1.7.7 of actionlint
cadmiumcat 9d1d759
Fix review apps action
cadmiumcat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| name: Dependabot auto-approve | ||
| on: pull_request | ||
| permissions: | ||
| pull-requests: read | ||
| jobs: | ||
| validate_dependabot_opened_this_PR: | ||
| runs-on: ubuntu-latest | ||
| if: github.event.pull_request.user.login == 'dependabot[bot]' | ||
| steps: | ||
| - run: echo "This PR was raised by Dependabot" | ||
| validate_this_is_an_allowed_dependency: | ||
| needs: validate_dependabot_opened_this_PR | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| is_allowed_dependency: ${{ steps.check_if_allowed_dependency.outputs.is_allowed_dependency }} | ||
| steps: | ||
| - name: Dependabot metadata | ||
| id: dependabot-metadata | ||
| uses: dependabot/fetch-metadata@v2 | ||
| - if: | | ||
| contains(steps.dependabot-metadata.outputs.package-ecosystem, 'bundler') && | ||
| (steps.dependabot-metadata.outputs.update-type == 'version-update:semver-patch') | ||
| id: check_if_allowed_dependency | ||
| run: | | ||
| echo "is_allowed_dependency=1" >> "$GITHUB_OUTPUT" | ||
| wait_for_checks: | ||
| runs-on: ubuntu-latest | ||
| needs: [ validate_dependabot_opened_this_PR, validate_this_is_an_allowed_dependency ] | ||
| if: ${{ needs.validate_this_is_an_allowed_dependency.outputs.is_allowed_dependency == 1 }} | ||
| steps: | ||
| - name: Wait for required checks to pass | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| REPO: ${{ github.repository }} | ||
| SHA: ${{ github.event.pull_request.head.sha }} | ||
| run: | | ||
| echo "Waiting for all required checks to pass on commit $SHA..." | ||
|
|
||
| MAX_RETRIES=30 | ||
| RETRY_DELAY=10 | ||
| ATTEMPT=0 | ||
|
|
||
| while [ $ATTEMPT -lt $MAX_RETRIES ]; do | ||
| echo "Attempt $((ATTEMPT+1))/$MAX_RETRIES" | ||
|
|
||
| # Get all check runs for the commit | ||
| RESPONSE=$(curl -s -H "Authorization: token $GH_TOKEN" \ | ||
| "https://api.github.com/repos/$REPO/commits/$SHA/check-runs") | ||
|
|
||
| # Exclude self (wait_for_checks) | ||
| EXCLUDED_CHECK="wait_for_checks" | ||
|
|
||
| echo "Ignoring ${EXCLUDED_CHECK}" | ||
|
|
||
| # Extract conclusions | ||
| CONCLUSIONS=$(echo "$RESPONSE" | jq -r '.check_runs[] | select(.name != "wait_for_checks") | .name + ":" + (.status + "/" + (.conclusion // "none"))') | ||
|
|
||
| echo "$CONCLUSIONS" | ||
|
|
||
| # Are there any in_progress or queued? | ||
| PENDING=$(echo "$RESPONSE" | jq -r --arg exclude "$EXCLUDED_CHECK" '[.check_runs[] | select(.name != $exclude and .status != "completed")] | length') | ||
|
|
||
| # Are all completed and successful? | ||
| FAILED=$(echo "$RESPONSE" | jq -r --arg exclude "$EXCLUDED_CHECK" '[.check_runs[] | select(.name != $exclude and .status == "completed" and .conclusion != "success")] | length') | ||
|
|
||
| if [ "$PENDING" -eq 0 ] && [ "$FAILED" -eq 0 ]; then | ||
| echo "All checks completed successfully." | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [ "$FAILED" -gt 0 ]; then | ||
| echo "One or more checks failed." | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Some checks still pending. Waiting $RETRY_DELAY seconds..." | ||
| sleep $RETRY_DELAY | ||
| ATTEMPT=$((ATTEMPT + 1)) | ||
| done | ||
|
|
||
| echo "Timed out waiting for checks to complete." | ||
| exit 1 | ||
| auto_approve_and_merge: | ||
| permissions: | ||
| pull-requests: write | ||
| contents: write | ||
| runs-on: ubuntu-latest | ||
| needs: [validate_dependabot_opened_this_PR, validate_this_is_an_allowed_dependency, wait_for_checks] | ||
| if: ${{ needs.validate_this_is_an_allowed_dependency.outputs.is_allowed_dependency == 1 }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Approve a PR if not already approved | ||
| run: | | ||
| # Sets the upstream metadata for `gh pr status` | ||
| gh pr checkout "${PR_URL}" | ||
|
|
||
| if [ "$(gh pr status --json reviewDecision -q .currentBranch.reviewDecision)" != "APPROVED" ]; | ||
| then | ||
| gh pr review --approve "${PR_URL}" | ||
| else | ||
| echo "PR already approved, skipping additional approvals to minimize emails/notification noise."; | ||
| fi | ||
| env: | ||
| PR_URL: ${{github.event.pull_request.html_url}} | ||
| GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} | ||
| - name: Enable auto-merge for Dependabot PRs | ||
| run: gh pr merge --auto --merge "$PR_URL" | ||
| env: | ||
| PR_URL: ${{github.event.pull_request.html_url}} | ||
| GH_TOKEN: ${{secrets.GITHUB_TOKEN}} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| name: "Lint Workflows" | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [main] | ||
| permissions: | ||
| contents: read | ||
| jobs: | ||
| actionlint: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Download actionlint | ||
| id: get_actionlint | ||
| run: | | ||
| curl -L "https://github.com/rhysd/actionlint/releases/download/v1.7.7/actionlint_1.7.7_linux_amd64.tar.gz" | tar -xvz actionlint | ||
| chmod +x actionlint | ||
| sudo mv actionlint /usr/local/bin/actionlint | ||
| shell: bash | ||
| - name: Check workflow files | ||
| run: actionlint -color | ||
| shell: bash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.