-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·126 lines (107 loc) · 3.42 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
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
#!/bin/bash
# Varun Chopra <[email protected]>
#
# This action runs every time a PR is updated & prepares it for CI.
# CI checks pull requests that are labeled 'needs_ci' and runs unit tests and lint.
# - If the 'needs_ci' label is present, no additional labels are added.
# - If the 'shipit' label is present, it ensures the 'needs_ci' label is added.
# - If neither 'needs_ci' nor 'shipit' are present, it adds the 'needs_ci:lite' label.
# - If 'ci_verified' label is present, it removes 'ci_verified' and adds 'needs_ci'.
# - If 'ci_verified:lite' label is present, it removes 'ci_verified:lite' and adds 'needs_ci:lite'.
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
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")
pr_body=$(jq --raw-output .pull_request.body "$GITHUB_EVENT_PATH")
number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
title=$(jq --raw-output .pull_request.title "$GITHUB_EVENT_PATH")
draft=$(jq --raw-output .pull_request.draft "$GITHUB_EVENT_PATH")
echo $title
echo $draft
if [[ "$draft" == "true" ]]; then
echo "Skipping PR since it's still in draft."
exit 0
fi
add_comment(){
curl -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"body\":\"${1}\"}" \
"${URI}/repos/${GITHUB_REPOSITORY}/issues/${number}/comments"
}
add_label(){
curl -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"labels\":[\"${1}\"]}" \
"${URI}/repos/${GITHUB_REPOSITORY}/issues/${number}/labels"
}
remove_label(){
curl -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
-X DELETE \
"${URI}/repos/${GITHUB_REPOSITORY}/issues/${number}/labels/${1// /%20}"
}
body=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${GITHUB_REPOSITORY}/pulls/${number}")
labels=$(echo "$body" | jq --raw-output '.labels[].name')
IFS=$'\n'
needs_ci_label_present=false
needs_ci_lite_label_present=false
shipit_label_present=false
for label in $labels; do
case $label in
ci_verified)
echo "Removing label: $label and adding needs_ci"
remove_label "$label"
add_label "needs_ci"
needs_ci_label_present=true
;;
ci_verified:lite)
echo "Removing label: $label and adding needs_ci:lite"
remove_label "$label"
add_label "needs_ci:lite"
needs_ci_lite_label_present=true
;;
needs_ci)
echo "needs_ci label is already present"
needs_ci_label_present=true
;;
needs_ci:lite)
echo "needs_ci:lite label is already present"
needs_ci_lite_label_present=true
;;
shipit)
echo "shipit label is present"
shipit_label_present=true
;;
*)
echo "Unknown label $label"
;;
esac
done
if [[ "$shipit_label_present" = true ]]; then
if [[ "$needs_ci_label_present" = false ]]; then
add_label "needs_ci"
fi
elif [[ "$needs_ci_lite_label_present" = false && "$needs_ci_label_present" = false ]]; then
add_label "needs_ci:lite"
fi
echo "Pull request passed all checkpoints!"