2023-03-26 11:41:37 -04:00
|
|
|
#!/bin/bash
|
2023-04-01 05:12:56 -04:00
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-26 11:41:37 -04:00
|
|
|
|
2023-03-29 11:16:26 -04:00
|
|
|
set -e
|
|
|
|
|
|
|
|
if ${VERBOSE:-false}; then set -x; fi
|
2023-03-26 11:41:37 -04:00
|
|
|
|
2023-03-26 17:03:56 -04:00
|
|
|
: ${FORGEJO:=https://codeberg.org}
|
2023-03-29 11:16:26 -04:00
|
|
|
: ${REPO:=forgejo-integration/forgejo}
|
2023-03-26 11:41:37 -04:00
|
|
|
: ${RELEASE_DIR:=dist/release}
|
2023-03-29 11:16:26 -04:00
|
|
|
: ${BIN_DIR:=$(mktemp -d)}
|
2023-03-26 11:41:37 -04:00
|
|
|
: ${TEA_VERSION:=0.9.0}
|
|
|
|
|
|
|
|
setup_tea() {
|
|
|
|
if ! test -f $BIN_DIR/tea ; then
|
|
|
|
curl -sL https://dl.gitea.io/tea/$TEA_VERSION/tea-$TEA_VERSION-linux-amd64 > $BIN_DIR/tea
|
|
|
|
chmod +x $BIN_DIR/tea
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
ensure_tag() {
|
2023-03-29 11:16:26 -04:00
|
|
|
if api GET repos/$REPO/tags/$TAG > /tmp/tag.json ; then
|
2023-03-26 11:41:37 -04:00
|
|
|
local sha=$(jq --raw-output .commit.sha < /tmp/tag.json)
|
2023-03-29 11:16:26 -04:00
|
|
|
if test "$sha" != "$SHA" ; then
|
2023-03-26 11:41:37 -04:00
|
|
|
cat /tmp/tag.json
|
2023-03-29 11:16:26 -04:00
|
|
|
echo "the tag SHA in the $REPO repository does not match the tag SHA that triggered the build: $SHA"
|
2023-03-26 11:41:37 -04:00
|
|
|
false
|
|
|
|
fi
|
|
|
|
else
|
2023-03-29 11:16:26 -04:00
|
|
|
api POST repos/$REPO/tags --data-raw '{"tag_name": "'$TAG'", "target": "'$SHA'"}'
|
2023-03-26 11:41:37 -04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-29 11:16:26 -04:00
|
|
|
upload_release() {
|
|
|
|
local assets=$(ls $RELEASE_DIR/* | sed -e 's/^/-a /')
|
|
|
|
local releasetype
|
|
|
|
echo "${TAG}" | grep -qi '\-rc' && export releasetype="--prerelease" && echo "Uploading as Pre-Release"
|
|
|
|
echo "${TAG}" | grep -qi '\-test' && export releasetype="--draft" && echo "Uploading as Draft"
|
|
|
|
test ${releasetype+false} || echo "Uploading as Stable"
|
2023-03-26 11:41:37 -04:00
|
|
|
ensure_tag
|
2023-03-29 11:16:26 -04:00
|
|
|
anchor=$(echo $TAG | sed -e 's/^v//' -e 's/[^a-zA-Z0-9]/-/g')
|
|
|
|
$BIN_DIR/tea release create $assets --repo $REPO --note "$RELEASENOTES" --tag $TAG --title $TAG ${releasetype}
|
2023-03-26 11:41:37 -04:00
|
|
|
}
|
|
|
|
|
2023-03-29 11:16:26 -04:00
|
|
|
upload() {
|
2023-03-26 11:41:37 -04:00
|
|
|
setup_api
|
|
|
|
setup_tea
|
2023-03-30 18:09:26 -04:00
|
|
|
GITEA_SERVER_TOKEN=$TOKEN $BIN_DIR/tea login add --url $FORGEJO
|
2023-03-29 11:16:26 -04:00
|
|
|
upload_release
|
2023-03-26 11:41:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setup_api() {
|
2023-03-26 17:03:56 -04:00
|
|
|
if ! which jq curl ; then
|
2023-03-30 09:22:00 -04:00
|
|
|
apt-get -qq update
|
2023-03-29 19:08:18 -04:00
|
|
|
apt-get install -y -qq jq curl wget
|
2023-03-26 11:41:37 -04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
api() {
|
|
|
|
method=$1
|
|
|
|
shift
|
|
|
|
path=$1
|
|
|
|
shift
|
|
|
|
|
2023-03-29 11:16:26 -04:00
|
|
|
curl --fail -X $method -sS -H "Content-Type: application/json" -H "Authorization: token $TOKEN" "$@" $FORGEJO/api/v1/$path
|
2023-03-26 11:41:37 -04:00
|
|
|
}
|
|
|
|
|
2023-03-29 11:16:26 -04:00
|
|
|
download() {
|
2023-03-26 11:41:37 -04:00
|
|
|
setup_api
|
|
|
|
(
|
|
|
|
mkdir -p $RELEASE_DIR
|
|
|
|
cd $RELEASE_DIR
|
2023-03-29 11:16:26 -04:00
|
|
|
api GET repos/$REPO/releases/tags/$TAG > /tmp/assets.json
|
2023-03-26 11:41:37 -04:00
|
|
|
jq --raw-output '.assets[] | "\(.name) \(.browser_download_url)"' < /tmp/assets.json | while read name url ; do
|
|
|
|
wget --quiet -O $name $url
|
|
|
|
done
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
missing() {
|
2023-03-29 11:16:26 -04:00
|
|
|
echo need upload or download argument got nothing
|
2023-03-26 11:41:37 -04:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
${@:-missing}
|