forked from CodelyTV/pr-size-labeler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.sh
106 lines (85 loc) · 3.23 KB
/
github.sh
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
#!/usr/bin/env bash
GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"
github::calculate_total_modifications() {
local -r pr_number="${1}"
local -r files_to_ignore="${2}"
local -r ignore_line_deletions="${3}"
local -r ignore_file_deletions="${4}"
local additions=0
local deletions=0
if [ -z "$files_to_ignore" ] && [ "$ignore_file_deletions" != "true" ]; then
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number")
additions=$(echo "$body" | jq '.additions')
if [ "$ignore_line_deletions" != "true" ]; then
((deletions += $(echo "$body" | jq '.deletions')))
fi
else
# NOTE: this code is not resilient to changes w/ > 100 files as we're not paginating
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number/files?per_page=100")
for file in $(echo "$body" | jq -r '.[] | @base64'); do
filename=$(jq::base64 '.filename')
status=$(jq::base64 '.status')
ignore=false
if [[ ( "$ignore_file_deletions" == "true" || "$ignore_line_deletions" == "true" ) && "$status" == "removed" ]]; then
continue
fi
for pattern in $files_to_ignore; do
if [[ $filename == $pattern ]]; then
ignore=true
break
fi
done
if [ "$ignore" = false ]; then
((additions += $(jq::base64 '.additions')))
if [ "$ignore_line_deletions" != "true" ]; then
((deletions += $(jq::base64 '.deletions')))
fi
fi
done
fi
echo $((additions + deletions))
}
github::add_label_to_pr() {
local -r pr_number="${1}"
local -r label_to_add="${2}"
local -r xs_label="${3}"
local -r s_label="${4}"
local -r m_label="${5}"
local -r l_label="${6}"
local -r xl_label="${7}"
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number")
local labels=$(echo "$body" | jq .labels | jq -r ".[] | .name" | grep -w -e "$xs_label" -e "$s_label" -e "$m_label" -e "$l_label" -e "$xl_label" -v)
labels=$(printf "%s\n%s" "$labels" "$label_to_add")
local -r comma_separated_labels=$(github::format_labels "$labels")
log::message "Final labels: $comma_separated_labels"
curl -sSL \
-H "Authorization: token $GITHUB_TOKEN" \
-H "$GITHUB_API_HEADER" \
-X PATCH \
-H "Content-Type: application/json" \
-d "{\"labels\":[$comma_separated_labels]}" \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number" >/dev/null
}
github::format_labels() {
SAVEIFS=$IFS
IFS=$'\n'
local -r labels=($@)
IFS=$SAVEIFS
quoted_labels=()
for ((i = 0; i < ${#labels[@]}; i++)); do
# echo "Label $i: ${labels[$i]}"
label="${labels[$i]}"
quoted_labels+=("$(str::quote "$label")")
done
coll::join_by "," "${quoted_labels[@]/#/}"
}
github::comment() {
local -r comment="$1"
curl -sSL \
-H "Authorization: token $GITHUB_TOKEN" \
-H "$GITHUB_API_HEADER" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"body\":$comment}" \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments"
}