Skip to content

Commit

Permalink
mirror: Add script to udpate GitHub labels for some release assets
Browse files Browse the repository at this point in the history
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 <quentin@isovalent.com>
  • Loading branch information
qmonnet committed Mar 22, 2023
1 parent 98e3911 commit 1ac1a89
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
19 changes: 18 additions & 1 deletion scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
91 changes: 91 additions & 0 deletions scripts/gh-label-release-assets.sh
Original file line number Diff line number Diff line change
@@ -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] <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=(
["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

0 comments on commit 1ac1a89

Please sign in to comment.