|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: GPL-2.0-or-later WITH Classpath-exception-2.0 |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +if [ $# -ne 4 ]; then |
| 7 | + echo "Usage: ${0} NAME VERSION REMOTE_NAME DIST_FILE" 1>&2 |
| 8 | + exit 1 |
| 9 | +fi |
| 10 | +NAME="${1}" |
| 11 | +VERSION="${2}" |
| 12 | +REMOTE_NAME="${3}" |
| 13 | +DIST_FILE="${4}" |
| 14 | + |
| 15 | +# Tag the current version and push the tag |
| 16 | +if [ "$(git status --porcelain | wc -l)" -ne 0 ]; then |
| 17 | + echo "will not tag: the working tree must be clean" 1>&2 |
| 18 | + exit 2 |
| 19 | +fi |
| 20 | +release_name="${NAME} v${VERSION}" |
| 21 | +git tag -s "${VERSION}" -m "${release_name}" |
| 22 | +git push "${REMOTE_NAME}" tag "${VERSION}" |
| 23 | + |
| 24 | +# Obtain GitHub data required to use the REST API |
| 25 | +gh_token="$(echo -e 'protocol=https\nhost=github.com' | |
| 26 | + /usr/libexec/git-core/git-credential-libsecret get | |
| 27 | + sed -n s/password=//p)" |
| 28 | +if [ -z "${gh_token}" ]; then |
| 29 | + echo "could not get the GitHub token" 1>&2 |
| 30 | + exit 3 |
| 31 | +fi |
| 32 | + |
| 33 | +remote_url="$(git remote get-url "${REMOTE_NAME}")" |
| 34 | +if [ -z "${remote_url}" ]; then |
| 35 | + echo "could not get the git remote url for ${REMOTE_NAME}" 1>&2 |
| 36 | + exit 4 |
| 37 | +fi |
| 38 | +owner="$(echo "${remote_url%*.git}" | tr / '\n' | tail -2 | head -1)" |
| 39 | +repo="$(echo "${remote_url%*.git}" | tr / '\n' | tail -1)" |
| 40 | + |
| 41 | +# Publish GitHub release |
| 42 | +COMMON_HEADERS=( |
| 43 | + -L -X POST -H "Authorization: Bearer ${gh_token}" |
| 44 | + -H "Accept: application/vnd.github+json" |
| 45 | + -H "X-GitHub-Api-Version: 2022-11-28" |
| 46 | +) |
| 47 | +body="Note: \`${DIST_FILE}\` is the official release source, \`*.zip\`" |
| 48 | +body="${body} and \`*.tar.gz\` files are empty on purpose, since GitHub" |
| 49 | +body="${body} doesn't allow deleting them." |
| 50 | +data="$( |
| 51 | + cat <<-EOF |
| 52 | + { |
| 53 | + "tag_name": "${VERSION}", |
| 54 | + "name": "${release_name}", |
| 55 | + "body": "${body}" |
| 56 | + } |
| 57 | + EOF |
| 58 | +)" |
| 59 | +url="https://api.github.com/repos/${owner}/${repo}/releases" |
| 60 | +resp="$(curl "${COMMON_HEADERS[@]}" "${url}" --data "${data}")" |
| 61 | +echo "${resp}" |
| 62 | + |
| 63 | +# Upload dist file |
| 64 | +upload_url="$(python3 '-BIScimport json, sys |
| 65 | +print(json.load(sys.stdin)["upload_url"].split("{")[0])' <<<"${resp}")" |
| 66 | +curl "${COMMON_HEADERS[@]}" -H "Content-Type: application/octet-stream" \ |
| 67 | + "${upload_url}?name=${DIST_FILE}" --data-binary "@${DIST_FILE}" |
0 commit comments