76 lines
3.7 KiB
Bash
76 lines
3.7 KiB
Bash
#!/usr/bin/env sh
|
|||
|
|
set -eu
|
||
|
|
|
||
|
|
version=${1:?Usage: scripts/release.sh X.Y.Z}
|
||
|
|
: "${GITEA_TOKEN:?GITEA_TOKEN secret is required}"
|
||
|
|
: "${GITEA_API_URL:?GITEA_API_URL is required}"
|
||
|
|
: "${GITEA_REPOSITORY:?GITEA_REPOSITORY must be owner/repository}"
|
||
|
|
: "${GITEA_REPOSITORY_URL:?GITEA_REPOSITORY_URL is required}"
|
||
|
|
printf '%s\n' "$version" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$' || { echo 'Release version must be X.Y.Z.' >&2; exit 2; }
|
||
|
|
|
||
|
|
GITEA_USERNAME=$(curl --fail --silent --show-error -H "Authorization: token $GITEA_TOKEN" "$GITEA_API_URL/user" | jq -r '.login')
|
||
|
|
export GITEA_USERNAME
|
||
|
|
git_push() {
|
||
|
|
askpass=$(mktemp)
|
||
|
|
cat > "$askpass" <<'ASKPASS'
|
||
|
|
#!/usr/bin/env sh
|
||
|
|
case "$1" in
|
||
|
|
*Username*) printf '%s\n' "$GITEA_USERNAME" ;;
|
||
|
|
*) printf '%s\n' "$GITEA_TOKEN" ;;
|
||
|
|
esac
|
||
|
|
ASKPASS
|
||
|
|
chmod 700 "$askpass"
|
||
|
|
if GIT_ASKPASS="$askpass" GIT_TERMINAL_PROMPT=0 git -c credential.helper= -c http.extraheader= push origin "$@"; then result=0; else result=$?; fi
|
||
|
|
rm -f "$askpass"
|
||
|
|
return "$result"
|
||
|
|
}
|
||
|
|
|
||
|
|
latest_subject=$(git log -1 --format=%s)
|
||
|
|
release_commit_version=$(printf '%s' "$latest_subject" | sed -n 's/^chore(release): v\([0-9][0-9.]*\).*/\1/p')
|
||
|
|
tag_exists=false
|
||
|
|
if git rev-parse "v$version" >/dev/null 2>&1; then tag_exists=true; fi
|
||
|
|
|
||
|
|
if [ "$tag_exists" = false ]; then
|
||
|
|
if [ "$release_commit_version" = "$version" ]; then
|
||
|
|
git tag "v$version" HEAD
|
||
|
|
git_push "v$version"
|
||
|
|
elif [ -n "$release_commit_version" ]; then
|
||
|
|
echo "HEAD is already a release commit for v$release_commit_version." >&2
|
||
|
|
exit 1
|
||
|
|
else
|
||
|
|
sh scripts/changelog.sh "$version"
|
||
|
|
git config user.name "Gitea Actions"
|
||
|
|
git config user.email "actions@users.noreply.gitea.local"
|
||
|
|
git add CHANGELOG.md
|
||
|
|
git commit -m "chore(release): v$version [skip ci]"
|
||
|
|
git tag "v$version"
|
||
|
|
git_push "HEAD:${GITEA_MAIN_BRANCH:-main}" "v$version"
|
||
|
|
fi
|
||
|
|
else
|
||
|
|
tagged_subject=$(git log -1 --format=%s "v$version")
|
||
|
|
case "$tagged_subject" in
|
||
|
|
"chore(release): v$version"*) ;;
|
||
|
|
*) echo "Tag v$version does not point at its changelog release commit." >&2; exit 1 ;;
|
||
|
|
esac
|
||
|
|
sh scripts/render-readme.sh "$version"
|
||
|
|
sh ./mvnw -B -Drevision="$version" package
|
||
|
|
fi
|
||
|
|
|
||
|
|
release_body=$(awk -v heading="## [$version] -" 'index($0, heading) == 1 {found=1; next} found && /^## / {exit} found {print}' CHANGELOG.md)
|
||
|
|
payload=$(jq -n --arg tag "v$version" --arg name "v$version" --arg body "$release_body" '{tag_name:$tag,name:$name,body:$body,draft:false,prerelease:false}')
|
||
|
|
existing_release=$(curl --silent -H "Authorization: token $GITEA_TOKEN" "$GITEA_API_URL/repos/$GITEA_REPOSITORY/releases/tags/v$version")
|
||
|
|
if printf '%s' "$existing_release" | jq -e '.id' >/dev/null 2>&1; then
|
||
|
|
release_id=$(printf '%s' "$existing_release" | jq -r '.id')
|
||
|
|
echo "Gitea release v$version already exists; checking its JAR attachment."
|
||
|
|
else
|
||
|
|
release_json=$(curl --fail --silent --show-error -H "Authorization: token $GITEA_TOKEN" -H 'Content-Type: application/json' -d "$payload" "$GITEA_API_URL/repos/$GITEA_REPOSITORY/releases")
|
||
|
|
release_id=$(printf '%s' "$release_json" | jq -r '.id')
|
||
|
|
fi
|
||
|
|
jar=$(find target -maxdepth 1 -name "sonar-bifrost-plugin-$version.jar" -print -quit)
|
||
|
|
[ -n "$jar" ] || { echo 'Release JAR is missing.' >&2; exit 1; }
|
||
|
|
attachments=$(curl --fail --silent --show-error -H "Authorization: token $GITEA_TOKEN" "$GITEA_API_URL/repos/$GITEA_REPOSITORY/releases/$release_id/assets")
|
||
|
|
if printf '%s' "$attachments" | jq -e --arg name "$(basename "$jar")" '.[]? | select(.name == $name)' >/dev/null; then
|
||
|
|
echo "Release v$version already has $(basename "$jar")."
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
curl --fail --silent --show-error -H "Authorization: token $GITEA_TOKEN" -H 'Content-Type: application/octet-stream' --data-binary "@$jar" "$GITEA_API_URL/repos/$GITEA_REPOSITORY/releases/$release_id/assets?name=$(basename "$jar")" >/dev/null
|