forked from transferwise/actions-pr-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
executable file
·134 lines (112 loc) · 4.44 KB
/
functions.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
126
127
128
129
130
131
132
133
134
#!/bin/bash
# Send emoji to PR description
# @param - GITHUB_PULL_REQUEST_EVENT_NUMBER
# @param - EMOJI
sendReaction() {
local GITHUB_ISSUE_NUMBER="$1"
local EMOJI="$2"
curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.squirrel-girl-preview+json" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"content\":\"${EMOJI}\"}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${GITHUB_ISSUE_NUMBER}/reactions"
}
# Remove emoji from PR description
# @param - GITHUB_PULL_REQUEST_EVENT_NUMBER
# @param - EMOJI
removeReaction() {
local GITHUB_ISSUE_NUMBER="$1"
local EMOJI="$2"
LIST=$(curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.squirrel-girl-preview+json" \
-X GET \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${GITHUB_ISSUE_NUMBER}/reactions" \
)
COMMENT_ID=$(echo "${LIST}" | jq ".[] | select (.content | contains(\"${EMOJI}\")) | .id")
curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.squirrel-girl-preview+json" \
-X DELETE \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${GITHUB_ISSUE_NUMBER}/reactions/${COMMENT_ID}"
}
# Send comment to PR
# @param - GITHUB_PULL_REQUEST_EVENT_NUMBER
# @param - comment text
sendComment() {
local GITHUB_ISSUE_NUMBER="$1"
local GITHUB_ISSUE_COMMENT="$2"
jq -n --arg msg "$GITHUB_ISSUE_COMMENT" '{body: $msg }' > tmp.txt
curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-H "Content-Type: application/json" \
-d @tmp.txt \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${GITHUB_ISSUE_NUMBER}/comments"
}
# Comment requesting changes to PR
# @param - GITHUB_PULL_REQUEST_EVENT_NUMBER
# @param - comment text
requestChangesComment() {
local GITHUB_ISSUE_NUMBER="$1"
local GITHUB_ISSUE_COMMENT="$2"
LIST=$(curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X GET \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${GITHUB_ISSUE_NUMBER}/reviews" \
)
LAST_STATE=$(echo "${LIST}" | jq -r "last( .[] | select (.user.login | contains(\"${GITHUB_COMMENT_ACTOR}\")) | .state )")
if [[ $LAST_STATE == "CHANGES_REQUESTED" ]]; then
return 0
fi
jq -n --arg msg "$GITHUB_ISSUE_COMMENT" '{body: $msg , event: "REQUEST_CHANGES"}' > tmp.txt
curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-H "Content-Type: application/json" \
-d @tmp.txt \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${GITHUB_ISSUE_NUMBER}/reviews"
}
# Approve PR
# @param - GITHUB_PULL_REQUEST_EVENT_NUMBER
approvePr() {
local GITHUB_ISSUE_NUMBER="$1"
LIST=$(curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X GET \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${GITHUB_ISSUE_NUMBER}/reviews" \
)
LAST_STATE=$(echo "${LIST}" | jq -r "last( .[] | select (.user.login | contains(\"${GITHUB_COMMENT_ACTOR}\")) | .state )")
if [[ $LAST_STATE == "APPROVED" ]]; then
return 0
fi
curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"event\":\"APPROVE\"}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${GITHUB_ISSUE_NUMBER}/reviews"
}
# Close PR
# @param - GITHUB_PULL_REQUEST_EVENT_NUMBER
closeIssue() {
local GITHUB_ISSUE_NUMBER="$1"
curl -sSL \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"state\":\"closed\"}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${GITHUB_ISSUE_NUMBER}"
}