-
Notifications
You must be signed in to change notification settings - Fork 7
155 lines (135 loc) · 5.87 KB
/
pull_request_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
name: Pull Request Content Check
on:
pull_request:
branches:
- 'main'
types:
- opened
- reopened
- synchronize
- edited
env:
SIZE_LABELS: >-
"Size - XS","Size - S","Size - M","Size - L","Size - XL","Size - XXL"
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
pull_request_content_and_size_check:
if: github.event.pull_request.merged != true
name: 'Pull Request Content and Size Check'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 2
- name: Step 1 - ⚙️ Setup GitHub Config
run: |
git config user.name "🤖 Pereira Tech Talks"
git config user.email "[email protected]"
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
- name: Step 2 - 🧪 Check pr size label
id: check_pr_size_label_step
env:
ENV_LABELS: ${{ toJSON(github.event.pull_request.labels) }}
run: |
IFS=',' read -ra SIZE_LABELS_ARR <<< "$SIZE_LABELS"
# Remove the quotes from SIZE_LABELS_ARR
SIZE_LABELS_ARR=("${SIZE_LABELS_ARR[@]//\"}")
# Get PR Labels
FOUND_LABELS_JSON=$(echo "$ENV_LABELS" | jq -r '.[] | .name')
readarray -t FOUND_LABELS <<<"$FOUND_LABELS_JSON"
echo "FOUND LABELS:"
printf '%s\n' "${FOUND_LABELS[@]}"
# Check if any of the labels in FOUND_LABELS is in SIZE_LABELS_ARR
for found_label in "${FOUND_LABELS[@]}"; do
for size_label in "${SIZE_LABELS_ARR[@]}"; do
if [[ "$found_label" == "$size_label" ]]; then
echo "Label '$found_label' found in SIZE_LABELS"
echo "size_label_found=$found_label" >> $GITHUB_OUTPUT
fi
done
done
- name: Step 3 - 🧪🧪 Calculate PR Size and Label
run: |
CURRENT_SIZE="${{ steps.check_pr_size_label_step.outputs.size_label_found }}"
PR_BASE_REF=${{ github.event.pull_request.base.ref }}
PR_HEAD_REF=${{ github.event.pull_request.head.ref }}
git fetch origin $PR_BASE_REF
git fetch origin $PR_HEAD_REF
CHANGES_STATS=$(git diff --shortstat origin/$PR_BASE_REF..origin/$PR_HEAD_REF)
echo $CHANGES_STATS
LINES_CHANGED=$(echo $CHANGES_STATS | awk '{print $4+$6}')
echo "Lines changed: $LINES_CHANGED"
if [ -z "$LINES_CHANGED" ]; then
echo "No lines changed or diff failed."
exit 1
fi
if [ $LINES_CHANGED -le 50 ]; then
SIZE="Size - XS"
elif [ $LINES_CHANGED -le 100 ]; then
SIZE="Size - S"
elif [ $LINES_CHANGED -le 500 ]; then
SIZE="Size - M"
elif [ $LINES_CHANGED -le 800 ]; then
SIZE="Size - L"
elif [ $LINES_CHANGED -le 1500 ]; then
SIZE="Size - XL"
else
SIZE="Size - XXL"
fi
echo "Current Size: '$CURRENT_SIZE'"
echo "New Size: '$SIZE'"
if [ "$SIZE" == "$CURRENT_SIZE" ]; then
echo "No size change detected."
exit 0
fi
if [ -n "$CURRENT_SIZE" ]; then
echo "Removing current size label..."
CURRENT_SIZE_ENCODED=$(jq -rn --arg str "$CURRENT_SIZE" '$str|@uri')
curl \
-X DELETE \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/$CURRENT_SIZE_ENCODED
fi
echo "Adding new size label..."
curl \
-X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels \
-d "{\"labels\": [\"${SIZE}\"]}"
if [ "$SIZE" == "Size - L" ] || [ "$SIZE" == "Size - XL" ] || [ "$SIZE" == "Size - XXL" ]; then
WARNING_MESSAGE="⚠️ WARNING: This PR is large.\n"
if [ "$SIZE" == "Size - XL" ] || [ "$SIZE" == "Size - XXL" ]; then
WARNING_MESSAGE="⚠️ 🔴 WARNING: This PR is too large.\nConsider breaking it down into smaller ones for faster and easier review next time.\n"
fi
WARNING_MESSAGE+="Make sure your code has tests and you have done the necessary QA."
echo "Adding warning comment..."
curl \
-X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \
-d "{\"body\": \"$WARNING_MESSAGE\"}"
fi
- name: Step 4 - 🧪🧪🧪 Check "TITLE" length
run: |
MIN_TITLE_LENGTH=10
PR_TITLE="${{ github.event.pull_request.title }}"
TITLE_LENGTH=$(echo -n "$PR_TITLE" | wc -c)
if [ "$TITLE_LENGTH" -lt "$MIN_TITLE_LENGTH" ]; then
echo "Pull request title is too short. It must have at least $MIN_TITLE_LENGTH characters."
exit 1
fi
- name: Step 5 - 🧪🧪🧪🧪 Check "BODY" length
run: |
MIN_BODY_LENGTH=30
PR_BODY=$(printf "%s" "${{ github.event.pull_request.body }}")
BODY_LENGTH=$(echo -n "$PR_BODY" | wc -c)
if [ "$BODY_LENGTH" -lt "$MIN_BODY_LENGTH" ]; then
echo "Pull request body is too short. It must have at least $MIN_BODY_LENGTH characters."
exit 1
fi