Skip to content

Commit

Permalink
fix setup-util-jq regression, support tagged releases in github-download
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Aug 15, 2023
1 parent 63d1940 commit 347e1c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
72 changes: 31 additions & 41 deletions commands/github-download
Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,32 @@ function github_download() (
# Action

# release helpers
function get_release_ref {
# output the reference for the release
local url="$GITHUB_API_URL/repos/$slug/releases/$release"
if command-missing jq; then
# don't use jq, rg, or sd to accomplish this, as none of them would be installed
# sed fixes minified json such as `},{` and `","`
fetch "$url" | sed -E $'s/,/,\\\n/g' | grep --fixed-strings --regexp='"tag_name":' | sed -E 's/.+: *"(.+)".*/\1/'
else
fetch "$url" | jq -r '.tag_name'
fi
}
function get_release_assets_raw {
function get_assets_for_release {
# output the assets [name, url] for the release
local url="$GITHUB_API_URL/repos/$slug/releases/$release"
# github urls always return 200, even if msising
# when [release=latest], then [/releases/latest] works but [/releases/latest/assets] does not work
# as [/releases/$release] returns asset information, use that with .assets[] instead of .[]
local url
url="$GITHUB_API_URL/repos/$slug/releases/$release"
if fetch "$url" | grep --quiet --fixed-strings --regexp='Not Found'; then
url="$(
fetch "$GITHUB_API_URL/repos/$slug/releases" | jq -r ".[] | select(.tag_name==\"$release\") | .url"
)"
fi
if command-missing jq; then
# don't use jq, rg, or sd to accomplish this, as none of them would be installed
# [tail -n+2] excludes the first line, which is the name of the repo
# this is fragile as if the order of arguments change, it is all over
# sed fixes minified json such as `},{` and `","`
fetch "$url" | sed -E $'s/,/,\\\n/g' | grep --extended-regexp --regexp='"(name|browser_download_url)":' | tail -n+2 | sed -E 's/.+: *"(.+)".*/\1/'
else
fetch "$url" | jq -r ".assets[] | (.name, .browser_download_url)"
fetch "$url" | jq -r '.assets[] | (.name, .browser_download_url)'
fi
}
function get_release_assets {
function get_matched_assets_for_release {
# get the assets [name, url] for the release
local assets=()
mapfile -t assets < <(get_release_assets_raw)
mapfile -t assets < <(get_assets_for_release)
if test "${#assets[@]}" -eq 0; then
echo-style --error='No release assets were found.'
return 1
Expand Down Expand Up @@ -242,26 +240,9 @@ function github_download() (
}

# release/reference helpers
function download_ref {
local url ref="$reference" filter
url="https://github.com/$slug/archive/$ref.tar.gz"
filter="*-$ref/${unzip_filter:-"*"}"
if test "$dry" = 'yes'; then
echo "$url"
return 0
fi
down "$url" \
--quiet="$quiet" \
--unzip-format="$unzip_format" \
--unzip-filter="$filter" \
--directory="$directory" \
--file="$file" \
--path="$path"
}
function download_release {
local url ref filter
ref="$(get_release_ref)"
url="https://github.com/$slug/archive/$ref.tar.gz"
function download_reference {
local url filter
url="https://github.com/$slug/archive/$reference.tar.gz"
filter="*-$reference/${unzip_filter:-"*"}"
if test "$dry" = 'yes'; then
echo "$url"
Expand All @@ -277,7 +258,7 @@ function github_download() (
}
function download_asset {
local asset assets
mapfile -t assets < <(get_release_assets)
mapfile -t assets < <(get_matched_assets_for_release)
if test "${#assets[@]}" -eq 0; then
echo-style --error='No download assets were found.'
return 1
Expand Down Expand Up @@ -319,10 +300,8 @@ function github_download() (
download_asset
elif test -n "$pathname"; then
download_pathname
elif test -n "$release"; then
download_release
elif test -n "$reference"; then
download_ref
download_reference
else
help "Invalid combination of options."
fi
Expand All @@ -343,13 +322,24 @@ function github_download() (
# fi
# done

# function get_reference_for_release {
# # output the reference for the release
# local url="$GITHUB_API_URL/repos/$slug/releases/$release"
# if command-missing jq; then
# # don't use jq, rg, or sd to accomplish this, as none of them would be installed
# # sed fixes minified json such as `},{` and `","`
# fetch "$url" | sed -E $'s/,/,\\\n/g' | grep --fixed-strings --regexp='"id":' | sed -E 's/.+: *"?(.+)"?,.*/\1/'
# else
# fetch "$url" | jq -r '.id'
# fi
# }
# function get_reference_clone {
# # then shallow clone it to the [--inside] directory
# git clone --quiet --depth 1 --branch "$reference" 'https://github.com/rfjakob/gocryptfs.git' "$inside"
# }
# function get_release_clone {
# # get the reference for the release
# local reference="$(get_release_ref)"
# local reference="$(get_reference_for_release)"

# # then shallow clone it to the [--inside] directory
# git clone --quiet --depth 1 --branch "$reference" 'https://github.com/rfjakob/gocryptfs.git' "$inside"
Expand Down
6 changes: 3 additions & 3 deletions commands/setup-util-jq
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ function setup_util_jq() (
ZYPPER='jq'
)
function get_github_asset_url {
# use rc release, as it supports more targets than 1.6, change back to [latest] when 1.7 is released
# use 'jq-1.7rc1' release, as it supports more targets than 1.6, change back to [latest] when 1.7 is released
github-download \
--dry \
--slug='stedolan/jq' \
--release='jq-1.7rc1' \
--slug='jqlang/jq' \
--release='114428721' \
--asset-filter="$(echo-escape-regex "$1")$" | echo-first-line || :
}
function add_download_option {
Expand Down

0 comments on commit 347e1c4

Please sign in to comment.