Skip to content

Commit 1ac1a89

Browse files
committed
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 <[email protected]>
1 parent 98e3911 commit 1ac1a89

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

scripts/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
This directory contains scripts for maintaining bpftool's GitHub mirror.
44

5+
## gh-label-release-assets.sh
6+
7+
This script can **add labels to GitHub release assets**. Labels are used in the
8+
GitHub interface instead of the file name in the list of assets, but they do
9+
not alter the download URL.
10+
11+
The script takes the tag reference for the release as single parameter. The
12+
repository to use, and the names and labels to set for assets are currently
13+
defined in the script itself.
14+
15+
It requires the [GitHub command line][gh] (`gh`).
16+
17+
Note that only users with push access to the repository can update release
18+
assets and set labels.
19+
20+
[gh]: https://cli.github.com/
21+
522
## sync-kernel.sh
623

724
### Synchronize Linux and bpftool mirror
@@ -138,7 +155,7 @@ It performs the following steps:
138155
repository and the bpftool mirror. Then it looks for remaining differences
139156
between the two repositories, and warn the user if it finds any. Patches
140157
picked up from the `bpf` tree are usually a source of differences at this
141-
step. If the patch containing the known differneces is to be updated after
158+
step. If the patch containing the known differences is to be updated after
142159
the synchronization in progress, the user should do it at this time, before
143160
the temporary files from the script are deleted.
144161

scripts/gh-label-release-assets.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
# Use this script to add labels to GitHub release assets for a given release.
8+
#
9+
# Based on the following console workflow:
10+
#
11+
# gh api \
12+
# '/repos/qmonnet/bpftool/releases/tags/v7.2.0-snapshot.0' \
13+
# --jq '.id'
14+
# gh api \
15+
# '/repos/qmonnet/bpftool/releases/96330927/assets' \
16+
# --jq '.[] | select(.name == "bpftool-amd64.tar.gz").id'
17+
# gh api \
18+
# --method PATCH \
19+
# -H "Accept: application/vnd.github+json" \
20+
# -H "X-GitHub-Api-Version: 2022-11-28" \
21+
# '/repos/qmonnet/bpftool/releases/assets/100280866' \
22+
# -f name='bpftool-arm64.tar.gz' \
23+
# -f label='Compressed binary (arm64)'
24+
25+
REPO="libbpf/bpftool"
26+
27+
usage() {
28+
echo "Update asset labels for bpftool releases"
29+
echo "Usage:"
30+
echo " $0 [options] <release_tag>"
31+
echo ""
32+
echo "OPTIONS"
33+
echo " -h display this help"
34+
exit "$1"
35+
}
36+
37+
OPTIND=1
38+
while getopts "h" opt; do
39+
case "$opt" in
40+
h)
41+
usage 0
42+
;;
43+
*)
44+
usage 1
45+
;;
46+
esac
47+
done
48+
shift $((OPTIND-1))
49+
[[ "${1:-}" = "--" ]] && shift
50+
51+
# Get release tag from command line
52+
if [[ "$#" -lt 1 ]]; then
53+
echo "error: missing release tag"
54+
usage 1
55+
fi
56+
release_tag="$1"
57+
echo "repo: ${REPO}, release tag: ${release_tag}"
58+
59+
# Add labels to set for given asset names here:
60+
declare -A assets_labels=(
61+
["bpftool-libbpf-${release_tag}-sources.tar.gz"]="Source code, including libbpf submodule (tar.gz)"
62+
)
63+
64+
# Get release ID
65+
release_id="$(gh api "/repos/${REPO}/releases/tags/${release_tag}" --jq '.id')"
66+
echo " found release ID ${release_id}"
67+
68+
# For each label to set, get asset ID, prompt user for confirmation, set label
69+
for asset_name in "${!assets_labels[@]}"; do
70+
asset_id="$(gh api "/repos/${REPO}/releases/${release_id}/assets" \
71+
--jq ".[] | select(.name == \"${asset_name}\").id")"
72+
echo " found asset ID ${asset_id}"
73+
74+
echo "asset '${asset_name}': add label '${assets_labels[${asset_name}]}'"
75+
answer=""
76+
read -rp 'proceed? [y/N]: ' answer
77+
78+
case "${answer}" in
79+
y|yes|Y|Yes|YES)
80+
gh api \
81+
--method PATCH \
82+
-H 'Accept: application/vnd.github+json' \
83+
-H 'X-GitHub-Api-Version: 2022-11-28' \
84+
"/repos/${REPO}/releases/assets/${asset_id}" \
85+
-f label="${assets_labels[${asset_name}]}"
86+
;;
87+
*)
88+
echo "cancelled"
89+
;;
90+
esac
91+
done

0 commit comments

Comments
 (0)