From 1ac1a897cddd5c0489d71241daa6e194d220a203 Mon Sep 17 00:00:00 2001 From: Quentin Monnet Date: Tue, 21 Mar 2023 22:46:38 +0000 Subject: [PATCH] mirror: Add script to udpate GitHub labels for some release assets Add a simple script relying on the "gh api" command to update release assets on GitHub, in order to set labels and make the list of assets easier to read. Signed-off-by: Quentin Monnet --- scripts/README.md | 19 ++++++- scripts/gh-label-release-assets.sh | 91 ++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100755 scripts/gh-label-release-assets.sh diff --git a/scripts/README.md b/scripts/README.md index 41793f0..a5f765c 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -2,6 +2,23 @@ This directory contains scripts for maintaining bpftool's GitHub mirror. +## gh-label-release-assets.sh + +This script can **add labels to GitHub release assets**. Labels are used in the +GitHub interface instead of the file name in the list of assets, but they do +not alter the download URL. + +The script takes the tag reference for the release as single parameter. The +repository to use, and the names and labels to set for assets are currently +defined in the script itself. + +It requires the [GitHub command line][gh] (`gh`). + +Note that only users with push access to the repository can update release +assets and set labels. + +[gh]: https://cli.github.com/ + ## sync-kernel.sh ### Synchronize Linux and bpftool mirror @@ -138,7 +155,7 @@ It performs the following steps: repository and the bpftool mirror. Then it looks for remaining differences between the two repositories, and warn the user if it finds any. Patches picked up from the `bpf` tree are usually a source of differences at this - step. If the patch containing the known differneces is to be updated after + step. If the patch containing the known differences is to be updated after the synchronization in progress, the user should do it at this time, before the temporary files from the script are deleted. diff --git a/scripts/gh-label-release-assets.sh b/scripts/gh-label-release-assets.sh new file mode 100755 index 0000000..4143a1e --- /dev/null +++ b/scripts/gh-label-release-assets.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +# Use this script to add labels to GitHub release assets for a given release. +# +# Based on the following console workflow: +# +# gh api \ +# '/repos/qmonnet/bpftool/releases/tags/v7.2.0-snapshot.0' \ +# --jq '.id' +# gh api \ +# '/repos/qmonnet/bpftool/releases/96330927/assets' \ +# --jq '.[] | select(.name == "bpftool-amd64.tar.gz").id' +# gh api \ +# --method PATCH \ +# -H "Accept: application/vnd.github+json" \ +# -H "X-GitHub-Api-Version: 2022-11-28" \ +# '/repos/qmonnet/bpftool/releases/assets/100280866' \ +# -f name='bpftool-arm64.tar.gz' \ +# -f label='Compressed binary (arm64)' + +REPO="libbpf/bpftool" + +usage() { + echo "Update asset labels for bpftool releases" + echo "Usage:" + echo " $0 [options] " + echo "" + echo "OPTIONS" + echo " -h display this help" + exit "$1" +} + +OPTIND=1 +while getopts "h" opt; do + case "$opt" in + h) + usage 0 + ;; + *) + usage 1 + ;; + esac +done +shift $((OPTIND-1)) +[[ "${1:-}" = "--" ]] && shift + +# Get release tag from command line +if [[ "$#" -lt 1 ]]; then + echo "error: missing release tag" + usage 1 +fi +release_tag="$1" +echo "repo: ${REPO}, release tag: ${release_tag}" + +# Add labels to set for given asset names here: +declare -A assets_labels=( + ["bpftool-libbpf-${release_tag}-sources.tar.gz"]="Source code, including libbpf submodule (tar.gz)" +) + +# Get release ID +release_id="$(gh api "/repos/${REPO}/releases/tags/${release_tag}" --jq '.id')" +echo " found release ID ${release_id}" + +# For each label to set, get asset ID, prompt user for confirmation, set label +for asset_name in "${!assets_labels[@]}"; do + asset_id="$(gh api "/repos/${REPO}/releases/${release_id}/assets" \ + --jq ".[] | select(.name == \"${asset_name}\").id")" + echo " found asset ID ${asset_id}" + + echo "asset '${asset_name}': add label '${assets_labels[${asset_name}]}'" + answer="" + read -rp 'proceed? [y/N]: ' answer + + case "${answer}" in + y|yes|Y|Yes|YES) + gh api \ + --method PATCH \ + -H 'Accept: application/vnd.github+json' \ + -H 'X-GitHub-Api-Version: 2022-11-28' \ + "/repos/${REPO}/releases/assets/${asset_id}" \ + -f label="${assets_labels[${asset_name}]}" + ;; + *) + echo "cancelled" + ;; + esac +done