Skip to content

Commit 9e4865a

Browse files
authored
feat: label based PR checks, auto merging, conditional manual check (#783)
1 parent 4bc33ff commit 9e4865a

File tree

3 files changed

+125
-47
lines changed

3 files changed

+125
-47
lines changed

.github/workflows/build-validation.yml

-47
This file was deleted.

.github/workflows/manual-build-validate.yml

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ jobs:
1010
# Check if PR, prevent running on forks and trigger comment
1111
if: ${{ github.event.issue.pull_request && github.repository_owner == 'flybywiresim' && github.event.comment.body == 'check build' }}
1212
steps:
13+
- name: Check if user is member of organization and maintainer
14+
id: check-permissions
15+
uses: actions/github-script@v6
16+
with:
17+
result-encoding: string
18+
script: |
19+
const { owner } = context.repo;
20+
const { data: members } = await github.rest.orgs.listMembers({
21+
org: owner,
22+
per_page: 100,
23+
});
24+
const { data: isOrgMember } = members.find(member => member.login === context.payload.comment.user.login) || { data: false };
25+
const { data: isMaintainer } = await github.rest.repos.getCollaboratorPermissionLevel({
26+
owner: owner,
27+
repo: context.repo.repo,
28+
username: context.payload.comment.user.login,
29+
});
30+
return isOrgMember === true && isMaintainer.permission === 'maintain'
1331
- name: React to comment
1432
uses: actions/github-script@v6
1533
with:

.github/workflows/pr-checks.yml

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: Build Checks and Auto Merge
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, ready_for_review, synchronize, labeled, unlabeled]
6+
7+
jobs:
8+
verify-labels:
9+
runs-on: ubuntu-latest
10+
if: ${{ github.event.pull_request.draft == false && github.repository_owner == 'flybywiresim' }} # Prevent running on forks
11+
permissions:
12+
issues: write
13+
pull-requests: write
14+
steps:
15+
- name: Assign Label
16+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'Review Required') }}
17+
uses: actions-ecosystem/action-add-labels@v1
18+
with:
19+
labels: 'Review Required'
20+
- name: Approved and Review Required Logic
21+
if: ${{ contains(github.event.pull_request.labels.*.name, 'Approved') && contains(github.event.pull_request.labels.*.name, 'Review Required') }}
22+
run: echo "RUN_REMOVE_LABEL=true" >> $GITHUB_ENV
23+
24+
- name: Remove Label
25+
if: ${{ env.RUN_REMOVE_LABEL == 'true' }}
26+
uses: actions-ecosystem/action-remove-labels@v1
27+
with:
28+
labels: Review Required
29+
id: remove-label
30+
31+
- name: Remove Label Failed # Fails job if the label cannot be removed
32+
if: ${{ steps.remove-label.outcome == 'failure' }}
33+
run: echo "Remove Label step failed" && exit 1
34+
35+
- name: Remove Label Succeeded
36+
if: ${{ steps.remove-label.outcome == 'success' && steps.remove-label.outputs.code == '0' }}
37+
run: echo "Remove Label step succeeded and returned code 0"
38+
39+
- name: Comment on PR # Checks for Reviewed Required label and comments on PR
40+
id: check-labels
41+
uses: mheap/github-action-required-labels@v4
42+
with:
43+
mode: exactly
44+
count: 0
45+
labels: "Review Required"
46+
add_comment: true
47+
message: "This PR is being prevented from merging because {{ provided }}. Use the Approved label to run build validation and auto merge the PR."
48+
49+
validate:
50+
needs: verify-labels
51+
if: ${{ success() && contains(github.event.pull_request.labels.*.name, 'Approved') }}
52+
runs-on: ubuntu-latest
53+
steps:
54+
- name: Get PR SHA
55+
id: sha
56+
uses: actions/github-script@v6
57+
with:
58+
result-encoding: string
59+
script: |
60+
const { owner, repo, number } = context.issue;
61+
const pr = await github.rest.pulls.get({
62+
owner,
63+
repo,
64+
pull_number: number,
65+
});
66+
return pr.data.head.sha
67+
- uses: actions/checkout@v3
68+
with:
69+
ref: ${{ steps.sha.outputs.result }}
70+
fetch-depth: 0
71+
- name: Make Build
72+
uses: actions/setup-python@v4
73+
with:
74+
python-version: 3.x
75+
- run: |
76+
pip install -r requirements.txt
77+
mkdocs build -s
78+
- name: Message failure
79+
if: ${{ failure() }}
80+
uses: actions/github-script@v6
81+
with:
82+
script: |
83+
github.rest.issues.createComment({
84+
issue_number: context.issue.number,
85+
owner: context.repo.owner,
86+
repo: context.repo.repo,
87+
body: 'Strict Build Failed! ❌ - Please checkout this PR locally to fix the issue before merging or see checks.',
88+
});
89+
- name: Automatically pr
90+
if: ${{ success() }}
91+
run: gh pr merge --squash --auto --subject "${{ github.event.pull_request.title }}" "${{ github.event.pull_request.number }}"
92+
env:
93+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
94+
95+
failed-validation: # This job will run if the validate job fails and reset the labels
96+
needs: validate
97+
if: ${{ failure() && contains(github.event.pull_request.labels.*.name, 'Approved') }}
98+
runs-on: ubuntu-latest
99+
steps:
100+
- name: Remove Label
101+
uses: actions-ecosystem/action-remove-labels@v1
102+
with:
103+
labels: Approved
104+
- name: Assign Label
105+
uses: actions-ecosystem/action-add-labels@v1
106+
with:
107+
labels: 'Review Required'

0 commit comments

Comments
 (0)