-
Notifications
You must be signed in to change notification settings - Fork 1.4k
171 lines (136 loc) · 6.11 KB
/
hotfix-pr-check.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
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
name: Hotfix-PR-Check
on:
pull_request:
types:
- opened
- edited
- synchronize
branches:
- "hotfix-*"
jobs:
hotfix_pr_check:
name: Verify Hotfix PR
runs-on: ubuntu-latest
env:
HOTFIX_PR_AUTHOR: ${{ github.event.pull_request.user.login }}
HOTFIX_PR_TITLE: ${{ github.event.pull_request.title }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Get hotfix pull request body
shell: bash
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: echo $PR_BODY > hotfix_pr_body.txt
- name: Get a list of all original PR numbers
shell: bash
run: |
# Extract a list of lines with the format 'juspay/hyperswitch/pull/1200' or '#1200' using 'sed'.
# If empty, then error out and exit.
# else, use 'grep' to extract out 'juspay/hyperswitch/pull/1200' or '#1200' patterns from each line.
# Use 'sed' to remove the part of the matched strings that precedes the last "/" character (in cases like, juspay/hyperswitch/pull/1200 - 1200)
# and sed again to remove any "#" characters from the extracted numeric part (in cases like #1200 - 1200), ultimately getting PR/issue number.
# Finally, remove (if any) duplicates from the list
SED_OUTPUT=$(sed -E '/\/juspay\/hyperswitch\/pull\/[0-9]+|#[0-9]+/!d' hotfix_pr_body.txt)
if [ -z "$SED_OUTPUT" ]; then
echo "::error::No original PRs found"
exit 1
else
PR_NUMBERS=($(echo "$SED_OUTPUT" | grep -oE 'juspay/hyperswitch/pull/[0-9]+|#([0-9]+)' | sed 's/.*\///' | sed 's/#//' | sort -u))
echo "PR_NUMBERS=${PR_NUMBERS[@]}" >> $GITHUB_ENV
echo "Original PR's found: ("${PR_NUMBERS[*]/#/#}")"
fi
- name: Verify Original PRs
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUMBERS="${PR_NUMBERS[*]}"
all_checks_failed=1
PR_AUTHORS=()
PR_TITLES=()
PR_BASE_REFS=()
PR_STATES=()
for pr_number in ${PR_NUMBERS}; do
is_pull_request="$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "/repos/juspay/hyperswitch/issues/${pr_number}" | jq '.pull_request')"
if [[ "$is_pull_request" == null ]]; then
continue
else
pr_info=$(gh pr view "${pr_number}" --json number,title,baseRefName,state,author)
pr_author=$(echo "${pr_info}" | jq -r '.author.login')
pr_title=$(echo "${pr_info}" | jq -r '.title')
pr_base_ref=$(echo "${pr_info}" | jq -r '.baseRefName')
pr_state=$(echo "${pr_info}" | jq -r '.state')
if [[ "${pr_author}" == "${HOTFIX_PR_AUTHOR}" && \
"${pr_title}" == "${HOTFIX_PR_TITLE}" && \
"${pr_base_ref}" == "main" && \
"${pr_state}" == "MERGED" ]]; then
all_checks_failed=0
break
fi
PR_AUTHORS+=("$pr_author")
PR_TITLES+=("$pr_title")
PR_BASE_REFS+=("$pr_base_ref")
PR_STATES+=("$pr_state")
fi
done
if [[ $all_checks_failed -eq 1 ]]; then
# Set a flag to track if a author match is found
author_match_found=0
for ((i = 0; i < ${#PR_AUTHORS[@]}; i++)); do
if [[ "${HOTFIX_PR_AUTHOR}" == "${PR_AUTHORS[i]}" ]]; then
# If a match is found, set the flag to 1 and break out of the loop
author_match_found=1
break
fi
done
if [[ $author_match_found -eq 0 ]]; then
echo "::error::Hotfix PR author does not match any of the Original PR authors. Hotfix PR author: '${HOTFIX_PR_AUTHOR}'"
fi
# Set a flag to track if a title match is found
title_match_found=0
for ((i = 0; i < ${#PR_TITLES[@]}; i++)); do
if [[ "${HOTFIX_PR_TITLE}" == "${PR_TITLES[i]}" ]]; then
# If a match is found, set the flag to 1 and break out of the loop
title_match_found=1
break
fi
done
if [[ $title_match_found -eq 0 ]]; then
echo "::error::Hotfix PR title does not match any of the Original PR titles. Hotfix PR title: '${HOTFIX_PR_TITLE}'"
fi
# Set a flag to track if any of the original PRs point to the 'main'
original_pr_points_to_main=0
for ((i = 0; i < ${#PR_BASE_REFS[@]}; i++)); do
if [[ "${PR_BASE_REFS[i]}" == "main" ]]; then
# If a match is found, set the flag to 1 and break out of the loop
original_pr_points_to_main=1
break
fi
done
if [[ $original_pr_points_to_main -eq 0 ]]; then
echo "::error::None of the Original PR's baseRef is 'main'"
fi
# Set a flag to track if any of the original PR's state is 'MERGED'
original_pr_merged=0
for ((i = 0; i < ${#PR_STATES[@]}; i++)); do
if [[ "${PR_STATES[i]}" == "MERGED" ]]; then
# If a match is found, set the flag to 1 and break out of the loop
original_pr_merged=1
break
fi
done
if [[ $original_pr_merged -eq 0 ]]; then
echo "::error::None of the Original PR is merged"
fi
# Print all Original PR's (number), (pr_title), (pr_author), (pr_base_ref) and (pr_state)
i=0
echo "Original PR info:"
for pr_number in ${PR_NUMBERS}; do
echo "#${pr_number} - pr_title: '${PR_TITLES[i]}' - pr_author: '${PR_AUTHORS[i]}' - pr_base_ref: '${PR_BASE_REFS[i]}' - pr_state: '${PR_STATES[i]}'"
i+=1
done
exit 1
else
echo "::notice::Hotfix PR satisfies all the required conditions"
fi