forked from vitorgalvao/tiny-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
climergebutton
executable file
·150 lines (121 loc) · 4.07 KB
/
climergebutton
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
readonly program="$(basename "${0}")"
readonly default_merge_strategy='squash'
function syntax_error {
echo -e "${program}: ${1}\nTry \`${program} --help\` for more information." >&2
exit 1
}
function usage {
echo "
Usage:
${program} [options] <github_pull_request_url>
Multiple <github_pull_request_url> can be given, and will be processed in sequence.
Options:
-m, --merge-method 'merge', 'squash', or 'rebase'. Defaults to '${default_merge_strategy}'.
-t, --ensure-token Ask to save GitHub token if there is none, and exit.
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
function get_github_token {
readonly local operating_system="$(uname -s)"
# Check if we already have a token set
local github_token
if [[ "${operating_system}" == 'Darwin' ]]; then
github_token="$(security find-generic-password -s "${program} github token" -w 2>/dev/null)"
else
local token_file
[[ -n "${XDG_CONFIG_HOME}" ]] && token_file="${XDG_CONFIG_HOME}/${program}/token" || token_file="${HOME}/.config/${program}/token"
[[ -f "${token_file}" ]] && github_token="$(< "${token_file}")" || github_token=''
fi
if [[ -n "${github_token}" ]]; then
echo "${github_token}"
return 0
fi
# If we get here, there was no token set and we need to request one
echo 'We need to create a new GitHub token to continue the script.' >&2
readonly local machine_name="$(hostname -s)"
local github_username github_password
read -rp "Github username: " github_username
read -rsp "Github password (never stored): " github_password
echo >&2
readonly local request=(curl --silent 'https://api.github.com/authorizations' --user "${github_username}:${github_password}" --data "{\"scopes\":[\"repo\"],\"note\":\"${program} for ${USER} on ${machine_name}\"}")
local response="$("${request[@]}")"
while grep --quiet 'Must specify two-factor authentication OTP code.' <<< "${response}"; do
read -rp '2FA code: ' otp
response="$("${request[@]}" --header "X-GitHub-OTP: ${otp}")"
done
if ! grep --quiet '"token"' <<< "${response}"; then
echo -e "\n${response}" >&2
exit 1
fi
github_token="$(grep 'token' <<< "${response}" | head -1 | cut -d'"' -f4)"
if [[ "${operating_system}" == 'Darwin' ]]; then
security add-generic-password -s "${program} github token" -a "${github_username}" -w "${github_token}"
else
mkdir -p "$(basename "${token_file}")"
echo "${github_password}" > "${token_file}"
fi
echo "${github_token}"
}
function merge_pr {
local url owner repo pr_number response
url="$(perl -pe 's|(.*/pull/\d+).*|\1|' <<< "${1}")"
owner="$(cut -d'/' -f4 <<< "${url}")"
repo="$(cut -d'/' -f5 <<< "${url}")"
pr_number="$(cut -d'/' -f7 <<< "${url}")"
echo -n "Merging ${owner}/${repo}/${pr_number}… "
response="$(curl --silent --request PUT --data "{ \"merge_method\": \"${merge_strategy:-${default_merge_strategy}}\" }" --header "Authorization: token ${github_token}" "https://api.github.com/repos/${owner}/${repo}/pulls/${pr_number}/merge")"
if grep --quiet '"merged": true' <<< "${response}"; then
echo 'Merged.'
else
response_message="$(grep 'message' <<< "${response}" | sed -E 's/.*"message": "(.*)".*/\1/')"
echo "Failed (${response_message})."
exit 1
fi
}
# available flags
args=()
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-m | --merge-method)
merge_strategy="${2}"
shift
;;
-t | --ensure-token)
ask_for_token='true'
;;
--)
shift
args+=("${@}")
break
;;
-*)
syntax_error "unrecognized option: ${1}"
;;
*)
args+=("${1}")
;;
esac
shift
done
set -- "${args[@]}"
readonly github_token="$(get_github_token)"
[[ -n "${ask_for_token}" ]] && exit 0
# Abort if no argument was given
if [[ "${#}" -eq 0 ]]; then
usage
exit 1
fi
while [[ "${1}" ]]; do # Allow for multiple github PR urls to be passed at once
if [[ "${1}" =~ https://github.com/* ]]; then
merge_pr "${1}"
shift
else
usage
exit 1
fi
done