-
Notifications
You must be signed in to change notification settings - Fork 0
105 lines (92 loc) · 3.81 KB
/
auto-merge.yaml
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
name: Auto Merge Approved PR
on:
pull_request_review:
types:
- submitted
jobs:
auto_merge:
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.base.ref == 'main' && github.event.review.state == 'approved' }}
outputs:
pr_number: ${{ steps.auto_merge_pr.outputs.pr_number }}
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
with:
token: ${{ secrets.MY_PAT }}
fetch-depth: 0
- name: Check GitHub CLI version
run: gh --version
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '14'
- name: Set up Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Add Approved label
run: |
pr_number=$(gh pr view ${{ github.event.pull_request.number }} --json number --jq '.number')
gh pr edit $pr_number --add-label "approved"
env:
GH_TOKEN: ${{ secrets.MY_PAT }}
- name: Auto Merge Approved PR
id: auto_merge_pr
run: |
pr_number=${{ github.event.pull_request.number }}
review_users=$(gh pr view ${pr_number} --json reviews | jq -r '.reviews[].author.login')
# Fetch CODEOWNERS file content
CODEOWNERS_CONTENT=$(curl -s "https://api.github.com/repos${{ github.owner }}/${{ github.repository }}/contents/.github/CODEOWNERS" | jq -r '.content' | base64 -d)
if [[ -n "$review_users" ]]; then
for user in $review_users; do
echo "Checking code owner for reviewer: $user"
# Check if the reviewer is a code owner
if echo "$CODEOWNERS_CONTENT" | grep -q "$user"; then
echo "PR has been approved by code owner, merging..."
gh pr merge $pr_number --auto --squash --delete-branch
break # Exit the loop if any reviewer is a code owner
else
echo "Reviewer $user is not a code owner."
fi
done
fi
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.MY_PAT }}
rebase_staging:
needs: auto_merge
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
with:
token: ${{ secrets.MY_PAT }}
fetch-depth: 0
- name: Set up Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Check if PR is merged
id: merge_status
run: |
PR_NUMBER="${{ needs.auto_merge.outputs.pr_number }}"
MERGED=$(curl -s -X GET -H "Authorization: Bearer ${{ secrets.MY_PAT }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER}" | \
jq -r '.merged')
if [ "$MERGED" == "true" ]; then
echo "PR #$PR_NUMBER has been merged."
else
echo "PR #$PR_NUMBER was closed without merging or not closed yet."
fi
echo "status=$MERGED" >> $GITHUB_OUTPUT
- name: Rebase "staging" onto "main"
if: steps.merge_status.outputs.status == 'true'
run: |
git checkout main
git pull origin main # Make sure your local 'main' is up to date with the remote
git checkout staging
git fetch origin # Make sure your remote tracking branches are up to date
git branch --set-upstream-to=origin/staging staging # Set up tracking branch
git rebase main
git push origin staging --force