[ci] Create github-releases-api.sh

This commit is contained in:
Andreas Dangel
2019-11-09 20:20:33 +01:00
parent fd52b4b8ea
commit fafb3e76b1
5 changed files with 222 additions and 119 deletions

View File

@ -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 <<EOF
{
"tag_name": "${TRAVIS_TAG}",
"target_commitish": "$(git show-ref --hash HEAD)",
"name": "${TRAVIS_TAG}",
"draft": true
}
EOF
)
RELEASE_ID=$(curl -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" -H "Content-Type: application/json" \
--data "${REQUEST}" -X POST https://api.github.com/repos/pmd/pmd/releases | jq ".id")
echo $RELEASE_ID
log_info "Created draft release with id ${RELEASE_ID}"
else
log_info "Using draft release with id "${RELEASE_ID}"
fi
UPLOAD_URL=$(echo "$RELEASE_DATA" | jq --raw-output ".upload_url")
UPLOAD_URL=${UPLOAD_URL%%\{\?name,label\}}
echo "Uploading pmd-bin-${VERSION}.zip ..."
curl -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" -H "Content-Type: application/zip" \
--data-binary "@pmd-dist/target/pmd-bin-${VERSION}.zip" \
-X POST "${UPLOAD_URL}?name=pmd-bin-${VERSION}.zip"
echo "Uploading pmd-src-${VERSION}.zip ..."
curl -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" -H "Content-Type: application/zip" \
--data-binary "@pmd-dist/target/pmd-src-${VERSION}.zip" \
-X POST "${UPLOAD_URL}?name=pmd-src-${VERSION}.zip"
gh_release_uploadAsset "$GH_RELEASE" "pmd-dist/target/pmd-bin-${VERSION}.zip"
gh_release_uploadAsset "$GH_RELEASE" "pmd-dist/target/pmd-src-${VERSION}.zip"
# Deploy to sourceforge files

View File

@ -3,6 +3,7 @@ set -e
source .travis/logger.sh
source .travis/common-functions.sh
source .travis/github-releases-api.sh
VERSION=$(./mvnw -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.5.0:exec)
log_info "Building PMD Documentation ${VERSION} on branch ${TRAVIS_BRANCH}"
@ -42,65 +43,16 @@ popd
if [[ "${VERSION}" != *-SNAPSHOT && "${TRAVIS_TAG}" != "" ]]; then
# 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 <<EOF
{
"tag_name": "${TRAVIS_TAG}",
"target_commitish": "$(git show-ref --hash HEAD)",
"name": "${TRAVIS_TAG}",
"draft": true
}
EOF
)
RELEASE_ID=$(curl -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" -H "Content-Type: application/json" \
--data "${REQUEST}" -X POST https://api.github.com/repos/pmd/pmd/releases | jq ".id")
echo $RELEASE_ID
log_info "Created draft release with id ${RELEASE_ID}"
else
log_info "Using draft release with id "${RELEASE_ID}"
fi
UPLOAD_URL=$(echo "$RELEASE_DATA" | jq --raw-output ".upload_url")
UPLOAD_URL=${UPLOAD_URL%%\{\?name,label\}}
echo "Uploading docs/pmd-doc-${VERSION}.zip ..."
curl -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" -H "Content-Type: application/zip" \
--data-binary "@docs/pmd-doc-${VERSION}.zip" \
-X POST "${UPLOAD_URL}?name=pmd-doc-${VERSION}.zip"
gh_releases_getLatestDraftRelease
GH_RELEASE="$RESULT"
gh_release_uploadAsset "$GH_RELEASE" "docs/pmd-doc-${VERSION}.zip"
# updating github release text
# renders, and skips the first 6 lines - the Jekyll front-matter
RENDERED_RELEASE_NOTES=$(bundle exec .travis/render_release_notes.rb docs/pages/release_notes.md | tail -n +6)
# Assumes, the release has already been created by travis github releases provider
RELEASE_NAME="PMD ${VERSION} ($(date -u +%d-%B-%Y))"
RELEASE_BODY="$RENDERED_RELEASE_NOTES"
RELEASE_BODY="${RELEASE_BODY//'\'/\\\\}"
RELEASE_BODY="${RELEASE_BODY//$'\r'/}"
RELEASE_BODY="${RELEASE_BODY//$'\n'/\\r\\n}"
RELEASE_BODY="${RELEASE_BODY//'"'/\\\"}"
cat > release-edit-request.json <<EOF
{
"name": "$RELEASE_NAME",
"body": "$RELEASE_BODY"
}
EOF
echo -e "\n\n"
log_info "Updating release at https://api.github.com/repos/pmd/pmd/releases/${RELEASE_ID}..."
RESPONSE=$(curl -i -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" -H "Content-Type: application/json" --data "@release-edit-request.json" -X PATCH https://api.github.com/repos/pmd/pmd/releases/${RELEASE_ID})
if [[ "$RESPONSE" != *"HTTP/1.1 200"* ]]; then
log_error "Github Request failed!"
echo "Request:"
cat release-edit-request.json
echo
echo "Response:"
echo "$RESPONSE"
else
log_success "Update OK"
fi
gh_release_updateRelease "$GH_RELEASE" "$RELEASE_NAME" "$RENDERED_RELEASE_NOTES"
echo -e "\n\n"
log_info "Adding the new doc to pmd.github.io..."

View File

@ -3,6 +3,7 @@ set -e
source .travis/logger.sh
source .travis/common-functions.sh
source .travis/github-releases-api.sh
VERSION=$(./mvnw -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.5.0:exec)
log_info "PMD Release ${VERSION}"
@ -17,36 +18,9 @@ if [[ "${VERSION}" == *-SNAPSHOT || "${TRAVIS_TAG}" == "" ]]; then
exit 0
fi
# Publish github release
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
log_error "No draft release found! (needs to be the latest one)."
exit 1;
fi
gh_releases_getLatestDraftRelease
GH_RELEASE="$RESULT"
log_info "Using draft release with id "${RELEASE_ID}"
REQUEST=$(cat <<EOF
{
"draft": false
}
EOF
)
RESPONSE=$(curl -i -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" -H "Content-Type: application/json" \
--data "${REQUEST}" -X PATCH https://api.github.com/repos/pmd/pmd/releases/${RELEASE_ID})
if [[ "$RESPONSE" != *"HTTP/1.1 200"* ]]; then
log_error "Github Request failed!"
echo "Request:"
echo "$REQUEST"
echo
echo "Response:"
echo "$RESPONSE"
else
log_success "Update OK"
HTML_URL=$(echo "$RELEASE_DATA" | jq --raw-output ".html_url")
log_info "The release ${HTML_URL} is published"
fi
gh_release_publishRelease "$GH_RELEASE"

View File

@ -0,0 +1,194 @@
#
# Creates a new release on github with the given tag and target_commit.
# The release is draft and not published.
#
# $RESULT = release json string
#
# See: https://developer.github.com/v3/repos/releases/#create-a-release
#
function gh_releases_createDraftRelease() {
local tagName="$1"
local targetCommitish="$2"
log_debug "$FUNCNAME: Creating new draft release for tag=$tagName and commit=$targetCommitish"
local request=$(cat <<-EOF
{
"tag_name": "${tagName}",
"target_commitish": "${targetCommitish}",
"name": "${tagName}",
"draft": true
}
EOF
)
log_debug "POST https://api.github.com/repos/pmd/pmd/releases"
log_info "Creating gihtub draft release"
RESULT=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
-H "Content-Type: application/json" \
-X POST \
--data "${request}" \
"https://api.github.com/repos/pmd/pmd/releases")
log_debug " -> 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"
}

View File

@ -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 ] $*"
}