auto-merge dependabot pull requests when tests pass

This commit is contained in:
burnettk 2022-06-10 17:42:19 -04:00
parent 85172c6e9b
commit 0006182ead
3 changed files with 68 additions and 12 deletions

View File

@ -2,27 +2,71 @@ name: Dependabot auto-merge
on: on:
workflow_run: workflow_run:
workflows: ["Tests"] workflows: ["Tests"]
# completed does not mean success of Tests workflow. see below checking github.event.workflow_run.conclusion
types: types:
- completed - completed
# workflow_call is used to indicate that a workflow can be called by another workflow. When a workflow is triggered with the workflow_call event, the event payload in the called workflow is the same event payload from the calling workflow. For more information see, "Reusing workflows."
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
# maybe hook into this instead of workflow_run:
# on:
# pull_request:
# pull_request_target:
# types: [labeled]
permissions: permissions:
contents: write contents: write
jobs: jobs:
# print the context for debugging in case a job gets skipped
printJob:
name: Print event
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: |
echo "$GITHUB_CONTEXT"
dependabot: dependabot:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' && github.event.workflow_run.conclusion == 'success' && github.event_name == 'pull_request' }} if: ${{ github.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}
steps: steps:
- name: Dependabot metadata - name: Development Code
id: metadata uses: actions/checkout@v3
uses: dependabot/fetch-metadata@v1.1.1
###### GET PR NUMBER
# we saved the pr_number in tests.yml. fetch it so we can merge the correct PR.
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
- name: 'Download artifact'
uses: actions/github-script@v6
with: with:
github-token: "${{ secrets.GITHUB_TOKEN }}" script: |
- name: Enable auto-merge for Dependabot PRs let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
# if: ${{contains(steps.metadata.outputs.dependency-names, 'pytest') && steps.metadata.outputs.update-type == 'version-update:semver-patch'}} owner: context.repo.owner,
# if: ${{contains(steps.metadata.outputs.dependency-names, 'pytest')}} repo: context.repo.repo,
# ideally we auto-merge if all checks pass run_id: context.payload.workflow_run.id,
run: gh pr merge --auto --merge "$PR_URL" });
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr_number"
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_number.zip`, Buffer.from(download.data));
- name: 'Unzip artifact'
run: unzip pr_number.zip
###########
- name: print pr number
run: cat pr_number
- name: actually merge it
run: gh pr merge --auto --merge "$(cat pr_number)"
env: env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

View File

@ -4,7 +4,6 @@ on:
push: push:
branches: branches:
- main - main
- master
jobs: jobs:
labeler: labeler:

View File

@ -219,3 +219,16 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
# part about saving PR number and then using it from auto-merge-dependabot-prs from:
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
- name: Save PR number
if: ${{ github.event_name == 'pull_request' }}
env:
PR_NUMBER: ${{ github.event.number }}
run: |
mkdir -p ./pr
echo "$PR_NUMBER" > ./pr/pr_number
- uses: actions/upload-artifact@v3
with:
name: pr_number
path: pr/