forked from USRSE/usrse.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull-request.sh
executable file
·103 lines (78 loc) · 3.24 KB
/
pull-request.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
# Suggested by Github actions to be strict
# Taken from https://www.github.com/vsoch/pull-request-action
set -e
set -o pipefail
################################################################################
# Global Variables (we can't use GITHUB_ prefix)
################################################################################
API_VERSION=v3
BASE=https://api.github.com
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
HEADER="Accept: application/vnd.github.${API_VERSION}+json"
HEADER="${HEADER}; application/vnd.github.antiope-preview+json; application/vnd.github.shadow-cat-preview+json"
# URLs
REPO_URL="${BASE}/repos/${GITHUB_REPOSITORY}"
PULLS_URL=$REPO_URL/pulls
################################################################################
# Helper Functions
################################################################################
check_credentials() {
if [[ -z "${GITHUB_TOKEN}" ]]; then
echo "You must include the GITHUB_TOKEN as an environment variable."
exit 1
fi
}
check_events_json() {
if [[ ! -f "${GITHUB_EVENT_PATH}" ]]; then
echo "Cannot find Github events file at ${GITHUB_EVENT_PATH}";
exit 1;
fi
echo "Found ${GITHUB_EVENT_PATH}";
}
create_pull_request() {
SOURCE="${1}" # from this branch
TARGET="${2}" # pull request TO this target
# Check if the branch already has a pull request open
TITLE='Updating Membership Counts'
BODY='This is a pull request to update membership counts for usrse.'
DATA="{\"base\":\"${TARGET}\", \"head\":\"${SOURCE}\", \"body\":\"${BODY}\"}"
RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X GET --data "${DATA}" ${PULLS_URL})
PR=$(echo "${RESPONSE}" | jq --raw-output '.[] | .head.ref')
echo "Response ref: ${PR}"
# Option 1: The pull request is already open
if [[ "${PR}" == "${SOURCE}" ]]; then
echo "Pull request from ${SOURCE} to ${TARGET} is already open!"
# Option 2: Open a new pull request
else
# Post the pull request
DATA="{\"title\":\"${TITLE}\", \"base\":\"${TARGET}\", \"head\":\"${SOURCE}\", \"body\":\"${BODY}\"}"
echo "curl --user ${GITHUB_ACTOR} -X POST --data ${DATA} ${PULLS_URL}"
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL}
echo $?
fi
}
main () {
# path to file that contains the POST response of the event
# Example: https://github.com/actions/bin/tree/master/debug
# Value: /github/workflow/event.json
check_events_json;
# User specified branch for PR
if [ -z "${BRANCH_FROM}" ]; then
echo "You must specify a branch to PR from."
exit 1
fi
echo "Branch for pull request is $BRANCH_FROM"
if [ -z "${BRANCH_AGAINST}" ]; then
BRANCH_AGAINST=master
fi
echo "Pull request will go against ${BRANCH_AGAINST}"
# Ensure we have a GitHub token
check_credentials
create_pull_request $BRANCH_FROM $BRANCH_AGAINST
}
echo "==========================================================================
START: Creating Membership Count Update Pull Request!";
main;
echo "==========================================================================
END: Finished";