Skip to content

Commit

Permalink
scripts: Add script to rename (add label to) release assets
Browse files Browse the repository at this point in the history
The only way I've found to add a label to existing release assets on
GitHub is via the GitHub API. Add a script to "rename" (add a label) to
existing assets.

In particular, we rename the archive containing the sources for veristat
and its libbpf submodule.

Example invocation (with REPO modified to point to my fork):

    $ ./scripts/gh-label-release-assets.sh test02
    repo: qmonnet/veristat, release tag: test02
      found release ID 101340990
      found asset ID 105863030
    asset 'veristat-libbpf-test02-sources.tar.gz': add label 'Source code, including libbpf submodule (tar.gz)'
    proceed? [y/N]: y
    [ ... JSON output from API request ... ]

After running the script, instead of being listed as
"veristat-libbpf-<tag>-sources.tar.gz", the asset appears on GitHub's
interface as "Source code, including libbpf submodule (tar.gz)". More
assets could easily be renamed as well by extending the array in the
script.

The script is not run from CI, because it requires the release to be
published (we only automated the creation of the draft release).

The script requires the GitHub command line ("gh") to be set up. If the
asset is found but the update fails with a 404 error, this is usually
synonym of unsufficient permissions for the GitHub token in use.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
  • Loading branch information
qmonnet committed Apr 28, 2023
1 parent be3b6d9 commit 5ef0836
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions scripts/gh-label-release-assets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)

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/libbpf/veristat/releases/tags/<some_tag>' \
# --jq '.id'
# gh api \
# '/repos/libbpf/veristat/releases/<release_id>/assets' \
# --jq '.[] | select(.name == "<file_name>").id'
# gh api \
# --method PATCH \
# -H "Accept: application/vnd.github+json" \
# -H "X-GitHub-Api-Version: 2022-11-28" \
# '/repos/libbpf/veristat/releases/assets/<asset_id>' \
# -f name='<new_file_name>' \
# -f label='<new_name_in_asset_list>'

REPO="libbpf/veristat"

usage() {
echo "Update asset labels for veristat releases"
echo "Usage:"
echo " $0 [options] <release_tag>"
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=(
["veristat-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

0 comments on commit 5ef0836

Please sign in to comment.