|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}" |
| 4 | + |
| 5 | +function bl_hub_available(){ |
| 6 | + # type instead of which, so it can be stubbed in tests |
| 7 | + 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." |
| 8 | +} |
| 9 | + |
| 10 | +function bl_hub_creds_available(){ |
| 11 | + config_file="${HUB_CONFIG:-${HOME}/.config/hub}" |
| 12 | + [[ -n "${GITHUB_USER:-}" ]] && [[ -n "${GITHUB_TOKEN:-}" ]] && return |
| 13 | + [[ -e "${config_file}" ]] && return |
| 14 | + bl_fail "No credentials found for (git)hub please set GITHUB_USER and GITHUB_TOKEN or create ~/.config/hub" |
| 15 | +} |
| 16 | + |
| 17 | +function bl_hub_check(){ |
| 18 | + bl_in_git_repo \ |
| 19 | + && bl_hub_available \ |
| 20 | + && bl_hub_creds_available |
| 21 | +} |
| 22 | + |
| 23 | +function bl_hub_download_latest(){ |
| 24 | + local install_dir="${1:-${HOME}/bin}" |
| 25 | + local os_arch="${2:-}" |
| 26 | + local tmpdir=".hubdl" |
| 27 | + local path |
| 28 | + local download_url |
| 29 | + local bin_path |
| 30 | + |
| 31 | + if [[ -z "${os_arch}" ]]; then |
| 32 | + if [[ "${OSTYPE}" =~ "darwin" ]]; then |
| 33 | + os_arch="darwin-amd64" |
| 34 | + else |
| 35 | + os_arch="linux-amd64" |
| 36 | + fi |
| 37 | + bl_debug "Hub Download detected arch: ${os_arch}" |
| 38 | + fi |
| 39 | + |
| 40 | + path="$(curl -s -L https://github.com/github/hub/releases/latest |grep -o '[^"]*hub-'${os_arch}'[^"]*')" |
| 41 | + download_url="https://github.com/${path}" |
| 42 | + |
| 43 | + bin_path="${install_dir}/hub" |
| 44 | + mkdir -p "${install_dir}" |
| 45 | + |
| 46 | + mkdir -p "${tmpdir}" |
| 47 | + bl_spushd "${tmpdir}" |
| 48 | + curl -s -L "${download_url}" > hub.tgz |
| 49 | + tar xf hub.tgz |
| 50 | + bl_spopd |
| 51 | + mv "${tmpdir}"/*/bin/hub "${bin_path}" |
| 52 | + rm -rf "${tmpdir}" |
| 53 | + |
| 54 | + bl_info "${download_url}/bin/hub --> ${bin_path}" |
| 55 | +} |
| 56 | + |
| 57 | +function bl_hub_issue_number_for_title(){ |
| 58 | + local title="${1}" |
| 59 | + bl_hub_check |
| 60 | + hub issue \ |
| 61 | + |grep "${title}" \ |
| 62 | + |awk -F'[ #]+' '{print $2}' |
| 63 | +} |
| 64 | + |
| 65 | +function bl_hub_add_issue_comment(){ |
| 66 | + local issue_number="${1}" |
| 67 | + local comment="${2}" |
| 68 | + |
| 69 | + bl_hub_check |
| 70 | + |
| 71 | + [[ -n "${comment}" ]] || bl_die "bl_hub_add_issue_comment: Comment must not be empty" |
| 72 | + hub issue show "${issue_number}" >/dev/null || bl_die "Github Issue number ${issue_number} isn't valid for repo $(pwd)" |
| 73 | + |
| 74 | + owner_repo="$(bl_github_owner_repo)" |
| 75 | + if hub api "repos/${owner_repo}/issues/${issue_number}/comments" --field body="${comment}" >/dev/null; then |
| 76 | + bl_debug "Added comment: \"${comment}\" to https://github.com/${owner_repo}/issues/${issue_number}" |
| 77 | + else |
| 78 | + bl_fail "Failed to add comment: ${comment} to issue: ${owner_repo}#${issue_number}" |
| 79 | + fi |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +function bl_hub_comment_or_create_issue(){ |
| 84 | + local title="${1}" |
| 85 | + local message="${2}" |
| 86 | + local issue_number |
| 87 | + local issue_url |
| 88 | + local action |
| 89 | + local owner_repo |
| 90 | + bl_hub_check |
| 91 | + |
| 92 | + owner_repo="$(bl_github_owner_repo)" |
| 93 | + issue_number="$(bl_hub_issue_number_for_title "${title}" ||:)" |
| 94 | + |
| 95 | + if [[ -z "${issue_number}" ]]; then |
| 96 | + action="created" |
| 97 | + # issue doesn't exist create it |
| 98 | + issue_url="$(hub issue create -m "${title} |
| 99 | +
|
| 100 | +${message}")" |
| 101 | + |
| 102 | + # Example issue url: https://github.com/{owner}/{repo}/issues/{issue number}" |
| 103 | + # To find the issue number, split on / and take the last field |
| 104 | + issue_number="$(awk -F'/' '{print $NF}' <<<"${issue_url}" )" |
| 105 | + |
| 106 | + bl_debug "Created issue: ${issue_url} with title \"${title}\"" |
| 107 | + else |
| 108 | + issue_url="https://github.com/${owner_repo}/issues/${issue_number}" |
| 109 | + action="commented" |
| 110 | + bl_debug "Found existing issue for title \"${title}\": ${issue_url}" |
| 111 | + bl_hub_add_issue_comment "${issue_number}" "${message}" |
| 112 | + fi |
| 113 | + cat <<EOJ |
| 114 | +{ |
| 115 | + "action": "${action}", |
| 116 | + "issue_number": "${issue_number}", |
| 117 | + "issue_url": "${issue_url}", |
| 118 | + "issue_ref": "${owner_repo}#${issue_number}" |
| 119 | +} |
| 120 | +EOJ |
| 121 | +} |
0 commit comments