From 0d5d1c8c36e5708c389e4cc3b156733d773a9815 Mon Sep 17 00:00:00 2001 From: stevenhorsman Date: Wed, 3 Jan 2024 12:10:09 +0000 Subject: [PATCH] ci: Add gh-util.sh script - The hub tool is now deprecated, so introduce a new alternative to `hub-util.sh` https://github.com/kata-containers/.github/blob/main/scripts/hub-util.sh that works with it. Initially I've only started with the couple of commands that we use regularly, but we can extend it in future. - Expects jq to be installed and `gh` to be installed an setup (see [1]) - Now we don't have lots of repos, I've moved it into `kata-containers` rather than `.github`, so it is more visible. Fixes: #8125 [1] https://docs.github.com/en/github-cli/github-cli/quickstart#prerequisites Signed-off-by: stevenhorsman --- ci/gh-util.sh | 182 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100755 ci/gh-util.sh diff --git a/ci/gh-util.sh b/ci/gh-util.sh new file mode 100755 index 0000000000..3ab62b9b81 --- /dev/null +++ b/ci/gh-util.sh @@ -0,0 +1,182 @@ +#!/bin/bash + +# Copyright (c) 2020 Intel Corporation +# Copyright (c) 2024 IBM Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +set -o errexit +set -o errtrace +set -o nounset +set -o pipefail + +[ -n "${DEBUG:-}" ] && set -o xtrace + +script_name=${0##*/} + +#--------------------------------------------------------------------- + +die() +{ + echo >&2 "$*" + exit 1 +} + +usage() +{ + cat < + list-labels-for-issue + +Options: + + -h Show this help statement. + -r Optional specification. Default: 'kata-containers/kata-containers' + +Examples: + +- List issues for a Pull Request 123 in kata-containers/kata-containers repo + + $ $script_name list-issues-for-pr 123 +EOF +} + +list_issues_for_pr() +{ + local pr="${1:-}" + local repo="${2:-kata-containers/kata-containers}" + + [ -z "$pr" ] && die "need PR" + + local commits=$(gh pr view ${pr} --repo ${repo} --json commits --jq .commits[].messageBody) + + [ -z "$commits" ] && die "cannot determine commits for PR $pr" + + # Extract the issue number(s) from the commits. + # + # This needs to be careful to take account of lines like this: + # + # fixes 99 + # fixes: 77 + # fixes #123. + # Fixes: #1, #234, #5678. + # + # Note the exclusion of lines starting with whitespace which is + # specifically to ignore vendored git log comments, which are whitespace + # indented and in the format: + # + # " " + # + local issues=$(echo "$commits" |\ + egrep -v "^( | )" |\ + egrep -i "fixes:* *(#*[0-9][0-9]*)" |\ + tr ' ' '\n' |\ + grep "[0-9][0-9]*" |\ + sed 's/[.,\#]//g' |\ + sort -nu || true) + + [ -z "$issues" ] && die "cannot determine issues for PR $pr" + + echo "# Issues linked to PR" + echo "#" + echo "# Fields: issue_number" + + local issue + echo "$issues"|while read issue + do + printf "%s\n" "$issue" + done +} + +list_labels_for_issue() +{ + local issue="${1:-}" + + [ -z "$issue" ] && die "need issue number" + + local labels=$(gh issue view ${issue} --repo kata-containers/kata-containers --json labels) + + [ -z "$labels" ] && die "cannot determine labels for issue $issue" + + printf "$labels" +} + +setup() +{ + for cmd in gh jq + do + command -v "$cmd" &>/dev/null || die "need command: $cmd" + done +} + +handle_args() +{ + setup + + local show_all="false" + local opt + + while getopts "ahr:" opt "$@" + do + case "$opt" in + a) show_all="true" ;; + h) usage && exit 0 ;; + r) repo="${OPTARG}" ;; + esac + done + + shift $(($OPTIND - 1)) + + local repo="${repo:-kata-containers/kata-containers}" + local cmd="${1:-}" + + case "$cmd" in + list-issues-for-pr) ;; + list-labels-for-issue) ;; + + "") usage && exit 0 ;; + *) die "invalid command: '$cmd'" ;; + esac + + # Consume the command name + shift + + local issue="" + local pr="" + + case "$cmd" in + list-issues-for-pr) + pr="${1:-}" + + list_issues_for_pr "$pr" "${repo}" + ;; + + list-labels-for-issue) + issue="${1:-}" + + list_labels_for_issue "$issue" + ;; + + *) die "impossible situation: cmd: '$cmd'" ;; + esac + + exit 0 +} + +main() +{ + handle_args "$@" +} + +main "$@"