-
-
Notifications
You must be signed in to change notification settings - Fork 1
81 lines (70 loc) · 2.86 KB
/
Copy pathensure-pr-source-develop.yml
File metadata and controls
81 lines (70 loc) · 2.86 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
name: pr-source-enforcer
# Runs when a PR targets `main`
on:
pull_request:
types: [opened, reopened, synchronize, edited]
branches:
- main
permissions:
contents: read
pull-requests: write
jobs:
enforce_pr_source:
runs-on: ubuntu-latest
outputs:
allowed: ${{ steps.check.outputs.allowed }}
steps:
- name: Set up shell
run: echo "starting pr-source-enforcer"
- name: Read inputs
id: check
env:
ALLOWED: ${{ secrets.ALLOWED_MERGERS }}
HEAD_REF: ${{ github.event.pull_request.head.ref }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
TARGET_BRANCH: ${{ github.event.pull_request.base.ref }}
run: |
# Allowed mergers come from a repo secret, comma-separated.
# Set this secret in your repo settings: Settings -> Secrets -> Actions -> New repository secret
# Example value: ayoub,my-org-release-bot
echo "HEAD_REF=$HEAD_REF"
echo "PR_AUTHOR=$PR_AUTHOR"
echo "TARGET_BRANCH=$TARGET_BRANCH"
# Normalize values (lowercase) for comparison
head_lc="$(echo "$HEAD_REF" | tr '[:upper:]' '[:lower:]')"
author_lc="$(echo "$PR_AUTHOR" | tr '[:upper:]' '[:lower:]')"
allowed_lc="$(echo "$ALLOWED" | tr '[:upper:]' '[:lower:]')"
# Default to empty allowed list if secret missing
if [ -z "$allowed_lc" ]; then
echo "Warning: ALLOWED_MERGERS secret is empty or missing. No users will be allowed as bypassers."
fi
# Check conditions: allowed if head is 'develop' OR author is in allowed list
allowed="false"
if [ "$head_lc" = "develop" ]; then
allowed="true"
else
# iterate allowed list
IFS=',' read -ra arr <<< "$allowed_lc"
for u in "${arr[@]}"; do
u_trim="$(echo "$u" | xargs)" # trim spaces
if [ -n "$u_trim" ] && [ "$u_trim" = "$author_lc" ]; then
allowed="true"
break
fi
done
fi
echo "allowed=$allowed"
echo "allowed=$allowed" >> "$GITHUB_OUTPUT"
- name: Fail if not allowed
if: steps.check.outputs.allowed != 'true'
env:
HEAD_REF: ${{ github.event.pull_request.head.ref }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
run: |
echo "ERROR: PR targeting 'main' is not allowed. Head branch is '$HEAD_REF' and PR author is '$PR_AUTHOR'."
echo "Only PRs whose head branch is 'develop' or PRs authored by an allowed merger (repo secret ALLOWED_MERGERS) may target 'main'."
exit 1
- name: Success note
if: steps.check.outputs.allowed == 'true'
run: |
echo "OK: PR allowed to target main (head branch is develop or author is allowed)."