forked from wkentaro/labelme
-
Notifications
You must be signed in to change notification settings - Fork 0
145 lines (124 loc) · 5.5 KB
/
auto_bump_pr_merge.yml
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
name: Auto Merge
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
BUMP:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Define allowed and ignored files
run: |
ALLOWED_FILES="package.json package.xml CMakeLists.txt CHANGELOG.rst setup.py"
echo "ALLOWED_FILES=$ALLOWED_FILES" >> $GITHUB_ENV
IGNORED_FILES=".github .jenkins"
echo "IGNORED_FILES=$IGNORED_FILES" >> $GITHUB_ENV
- name: Check if PR should be merged
run: |
PR_TITLE="${{ github.event.pull_request.title }}"
BASE_BRANCH="${{ github.event.pull_request.base.ref }}"
HEAD_BRANCH="${{ github.event.pull_request.head.ref }}"
CHANGED_FILES=$(gh pr diff ${{ github.event.pull_request.number }} --name-only)
mapfile -t change_files < <(echo "$CHANGED_FILES")
ALLOWED_FILES_STRING="${{ env.ALLOWED_FILES }}"
IFS=' ' read -r -a allowed_files <<< "$ALLOWED_FILES_STRING"
IGNORED_FILES_STRING="${{ env.IGNORED_FILES }}"
IFS=' ' read -r -a ignored_files <<< "$IGNORED_FILES_STRING"
filtered_change_files=()
for change_file in "${change_files[@]}"; do
exclude_item=false
for ignored_file in "${ignored_files[@]}"; do
if [[ "$change_file" == *"$ignored_file"* ]]; then
exclude_item=true
break;
fi
done
if [ "$exclude_item" = false ]; then
filtered_change_files+=("$change_file")
fi
done
only_allowed_files=true
for filtered_change_file in "${filtered_change_files[@]}"; do
allowed_change_file=false
filtered_change_file_name=$(basename "$filtered_change_file")
for allowed_file in "${allowed_files[@]}"; do
if [ "$filtered_change_file_name" = "$allowed_file" ]; then
allowed_change_file=true
break;
fi
done
if [ "$allowed_change_file" = false ]; then
only_allowed_files=false
break;
fi
done
if [[ "$BASE_BRANCH" == "main" && ( "$HEAD_BRANCH" == "develop" || "$HEAD_BRANCH" == "tote" ) && "$only_allowed_files" == true ]]; then
echo "should_merge=true" >> $GITHUB_ENV
else
echo "should_merge=false" >> $GITHUB_ENV
fi
env:
GITHUB_TOKEN: ${{ secrets.SECRETS_GITHUB_TOKEN }}
- name: Ready for review
if: success() && env.should_merge == 'true'
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
IS_DRAFT=$(gh pr view $PR_NUMBER --json isDraft --jq '.isDraft')
if [[ "$IS_DRAFT" == "true" ]]; then
gh pr ready $PR_NUMBER
echo "Draft status removed. Ready to merge."
fi
env:
GITHUB_TOKEN: ${{ secrets.SECRETS_GITHUB_TOKEN }}
- name: Wait and Check PR status
if: success() && env.should_merge == 'true'
run: |
PR_NUMBER=$(jq -r '.pull_request.number' < "$GITHUB_EVENT_PATH")
STATUS_CHECKS=$(gh pr checks "$PR_NUMBER" --json name,state --jq '.[]')
echo "$STATUS_CHECKS" | jq -r '. | "\(.name): \(.state)"'
PR_STATUS_ALL_SUCCESS=true
echo "pr_status_all_success=true" >> $GITHUB_ENV
for i in {1..10}; do
echo "Checking status checks... (Attempt $i)"
PR_NUMBER=$(jq -r '.pull_request.number' < "$GITHUB_EVENT_PATH")
STATUS_CHECKS=$(gh pr checks "$PR_NUMBER" --json name,state --jq '.[]')
while IFS= read -r status; do
CHECK_NAME=$(echo "$status" | jq -r '.name')
CHECK_STATE=$(echo "$status" | jq -r '.state')
if [[ "$CHECK_NAME" != "BUMP" && "$CHECK_STATE" != "SUCCESS" ]]; then
PR_STATUS_ALL_SUCCESS=false
break
fi
done <<< "$STATUS_CHECKS"
if [ "$PR_STATUS_ALL_SUCCESS" = true ]; then
echo "All required status checks are successful."
echo "pr_status_all_success=true" >> $GITHUB_ENV
break
fi
sleep 60
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check all checkboxes
if: success() && env.should_merge == 'true' && env.pr_status_all_success == 'true'
run: |
gh pr view ${{ github.event.pull_request.number }} --json body --jq .body > pr_body.txt
PR_BODY=$(cat pr_body.txt)
CHECKED_BODY=$(echo "$PR_BODY" | sed 's/\[ \]/[x]/g')
if [[ "$PR_BODY" != "$CHECKED_BODY" ]]; then
gh pr edit ${{ github.event.pull_request.number }} --body "$CHECKED_BODY"
echo "All checkboxes have been checked"
else
echo "No checkboxes found or all checkboxes are already checked"
fi
env:
GITHUB_TOKEN: ${{ secrets.SECRETS_GITHUB_TOKEN }}
- name: Merged PR
if: success() && env.should_merge == 'true' && env.pr_status_all_success == 'true'
run: |
COMMENT="본 패키지는 변경 사항이 없는 리포지토리 / 패키지임을 확인하여 병합되었습니다!"
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT"
gh pr merge ${{ github.event.pull_request.number }} --merge --admin
env:
GITHUB_TOKEN: ${{ secrets.SECRETS_GITHUB_TOKEN }}