Skip to content

Commit 994e37c

Browse files
authored
ci: enable Automatic Merge of Approved Pull Requests (#306)
Add automation that will merge approved PRs with passed CI checks automatically. It uses following GitHub Actions: * https://github.com/redhat-plumbers-in-action/gather-pull-request-metadata - to gather important data about PRs (used in following actions) * https://github.com/redhat-plumbers-in-action/pull-request-validator - to check if PR is approved and has passed CI checks, and more * https://github.com/redhat-plumbers-in-action/auto-merge - to merge approved PRs * https://github.com/redhat-plumbers-in-action/issue-commentator - to comment on PR with status of PR and merge status
1 parent f3d5723 commit 994e37c

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

.github/auto-merge.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target-branch': ['master']
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Auto Merge On Demand
2+
on:
3+
schedule:
4+
# Workflow runs every 45 minutes
5+
- cron: '*/45 * * * *'
6+
workflow_dispatch:
7+
inputs:
8+
pr-number:
9+
description: 'Pull Request number/s ; when not provided, the workflow will run for all open PRs'
10+
required: true
11+
default: '0'
12+
13+
permissions:
14+
contents: read
15+
16+
jobs:
17+
# Get all open PRs
18+
gather-pull-requests:
19+
if: github.repository_owner == 'sclorg'
20+
runs-on: ubuntu-latest
21+
22+
outputs:
23+
pr-numbers: ${{ steps.get-pr-numbers.outputs.result }}
24+
pr-numbers-manual: ${{ steps.parse-manual-input.outputs.result }}
25+
26+
steps:
27+
- id: get-pr-numbers
28+
if: inputs.pr-number == '0'
29+
name: Get all open PRs
30+
uses: actions/github-script@v6
31+
with:
32+
# !FIXME: this is not working if there is more than 100 PRs opened
33+
script: |
34+
const { data: pullRequests } = await github.rest.pulls.list({
35+
owner: context.repo.owner,
36+
repo: context.repo.repo,
37+
state: 'open',
38+
per_page: 100
39+
});
40+
return pullRequests.map(pr => pr.number);
41+
42+
- id: parse-manual-input
43+
if: inputs.pr-number != '0'
44+
name: Parse manual input
45+
run: |
46+
# shellcheck disable=SC2086
47+
echo "result="[ ${{ inputs.pr-number }} ]"" >> $GITHUB_OUTPUT
48+
shell: bash
49+
50+
validate-pr:
51+
name: 'Validation of Pull Request #${{ matrix.pr-number }}'
52+
needs: [ gather-pull-requests ]
53+
runs-on: ubuntu-latest
54+
55+
strategy:
56+
fail-fast: false
57+
matrix:
58+
pr-number: ${{ inputs.pr-number == 0 && fromJSON(needs.gather-pull-requests.outputs.pr-numbers) || fromJSON(needs.gather-pull-requests.outputs.pr-numbers-manual) }}
59+
60+
permissions:
61+
# required for merging PRs
62+
contents: write
63+
# required for PR comments and setting labels
64+
pull-requests: write
65+
66+
steps:
67+
- id: metadata
68+
name: Gather Pull Request Metadata
69+
uses: redhat-plumbers-in-action/gather-pull-request-metadata@v1
70+
with:
71+
pr-number: ${{ matrix.pr-number }}
72+
73+
- id: pull-request-validator
74+
name: Pull Request Validator
75+
uses: redhat-plumbers-in-action/pull-request-validator@v2
76+
with:
77+
pr-metadata: ${{ steps.metadata.outputs.metadata }}
78+
token: ${{ secrets.GITHUB_TOKEN }}
79+
80+
- id: auto-merge
81+
name: Auto Merge
82+
uses: redhat-plumbers-in-action/auto-merge@v2
83+
with:
84+
pr-metadata: ${{ steps.metadata.outputs.metadata }}
85+
token: ${{ secrets.GITHUB_TOKEN }}
86+
87+
- if: ${{ !cancelled() }}
88+
name: Show results in PR comment
89+
uses: redhat-plumbers-in-action/issue-commentator@v1
90+
with:
91+
issue: ${{ fromJSON(steps.metadata.outputs.metadata).number }}
92+
message: |
93+
${{ steps.pull-request-validator.outputs.status && steps.pull-request-validator.outputs.status || '' }}
94+
${{ steps.auto-merge.outputs.status && steps.auto-merge.outputs.status || '' }}
95+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/auto-merge.yml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Auto Merge
2+
on:
3+
workflow_run:
4+
workflows: [ Gather Pull Request Metadata ]
5+
types:
6+
- completed
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
download-metadata:
13+
if: >
14+
github.event.workflow_run.event == 'pull_request' &&
15+
github.event.workflow_run.conclusion == 'success'
16+
runs-on: ubuntu-latest
17+
18+
outputs:
19+
pr-metadata: ${{ steps.Artifact.outputs.pr-metadata-json }}
20+
21+
steps:
22+
- id: Artifact
23+
name: Download Artifact
24+
uses: redhat-plumbers-in-action/download-artifact@v1
25+
with:
26+
name: pr-metadata
27+
28+
auto-merge:
29+
needs: [ download-metadata ]
30+
runs-on: ubuntu-latest
31+
32+
permissions:
33+
# required for ability to merge Pull Request
34+
contents: write
35+
# required for setting labels
36+
pull-requests: write
37+
38+
steps:
39+
- id: pull-request-validator
40+
name: Pull Request Validator
41+
uses: redhat-plumbers-in-action/pull-request-validator@v2
42+
with:
43+
pr-metadata: ${{ needs.download-metadata.outputs.pr-metadata }}
44+
token: ${{ secrets.GITHUB_TOKEN }}
45+
46+
- id: auto-merge
47+
name: Auto Merge
48+
uses: redhat-plumbers-in-action/auto-merge@v2
49+
with:
50+
pr-metadata: ${{ needs.download-metadata.outputs.pr-metadata }}
51+
token: ${{ secrets.GITHUB_TOKEN }}
52+
53+
- if: ${{ !cancelled() }}
54+
name: Show results in PR comment
55+
uses: redhat-plumbers-in-action/issue-commentator@v1
56+
with:
57+
issue: ${{ fromJSON(needs.download-metadata.outputs.pr-metadata).number }}
58+
message: |
59+
${{ steps.pull-request-validator.outputs.status && steps.pull-request-validator.outputs.status || '' }}
60+
${{ steps.auto-merge.outputs.status && steps.auto-merge.outputs.status || '' }}
61+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/pr-metadata.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Gather Pull Request Metadata
2+
on:
3+
pull_request:
4+
types: [ opened, reopened, synchronize ]
5+
branches: [ master ]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
gather-metadata:
12+
if: github.repository_owner == 'sclorg'
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Repository checkout
17+
uses: actions/checkout@v4
18+
19+
- id: Metadata
20+
name: Gather Pull Request Metadata
21+
uses: redhat-plumbers-in-action/gather-pull-request-metadata@v1
22+
23+
- name: Upload artifact with gathered metadata
24+
uses: actions/upload-artifact@v4
25+
with:
26+
name: pr-metadata
27+
path: ${{ steps.Metadata.outputs.metadata-file }}

0 commit comments

Comments
 (0)