-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib
136 lines (110 loc) · 4.19 KB
/
lib
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
#!/bin/bash
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
function bl_hub_available(){
# type instead of which, so it can be stubbed in tests
type hub &>/dev/null || bl_fail "hub (github cli) binary not found, please install it via your package manager or use bl_hub_download_latest."
}
function bl_hub_creds_available(){
config_file="${HUB_CONFIG:-${HOME}/.config/hub}"
[[ -n "${GITHUB_USER:-}" ]] && [[ -n "${GITHUB_TOKEN:-}" ]] && return
[[ -e "${config_file}" ]] && return
bl_fail "No credentials found for (git)hub please set GITHUB_USER and GITHUB_TOKEN or create ~/.config/hub"
}
function bl_hub_check(){
bl_in_git_repo \
&& bl_hub_available \
&& bl_hub_creds_available
}
function bl_hub_download_latest(){
local install_dir="${1:-${HOME}/bin}"
local os_arch="${2:-}"
local tmpdir=".hubdl"
local download_url
local bin_path
bl_curl_available
if [[ -z "${os_arch}" ]]; then
if [[ "${OSTYPE}" =~ "darwin" ]]; then
os_arch="darwin-amd64"
else
os_arch="linux-amd64"
fi
bl_debug "Hub Download detected arch: ${os_arch}"
fi
asset_url="$(curl https://api.github.com/repos/github/hub/releases/latest \
|jq -r ".assets | map(select(.name | contains(\"${os_arch}\")))[0].url")"
download_url="$(curl "${asset_url}" |jq -r '.browser_download_url')"
bin_path="${install_dir}/hub"
mkdir -p "${install_dir}"
mkdir -p "${tmpdir}"
bl_spushd "${tmpdir}"
curl -s -L "${download_url}" > hub.tgz
tar xf hub.tgz
bl_spopd
mv "${tmpdir}"/*/bin/hub "${bin_path}"
rm -rf "${tmpdir}"
bl_info "${download_url}/bin/hub --> ${bin_path}"
}
function bl_hub_issue_number_for_title(){
local title="${1}"
bl_hub_check
hub issue \
|grep "${title}" \
|awk -F'[ #]+' '{print $2; exit}'
}
function bl_hub_add_issue_comment(){
local issue_number="${1}"
local comment="${2}"
bl_hub_check
[[ -n "${comment}" ]] || bl_die "bl_hub_add_issue_comment: Comment must not be empty"
hub issue show "${issue_number}" >/dev/null || bl_die "Github Issue number ${issue_number} isn't valid for repo $(pwd)"
owner_repo="$(bl_github_owner_repo)"
if hub api "repos/${owner_repo}/issues/${issue_number}/comments" --field body="${comment}" >/dev/null; then
bl_debug "Added comment: \"${comment}\" to https://github.com/${owner_repo}/issues/${issue_number}"
else
bl_fail "Failed to add comment: ${comment} to issue: ${owner_repo}#${issue_number}"
fi
}
function bl_hub_comment_or_create_issue(){
local title="${1}"
local message="${2}"
local label="${3:-}"
local label_param=""
local issue_number
local issue_url
local action
local owner_repo
bl_hub_check
owner_repo="$(bl_github_owner_repo)"
issue_number="$(bl_hub_issue_number_for_title "${title}" ||:)"
if [[ -z "${issue_number}" ]]; then
action="created"
if [[ -n "${label}" ]]; then
label_param="-l ${label}"
fi
# issue doesn't exist create it
# The following prevents a shellcheck warning about label_param
# getting split into multiple words. That is exactly what should
# happen as "-l" and "labelname" should be separate tokens for
# the hub command.
# shellcheck disable=SC2086
issue_url="$(hub issue create -m "${title}
${message}" ${label_param})"
# Example issue url: https://github.com/{owner}/{repo}/issues/{issue number}"
# To find the issue number, split on / and take the last field
issue_number="$(awk -F'/' '{print $NF}' <<<"${issue_url}" )"
bl_debug "Created issue: ${issue_url} with title \"${title}\""
else
issue_url="https://github.com/${owner_repo}/issues/${issue_number}"
action="commented"
bl_debug "Found existing issue for title \"${title}\": ${issue_url}"
bl_hub_add_issue_comment "${issue_number}" "${message}"
fi
cat <<EOJ
{
"action": "${action}",
"issue_number": "${issue_number}",
"issue_url": "${issue_url}",
"issue_ref": "${owner_repo}#${issue_number}"
}
EOJ
}