-
Notifications
You must be signed in to change notification settings - Fork 0
208 lines (188 loc) · 8.62 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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 }}
pr_head_ref: ${{ steps.head_ref.outputs.pr_head_ref }}
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4
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@v4
with:
node-version: '20'
- 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: Output Head Ref
id: head_ref
run: |
echo "pr_head_ref=${{ github.event.pull_request.head.ref }}" >> $GITHUB_OUTPUT
- name: Extract SERVICE_REPO_NAME from head ref
id: extract_service_repo_name
continue-on-error: true
run: |
regex="^main-branch-update-from-(.*)-values$"
if [[ "${{ steps.head_ref.outputs.pr_head_ref }}" =~ $regex ]]; then
service_repo_name="${BASH_REMATCH[1]}"
echo "SERVICE REPO Name: $service_repo_name"
echo "service_repo_name=$service_repo_name" >> $GITHUB_OUTPUT
else
echo "Its not an image tag update."
exit 1
fi
- name: Parse reviewers mapping YAML
id: parse_reviewers_mapping
if: steps.extract_service_repo_name.outcome == 'success'
env:
service_repo_name: ${{ steps.extract_service_repo_name.outputs.service_repo_name }}
run: |
reviewers_mapping=$(cat reviewers-map.yaml | yq -r ".\"$service_repo_name\"[]" | tr '\n' ',' | sed 's/,$//')
echo "SERVICE REVIEWERS: $reviewers_mapping"
echo "reviewers_mapping=$reviewers_mapping" >> $GITHUB_OUTPUT
# - name: Test1
# run: |
# # Extract the PR number from the pull request
# pr_number=${{ github.event.pull_request.number }}
# checks_output=$(gh pr checks ${pr_number} --required)
# echo "$checks_output"
# shell: bash
# env:
# GH_TOKEN: ${{ secrets.MY_PAT }}
# - name: Test2
# run: |
# # Extract the PR number from the pull request
# pr_number=${{ github.event.pull_request.number }}
# # Get the required failed checks
# checks_output=$(gh pr checks ${pr_number} --required)
# echo "$checks_output"
# # Extract check name, status, and URL
# check_name=$(echo "$checks_output" | awk '{print $1}')
# check_status=$(echo "$checks_output" | awk '{print $2}')
# check_url=$(echo "$checks_output" | awk '{print $4}')
# echo "$check_name, $check_status, $check_url"
# env:
# GH_TOKEN: ${{ secrets.MY_PAT }}
# - name: Add PR Comment with approval dismissal
# id: check_required_checks
# run: |
# # Extract the PR number from the pull request
# pr_number=${{ github.event.pull_request.number }}
# # Get the required failed checks
# checks_output=$(gh pr checks ${pr_number} --required)
# echo "$checks_output"
# # Extract check name, status, and URL
# check_name=$(echo "$checks_output" | awk '{print $1}')
# check_status=$(echo "$checks_output" | awk '{print $2}')
# check_url=$(echo "$checks_output" | awk '{print $4}')
# echo "$check_name, $check_status, $check_url"
# # If the check is failing, comment on the PR and break the loop
# if [ "$check_status" == "fail" ]; then
# gh pr comment $pr_number --body "The required check '$check_name' has failed. Please fix the issues before approving the PR.
# Check URL: $check_url"
# # Dismiss the existing review
# gh api repos/${{ github.repository }}/pulls/$pr_number/reviews --jq '.[] | select(.state == "APPROVED") | .id' | xargs -I '{}' gh api --method=PUT -f message="Dismissed due to required checks failure." repos/${{ github.repository }}/pulls/$pr_number/reviews/'{}'/dismissals
# fi
# echo "check_status=$check_status" >> $GITHUB_OUTPUT
# env:
# GH_TOKEN: ${{ secrets.MY_PAT }}
- name: Auto Merge Approved PR
id: auto_merge_pr
# if: steps.check_required_checks.outputs.check_status == 'pass'
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)
# Fetch service owners reviewers based on the service repo name
service_reviewers="${{ steps.parse_reviewers_mapping.outputs.reviewers_mapping }}"
if [[ -n "$review_users" ]]; then
for user in $review_users; do
echo "Checking code owner or service 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 edit $pr_number --add-label "codeowner-approved"
gh pr merge $pr_number --squash --delete-branch
break # Exit the loop if any reviewer is a code owner
elif [[ "$service_reviewers" =~ (^|,)$user($|,) ]]; then
echo "PR has been approved by service owner, merging..."
gh pr edit $pr_number --add-label "service-owner-approved"
gh pr merge $pr_number --squash --delete-branch
break # Exit the loop if any reviewer is a service owner
else
echo "Reviewer $user is not a code owner or service 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:
- name: Debugging
run: |
echo "PR Number: ${{ needs.auto_merge.outputs.pr_number }}"
echo "Head Ref: ${{ needs.auto_merge.outputs.pr_head_ref }}"
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4
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
shell: bash
- name: Rebase "staging" onto "main"
if: steps.merge_status.outputs.status == 'true'
run: |
regex="^main-branch-update-from-.*-values$"
git checkout main
git pull origin main
git checkout staging
git fetch origin
git branch --set-upstream-to=origin/staging staging
if [[ "${{ needs.auto_merge.outputs.pr_head_ref }}" =~ $regex ]]; then
# Accept Changes From main during the rebase
echo "Branch: ${{ needs.auto_merge.outputs.pr_head_ref }}"
git rebase -Xours main
else
git rebase main
fi
# Force push the changes to staging
git push origin staging --force
shell: bash