From fafb3e76b1d93a1b0049e4ef32f263e3e417b692 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 9 Nov 2019 20:20:33 +0100 Subject: [PATCH] [ci] Create github-releases-api.sh --- .travis/build-deploy.sh | 38 ++----- .travis/build-doc.sh | 64 ++--------- .travis/build-publish.sh | 36 +----- .travis/github-releases-api.sh | 194 +++++++++++++++++++++++++++++++++ .travis/logger.sh | 9 +- 5 files changed, 222 insertions(+), 119 deletions(-) create mode 100644 .travis/github-releases-api.sh diff --git a/.travis/build-deploy.sh b/.travis/build-deploy.sh index 4552e9b237..1f72543946 100755 --- a/.travis/build-deploy.sh +++ b/.travis/build-deploy.sh @@ -3,6 +3,7 @@ set -e source .travis/logger.sh source .travis/common-functions.sh +source .travis/github-releases-api.sh function upload_baseline() { log_info "Generating and uploading baseline for pmdtester..." @@ -58,42 +59,19 @@ elif travis_isPush; then echo -e "\n\n" log_info "This is a release build for tag ${TRAVIS_TAG}" echo -e "\n\n" + + # create a draft github release + gh_releases_createDraftRelease "${TRAVIS_TAG}" "$(git show-ref --hash HEAD)" + GH_RELEASE="$RESULT" + ./mvnw deploy -Possrh,sign,pmd-release $MVN_BUILD_FLAGS echo -e "\n\n" # Deploy to ossrh has already been done with the usual maven build # Deploy to github releases - RELEASE_ID=$(curl -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" 'https://api.github.com/repos/pmd/pmd/releases?per_page=1'|jq ".[0].id") - RELEASE_DATA=$(curl -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" "https://api.github.com/repos/pmd/pmd/releases/${RELEASE_ID}") - DRAFT=$(echo "$RELEASE_DATA"|jq ".draft") - if [ "$DRAFT" != "true" ]; then - REQUEST=$(cat < release-edit-request.json < response: $RESULT" + + log_success "Created draft release with id $(echo $RESULT | jq --raw-output ".url")" +} + +# +# Gets the latest release, if it is a draft and returns with 0. +# Returns with 1, if the latest release is not a draft - meaning, there is no +# draft release (yet?). +# +# RESULT = release json string +# +# See: https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository +# +function gh_releases_getLatestDraftRelease() { + log_debug "$FUNCNAME" + log_debug "GET https://api.github.com/repos/pmd/pmd/releases?per_page=1" + RESULT=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ + "https://api.github.com/repos/pmd/pmd/releases?per_page=1" | jq ".[0]") + log_debug " -> response: $RESULT" + local draft=$(echo $RESULT | jq ".draft") + if [ "$draft" != "true" ]; then + RESULT="" + log_error "Could not find draft release!" + return 1 + fi + log_info "Found draft release: $(echo $RESULT | jq --raw-output ".url")" +} + +# +# Deletes a release. +# +# See: https://developer.github.com/v3/repos/releases/#delete-a-release +# +function gh_release_deleteRelease() { + local release="$1" + + gh_release_getIdFromData "$release" + local releaseId="$RESULT" + log_debug "$FUNCNAME id=$releaseId" + log_debug "DELETE https://api.github.com/repos/pmd/pmd/releases/$releaseId" + log_info "Deleting github release $releaseId" + local response + response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ + -X DELETE \ + "https://api.github.com/repos/pmd/pmd/releases/$releaseId") + log_debug " -> response: $response" + log_success "Deleted release with id $releaseId" +} + +# +# Determines the release id from the given JSON release data. +# +# RESULT = "the release id" +# +function gh_release_getIdFromData() { + local release="$1" + + RESULT=$(echo $release | jq --raw-output ".id") +} + +# +# Uploads a asset to an existing release. +# +# See: https://developer.github.com/v3/repos/releases/#upload-a-release-asset +# +function gh_release_uploadAsset() { + local release="$1" + local filename="$2" + local name=$(basename $filename) + + gh_release_getIdFromData "$release" + local releaseId="$RESULT" + log_debug "$FUNCNAME: releaseId=$releaseId file=$filename name=$name" + + local uploadUrl=$(echo "$release" | jq --raw-output ".upload_url") + uploadUrl="${uploadUrl%%\{\?name,label\}}" + uploadUrl="${uploadUrl}?name=${name}" + log_debug "POST $uploadUrl" + log_info "Uploading $filename to github release $releaseId" + local response + response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ + -H "Content-Type: application/zip" \ + --data-binary "@$filename" \ + -X POST \ + "${uploadUrl}") + log_debug " -> response: $response" + log_success "Uploaded release asset $filename for release $releaseId" +} + +# +# Updates the release info: name and body. +# The body is escaped to fit into JSON, so it is allowed for the body to be +# a multi-line string. +# +# See: https://developer.github.com/v3/repos/releases/#edit-a-release +# +function gh_release_updateRelease() { + local release="$1" + local name="$2" + local body="$3" + + gh_release_getIdFromData "$release" + local releaseId="$RESULT" + log_debug "$FUNCNAME releaseId=$releaseId name=$name" + + body="${body//'\'/\\\\}" + body="${body//$'\r'/}" + body="${body//$'\n'/\\r\\n}" + body="${body//'"'/\\\"}" + + local request=$(cat <<-EOF + { + "name": "${name}", + "body": "${body}" + } + EOF + ) + + log_debug "PATCH https://api.github.com/repos/pmd/pmd/releases/${releaseId}" + log_debug " -> request: $request" + log_info "Updating github release $releaseId" + local response + response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ + -H "Content-Type: application/json" \ + --data "${request}" \ + -X PATCH \ + "https://api.github.com/repos/pmd/pmd/releases/${releaseId}") + log_debug " -> response: $response" + log_success "Updated release with id=$releaseId" +} + +# +# Publish a release by setting draft="false". +# Note: This will send out the notification emails if somebody +# watched the releases. +# +# See: https://developer.github.com/v3/repos/releases/#edit-a-release +# +function gh_release_publishRelease() { + local release="$1" + + gh_release_getIdFromData "$release" + local releaseId="$RESULT" + log_debug "$FUNCNAME releaseId=$releaseId" + + local request='{"draft":false}' + log_debug "PATCH https://api.github.com/repos/pmd/pmd/releases/${releaseId}" + log_debug " -> request: $request" + log_info "Publishing github release $releaseId" + local response + response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ + -H "Content-Type: application/json" \ + --data "${request}" \ + -X PATCH \ + "https://api.github.com/repos/pmd/pmd/releases/${releaseId}") + log_debug " -> response: $response" + local htmlUrl=$(echo "$response" | jq --raw-output ".html_url") + log_success "Published release with id=$releaseId at $htmlUrl" +} + diff --git a/.travis/logger.sh b/.travis/logger.sh index 1c88df9413..cd76d5bd3a 100644 --- a/.travis/logger.sh +++ b/.travis/logger.sh @@ -6,13 +6,18 @@ COL_RESET="\e[0m" COL_YELLOW="\e[33;1m" function log_error() { - echo -e "${COL_RED}[ERROR] $*${COL_RESET}" + echo -e "${COL_RED}[ERROR ] $*${COL_RESET}" } function log_info() { - echo -e "${COL_YELLOW}[INFO] $*${COL_RESET}" + echo -e "${COL_YELLOW}[INFO ] $*${COL_RESET}" } function log_success() { echo -e "${COL_GREEN}[SUCCESS] $*${COL_RESET}" } + +function log_debug() { + true + #echo -e "[DEBUG ] $*" +}