|
| 1 | +#!/hint/bash |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +[[ -z ${DEVTOOLS_INCLUDE_ISSUE_MOVE_SH:-} ]] || return 0 |
| 6 | +DEVTOOLS_INCLUDE_ISSUE_MOVE_SH=1 |
| 7 | + |
| 8 | +_DEVTOOLS_LIBRARY_DIR=${_DEVTOOLS_LIBRARY_DIR:-@pkgdatadir@} |
| 9 | + |
| 10 | +# shellcheck source=src/lib/common.sh |
| 11 | +source "${_DEVTOOLS_LIBRARY_DIR}"/lib/common.sh |
| 12 | +# shellcheck source=src/lib/cache.sh |
| 13 | +source "${_DEVTOOLS_LIBRARY_DIR}"/lib/cache.sh |
| 14 | +# shellcheck source=src/lib/api/gitlab.sh |
| 15 | +source "${_DEVTOOLS_LIBRARY_DIR}"/lib/api/gitlab.sh |
| 16 | + |
| 17 | +set -eo pipefail |
| 18 | + |
| 19 | + |
| 20 | +pkgctl_issue_move_usage() { |
| 21 | + local -r COMMAND=${_DEVTOOLS_COMMAND:-${BASH_SOURCE[0]##*/}} |
| 22 | + cat <<- _EOF_ |
| 23 | + Usage: ${COMMAND} [OPTIONS] [IID] [DESTINATION_PACKAGE] |
| 24 | +
|
| 25 | + The move command allows users to transfer an issue from one project to another |
| 26 | + within the Arch Linux packaging group. This is useful when an issue is |
| 27 | + identified to be more relevant or better handled in a different project. |
| 28 | +
|
| 29 | + By default, the command operates within the current directory, but users can |
| 30 | + specify a different package base from which to move the issue. |
| 31 | +
|
| 32 | + Users must specify the issue ID (IID) and the destination package to which the |
| 33 | + issue should be moved. A comment message explaining the reason for the move can |
| 34 | + be provided directly through the command line. |
| 35 | +
|
| 36 | + OPTIONS |
| 37 | + -p, --package PKGBASE Move from <pkgbase> instead of the current directory |
| 38 | + -m, --message MSG Use the provided message as the comment |
| 39 | + -e, --edit Edit the comment using an editor |
| 40 | + -h, --help Show this help text |
| 41 | +
|
| 42 | + EXAMPLES |
| 43 | + $ ${COMMAND} 42 to-bar |
| 44 | + $ ${COMMAND} --package from-foo 42 to-bar |
| 45 | +_EOF_ |
| 46 | +} |
| 47 | + |
| 48 | +pkgctl_issue_move() { |
| 49 | + if (( $# < 1 )); then |
| 50 | + pkgctl_issue_move_usage |
| 51 | + exit 0 |
| 52 | + fi |
| 53 | + |
| 54 | + local iid="" |
| 55 | + local pkgbase="" |
| 56 | + local message="" |
| 57 | + local edit=0 |
| 58 | + |
| 59 | + local to_project_name to_project_id project_path issue_url to_iid result |
| 60 | + |
| 61 | + # option checking |
| 62 | + while (( $# )); do |
| 63 | + case $1 in |
| 64 | + -h|--help) |
| 65 | + pkgctl_issue_move_usage |
| 66 | + exit 0 |
| 67 | + ;; |
| 68 | + -p|--package) |
| 69 | + (( $# <= 1 )) && die "missing argument for %s" "$1" |
| 70 | + pkgbase=$2 |
| 71 | + shift 2 |
| 72 | + ;; |
| 73 | + -m|--message) |
| 74 | + (( $# <= 1 )) && die "missing argument for %s" "$1" |
| 75 | + message=$2 |
| 76 | + shift 2 |
| 77 | + ;; |
| 78 | + -e|--edit) |
| 79 | + edit=1 |
| 80 | + shift |
| 81 | + ;; |
| 82 | + -*) |
| 83 | + die "invalid argument: %s" "$1" |
| 84 | + ;; |
| 85 | + *) |
| 86 | + break |
| 87 | + ;; |
| 88 | + esac |
| 89 | + done |
| 90 | + |
| 91 | + if [[ -z ${pkgbase} ]]; then |
| 92 | + if ! [[ -f PKGBUILD ]]; then |
| 93 | + die "missing --package option or PKGBUILD in current directory" |
| 94 | + fi |
| 95 | + pkgbase=$(realpath --canonicalize-existing .) |
| 96 | + fi |
| 97 | + pkgbase=$(basename "${pkgbase}") |
| 98 | + |
| 99 | + if (( $# < 2 )); then |
| 100 | + pkgctl_issue_move_usage |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + |
| 104 | + iid=$1 |
| 105 | + to_project_name=$(basename "$2") |
| 106 | + |
| 107 | + # spawn editor |
| 108 | + if (( edit )); then |
| 109 | + msgfile=$(mktemp --tmpdir="${WORKDIR}" pkgctl-issue-note.XXXXXXXXXX.md) |
| 110 | + printf "%s\n" "${message}" >> "${msgfile}" |
| 111 | + if [[ -n $VISUAL ]]; then |
| 112 | + $VISUAL "${msgfile}" || die |
| 113 | + elif [[ -n $EDITOR ]]; then |
| 114 | + $EDITOR "${msgfile}" || die |
| 115 | + else |
| 116 | + die "No usable editor found (tried \$VISUAL, \$EDITOR)." |
| 117 | + fi |
| 118 | + message=$(cat "${msgfile}") |
| 119 | + fi |
| 120 | + |
| 121 | + if ! result=$(gitlab_project "${to_project_name}"); then |
| 122 | + msg_error "Failed to query target project ${BOLD}${to_project_name}${ALL_OFF}" |
| 123 | + exit 1 |
| 124 | + fi |
| 125 | + |
| 126 | + if ! to_project_id=$(jq --raw-output ".id" <<< "${result}"); then |
| 127 | + msg_error "Failed to query project id for ${BOLD}${to_project_name}${ALL_OFF}" |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | + |
| 131 | + # comment on issue |
| 132 | + if [[ -n ${message} ]]; then |
| 133 | + if ! result=$(gitlab_create_project_issue_note "${pkgbase}" "${iid}" "${message}"); then |
| 134 | + msg_error "Failed to comment on issue ${BOLD}#${iid}${ALL_OFF}" |
| 135 | + exit 1 |
| 136 | + fi |
| 137 | + msg_success "Commented on issue ${BOLD}#${iid}${ALL_OFF}" |
| 138 | + fi |
| 139 | + |
| 140 | + if ! result=$(gitlab_project_issue_move "${pkgbase}" "${iid}" "${to_project_id}"); then |
| 141 | + msg_error "Failed to move issue ${BOLD}#${iid}${ALL_OFF} to ${BOLD}${to_project_name}${ALL_OFF}" |
| 142 | + exit 1 |
| 143 | + fi |
| 144 | + |
| 145 | + if ! to_iid=$(jq --raw-output ".iid" <<< "${result}"); then |
| 146 | + msg_error "Failed to query issue id for ${BOLD}${to_project_name}${ALL_OFF}" |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | + |
| 150 | + project_path=$(gitlab_project_name_to_path "${to_project_name}") |
| 151 | + issue_url="${GIT_PACKAGING_URL_HTTPS}/${project_path}/-/issues/${to_iid}" |
| 152 | + |
| 153 | + msg_success "Moved issue ${BOLD}${pkgbase}${ALL_OFF} ${BOLD}#${iid}${ALL_OFF} to ${BOLD}${to_project_name}${ALL_OFF} ${BOLD}#${to_iid}${ALL_OFF}" |
| 154 | + echo |
| 155 | + printf "%sView this issue on GitLab: %s%s\n" "${GRAY}" "${issue_url}" "${ALL_OFF}" |
| 156 | +} |
0 commit comments