-
Notifications
You must be signed in to change notification settings - Fork 17.5k
126 lines (101 loc) · 4.66 KB
/
pre-review.yml
File metadata and controls
126 lines (101 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
name: Pre-review
on:
pull_request_target:
branches: [ master ]
types: [opened, synchronize, reopened, closed]
pull_request_review:
types: [submitted]
permissions:
contents: read
pull-requests: write
issues: write
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
pre-review:
if: github.event_name == 'pull_request' || github.event_name == 'pull_request_target'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
- name: Install Python 3
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Fetch branch
run: git fetch origin ${{ github.event.pull_request.base.ref }}
- name: Collect information
run: |
setEnv() { echo "$1=${!1}" >> $GITHUB_ENV; }
CONTRIBUTOR=${{ github.event.pull_request.user.login }}
setEnv "CONTRIBUTOR"
BASE_REPO=${{ github.event.pull_request.base.repo.full_name }}
setEnv "BASE_REPO"
HEAD_REF=${{ github.event.pull_request.head.ref }}
setEnv "HEAD_REF"
PR_NUMBER=${{ github.event.pull_request.number }}
setEnv "PR_NUMBER"
echo "PR_NUMBER: $PR_NUMBER"
MERGE_BASE=$(git merge-base origin/${{ github.event.pull_request.base.ref }} HEAD)
setEnv "MERGE_BASE"
CHANGED_FILES=$(gh pr view $PR_NUMBER -R "$BASE_REPO" --json files -q '.files[].path' | paste -sd '\\n' -)
echo "CHANGED_FILES<<EOF" >> $GITHUB_ENV
printf '%s\n' "$CHANGED_FILES" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Run "preReview" script
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
BASE_REPO=${{ github.event.pull_request.base.repo.full_name }}
REVIEW_MESSAGE=$(python pre_review.py "$CHANGED_FILES" "$CONTRIBUTOR")
echo "REVIEW_MESSAGE<<EOF" >> $GITHUB_ENV
echo "$REVIEW_MESSAGE" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "===== REVIEW_MESSAGE ====="
echo "$REVIEW_MESSAGE"
echo "==========================="
# Attempt to comment (will skip for forks)
gh pr comment "$PR_NUMBER" --body "$REVIEW_MESSAGE" -R "$BASE_REPO"
# --------------------------------------------------------------------------
# LABELS
# --------------------------------------------------------------------------
# --- "Changes Requested" or "Awaiting Maintainer Validation" labels ---
if echo "$REVIEW_MESSAGE" | grep -Eq '\- \[ \]'; then
gh pr edit "$PR_NUMBER" --add-label "Changes Requested" -R "$BASE_REPO"
gh pr edit "$PR_NUMBER" --remove-label "Awaiting Maintainer Validation" -R "$BASE_REPO"
else
gh pr edit "$PR_NUMBER" --add-label "Awaiting Maintainer Validation" -R "$BASE_REPO"
gh pr edit "$PR_NUMBER" --remove-label "Changes Requested" -R "$BASE_REPO"
fi
# --- Invalid label ---
if echo "$REVIEW_MESSAGE" | grep -qi 'Missing required contribution'; then
gh pr edit "$PR_NUMBER" --add-label "invalid" -R "$BASE_REPO"
else
gh pr edit "$PR_NUMBER" --remove-label "invalid" -R "$BASE_REPO"
fi
# --- Conflict present label ---
if echo "$REVIEW_MESSAGE" | grep -qi 'archived'; then
gh pr edit "$PR_NUMBER" --add-label "Conflict present" -R "$BASE_REPO"
else
gh pr edit "$PR_NUMBER" --remove-label "Conflict present" -R "$BASE_REPO"
fi
env:
CHANGED_FILES: ${{ env.CHANGED_FILES }}
CONTRIBUTOR: ${{ env.CONTRIBUTOR }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
remove-awaiting-on-merge:
if: github.event_name == 'pull_request_target' && github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- name: Remove 'Awaiting Maintainer Validation' on merged PR
run: |
if [ "${{ github.event.pull_request.merged }}" == "true" ]; then
echo "PR merged — removing 'Awaiting Maintainer Validation' label"
gh pr edit ${{ github.event.pull_request.number }} --remove-label "Awaiting Maintainer Validation" -R "${{ github.event.pull_request.base.repo.full_name }}"
else
echo "PR closed but not merged — skipping label removal"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}