forked from abinoda/label-when-approved-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·83 lines (66 loc) · 2.2 KB
/
entrypoint.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
#!/bin/bash
set -e
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi
if [[ -z "$GITHUB_REPOSITORY" ]]; then
echo "Set the GITHUB_REPOSITORY env variable."
exit 1
fi
if [[ -z "$GITHUB_EVENT_PATH" ]]; then
echo "Set the GITHUB_EVENT_PATH env variable."
exit 1
fi
addLabel=$ADD_LABEL
if [[ -n "$LABEL_NAME" ]]; then
echo "Warning: Plase define the ADD_LABEL variable instead of the deprecated LABEL_NAME."
addLabel=$LABEL_NAME
fi
if [[ -z "$addLabel" ]]; then
echo "Set the ADD_LABEL or the LABEL_NAME env variable."
exit 1
fi
URI="https://api.github.com"
API_HEADER="Accept: application/vnd.github.v3+json"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
action=$(jq --raw-output .action "$GITHUB_EVENT_PATH")
state=$(jq --raw-output .review.state "$GITHUB_EVENT_PATH")
number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
label_when_approved() {
# https://developer.github.com/v3/pulls/reviews/#list-reviews-on-a-pull-request
body=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${GITHUB_REPOSITORY}/pulls/${number}/reviews?per_page=100")
reviews=$(echo "$body" | jq --raw-output '.[] | {state: .state} | @base64')
approvals=0
for r in $reviews; do
review="$(echo "$r" | base64 -d)"
rState=$(echo "$review" | jq --raw-output '.state')
if [[ "$rState" == "APPROVED" ]]; then
approvals=$((approvals+1))
fi
echo "${approvals}/${APPROVALS} approvals"
if [[ "$approvals" -ge "$APPROVALS" ]]; then
echo "Labeling pull request"
curl -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"labels\":[\"${addLabel}\"]}" \
"${URI}/repos/${GITHUB_REPOSITORY}/issues/${number}/labels"
if [[ -n "$REMOVE_LABEL" ]]; then
curl -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
-X DELETE \
"${URI}/repos/${GITHUB_REPOSITORY}/issues/${number}/labels/${REMOVE_LABEL}"
fi
break
fi
done
}
if [[ "$action" == "submitted" ]] && [[ "$state" == "approved" ]]; then
label_when_approved
else
echo "Ignoring event ${action}/${state}"
fi