-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.sh
executable file
·103 lines (91 loc) · 2.99 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
#!/bin/bash
set -eu
TRUNK=${TRUNK_BRANCH:=master}
# env | grep GITHUB
case $GITHUB_REF in
*/"$TRUNK") echo "We are on $TRUNK, let's merge.";;
*) echo "Not on $TRUNK (${GITHUB_REF}), do nothing."; exit 0;;
esac
# Let's create a PR
echo "Creating a PR..."
payload=$(cat <<EOF
{
"base": "${BASE_BRANCH:=develop}",
"head": "${TRUNK}",
"title": "Merging back ${TRUNK}"
}
EOF
)
tmp=$(mktemp)
status_code=$(curl --silent --output ${tmp} \
--write-out "%{http_code}" \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-type: application/json" \
-X POST https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls \
-d "${payload}")
output=$(cat ${tmp})
rm ${tmp}
if test ${status_code} -eq 422; then #Existing PR
echo "A PR already exists, let's find its number..."
tmp=$(mktemp)
status_code=$(curl --silent --output ${tmp} \
--write-out "%{http_code}" \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-type: application/json" \
-X GET "https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls?head=${TRUNK}&base=${BASE_BRANCH}" )
output=$(cat ${tmp})
rm ${tmp}
if test ${status_code} -ne 200; then
echo "An error occured trying to find the existing PR, status code ${status_code}"
echo $output | jq
exit 1
fi
pr_no=$(echo $output | jq -r '.[0].number')
echo "Found an existing PR (#${pr_no}), let's try and use that..."
elif test ${status_code} -ne 201; then # Other failure
echo "Creating a PR has failed with status code ${status_code}"
echo $output | jq
exit 1
else
pr_no=$(echo $output | jq -r '.number')
fi
echo "Merging PR..."
tmp=$(mktemp)
status_code=$(curl --silent --output ${tmp} \
--write-out "%{http_code}" \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-type: application/json" \
-X PUT https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${pr_no}/merge )
output=$(cat ${tmp})
rm ${tmp}
if test ${status_code} -ne 200; then
echo "The merge has failed with status code ${status_code}"
echo $output | jq
if [ -e ${DESC_PATH:=".github/merge-instructions.md"} ] ; then
merge_instructions=${DESC_PATH}
else
merge_instructions='/default_description.md'
fi
echo "Updating PR description..."
payload=$(jq -n --rawfile a ${merge_instructions} '.body=$a' | envsubst)
curl --silent --output /dev/null --fail \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-type: application/json" \
-X PATCH https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${pr_no} \
-d "${payload}"
echo "Requesting review from ${GITHUB_ACTOR}"
payload=$(cat <<EOF
{
"reviewers": [
"${GITHUB_ACTOR}"
]
}
EOF
)
curl --silent --output /dev/null --fail \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-type: application/json" \
-X POST https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${pr_no}/requested_reviewers \
-d "${payload}"
exit 1
fi