|
| 1 | +# |
| 2 | +# The functions here require the following scripts: |
| 3 | +# .travis/logger.sh |
| 4 | +# |
| 5 | +# The functions here require the following environment variables: |
| 6 | +# GITHUB_OAUTH_TOKEN |
| 7 | +# |
| 8 | + |
| 9 | +# |
| 10 | +# Creates a new release on github with the given tag and target_commit. |
| 11 | +# The release is draft and not published. |
| 12 | +# |
| 13 | +# $RESULT = release json string |
| 14 | +# |
| 15 | +# See: https://developer.github.com/v3/repos/releases/#create-a-release |
| 16 | +# |
| 17 | +function gh_releases_createDraftRelease() { |
| 18 | + local tagName="$1" |
| 19 | + local targetCommitish="$2" |
| 20 | + |
| 21 | + log_debug "$FUNCNAME: Creating new draft release for tag=$tagName and commit=$targetCommitish" |
| 22 | + |
| 23 | + local request=$(cat <<-EOF |
| 24 | + { |
| 25 | + "tag_name": "${tagName}", |
| 26 | + "target_commitish": "${targetCommitish}", |
| 27 | + "name": "${tagName}", |
| 28 | + "draft": true |
| 29 | + } |
| 30 | + EOF |
| 31 | + ) |
| 32 | +
|
| 33 | + log_debug "POST https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases" |
| 34 | + log_info "Creating gihtub draft release" |
| 35 | + RESULT=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ |
| 36 | + -H "Content-Type: application/json" \ |
| 37 | + -X POST \ |
| 38 | + --data "${request}" \ |
| 39 | + "https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases") |
| 40 | + log_debug " -> response: $RESULT" |
| 41 | +
|
| 42 | + log_success "Created draft release with id $(echo $RESULT | jq --raw-output ".url")" |
| 43 | +} |
| 44 | +
|
| 45 | +# |
| 46 | +# Gets the latest release, if it is a draft and returns with 0. |
| 47 | +# Returns with 1, if the latest release is not a draft - meaning, there is no |
| 48 | +# draft release (yet?). |
| 49 | +# |
| 50 | +# RESULT = release json string |
| 51 | +# |
| 52 | +# See: https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository |
| 53 | +# |
| 54 | +function gh_releases_getLatestDraftRelease() { |
| 55 | + log_debug "$FUNCNAME" |
| 56 | + log_debug "GET https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases?per_page=1" |
| 57 | + RESULT=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ |
| 58 | + "https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases?per_page=1" | jq ".[0]") |
| 59 | + log_debug " -> response: $RESULT" |
| 60 | + local draft=$(echo $RESULT | jq ".draft") |
| 61 | + if [ "$draft" != "true" ]; then |
| 62 | + RESULT="" |
| 63 | + log_error "Could not find draft release!" |
| 64 | + return 1 |
| 65 | + fi |
| 66 | + log_info "Found draft release: $(echo $RESULT | jq --raw-output ".url")" |
| 67 | +} |
| 68 | +
|
| 69 | +# |
| 70 | +# Deletes a release. |
| 71 | +# |
| 72 | +# See: https://developer.github.com/v3/repos/releases/#delete-a-release |
| 73 | +# |
| 74 | +function gh_release_deleteRelease() { |
| 75 | + local release="$1" |
| 76 | +
|
| 77 | + gh_release_getIdFromData "$release" |
| 78 | + local releaseId="$RESULT" |
| 79 | + log_debug "$FUNCNAME id=$releaseId" |
| 80 | + log_debug "DELETE https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/$releaseId" |
| 81 | + log_info "Deleting github release $releaseId" |
| 82 | + local response |
| 83 | + response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ |
| 84 | + -X DELETE \ |
| 85 | + "https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/$releaseId") |
| 86 | + log_debug " -> response: $response" |
| 87 | + log_success "Deleted release with id $releaseId" |
| 88 | +} |
| 89 | +
|
| 90 | +# |
| 91 | +# Determines the release id from the given JSON release data. |
| 92 | +# |
| 93 | +# RESULT = "the release id" |
| 94 | +# |
| 95 | +function gh_release_getIdFromData() { |
| 96 | + local release="$1" |
| 97 | +
|
| 98 | + RESULT=$(echo $release | jq --raw-output ".id") |
| 99 | +} |
| 100 | +
|
| 101 | +# |
| 102 | +# Uploads a asset to an existing release. |
| 103 | +# |
| 104 | +# See: https://developer.github.com/v3/repos/releases/#upload-a-release-asset |
| 105 | +# |
| 106 | +function gh_release_uploadAsset() { |
| 107 | + local release="$1" |
| 108 | + local filename="$2" |
| 109 | + local name=$(basename $filename) |
| 110 | +
|
| 111 | + gh_release_getIdFromData "$release" |
| 112 | + local releaseId="$RESULT" |
| 113 | + log_debug "$FUNCNAME: releaseId=$releaseId file=$filename name=$name" |
| 114 | +
|
| 115 | + local uploadUrl=$(echo "$release" | jq --raw-output ".upload_url") |
| 116 | + uploadUrl="${uploadUrl%%\{\?name,label\}}" |
| 117 | + uploadUrl="${uploadUrl}?name=${name}" |
| 118 | + log_debug "POST $uploadUrl" |
| 119 | + log_info "Uploading $filename to github release $releaseId" |
| 120 | + local response |
| 121 | + response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ |
| 122 | + -H "Content-Type: application/zip" \ |
| 123 | + --data-binary "@$filename" \ |
| 124 | + -X POST \ |
| 125 | + "${uploadUrl}") |
| 126 | + log_debug " -> response: $response" |
| 127 | + log_success "Uploaded release asset $filename for release $releaseId" |
| 128 | +} |
| 129 | +
|
| 130 | +# |
| 131 | +# Updates the release info: name and body. |
| 132 | +# The body is escaped to fit into JSON, so it is allowed for the body to be |
| 133 | +# a multi-line string. |
| 134 | +# |
| 135 | +# See: https://developer.github.com/v3/repos/releases/#edit-a-release |
| 136 | +# |
| 137 | +function gh_release_updateRelease() { |
| 138 | + local release="$1" |
| 139 | + local name="$2" |
| 140 | + local body="$3" |
| 141 | +
|
| 142 | + gh_release_getIdFromData "$release" |
| 143 | + local releaseId="$RESULT" |
| 144 | + log_debug "$FUNCNAME releaseId=$releaseId name=$name" |
| 145 | +
|
| 146 | + body="${body//'\'/\\\\}" |
| 147 | + body="${body//$'\r'/}" |
| 148 | + body="${body//$'\n'/\\r\\n}" |
| 149 | + body="${body//'"'/\\\"}" |
| 150 | +
|
| 151 | + local request=$(cat <<-EOF |
| 152 | + { |
| 153 | + "name": "${name}", |
| 154 | + "body": "${body}" |
| 155 | + } |
| 156 | + EOF |
| 157 | + ) |
| 158 | +
|
| 159 | + log_debug "PATCH https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/${releaseId}" |
| 160 | + log_debug " -> request: $request" |
| 161 | + log_info "Updating github release $releaseId" |
| 162 | + local response |
| 163 | + response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ |
| 164 | + -H "Content-Type: application/json" \ |
| 165 | + --data "${request}" \ |
| 166 | + -X PATCH \ |
| 167 | + "https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/${releaseId}") |
| 168 | + log_debug " -> response: $response" |
| 169 | + log_success "Updated release with id=$releaseId" |
| 170 | +} |
| 171 | +
|
| 172 | +# |
| 173 | +# Publish a release by setting draft="false". |
| 174 | +# Note: This will send out the notification emails if somebody |
| 175 | +# watched the releases. |
| 176 | +# |
| 177 | +# See: https://developer.github.com/v3/repos/releases/#edit-a-release |
| 178 | +# |
| 179 | +function gh_release_publishRelease() { |
| 180 | + local release="$1" |
| 181 | +
|
| 182 | + gh_release_getIdFromData "$release" |
| 183 | + local releaseId="$RESULT" |
| 184 | + log_debug "$FUNCNAME releaseId=$releaseId" |
| 185 | +
|
| 186 | + local request='{"draft":false}' |
| 187 | + log_debug "PATCH https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/${releaseId}" |
| 188 | + log_debug " -> request: $request" |
| 189 | + log_info "Publishing github release $releaseId" |
| 190 | + local response |
| 191 | + response=$(curl --fail -s -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \ |
| 192 | + -H "Content-Type: application/json" \ |
| 193 | + --data "${request}" \ |
| 194 | + -X PATCH \ |
| 195 | + "https://api.github.com/repos/pmd/pmd-eclipse-plugin/releases/${releaseId}") |
| 196 | + log_debug " -> response: $response" |
| 197 | + local htmlUrl=$(echo "$response" | jq --raw-output ".html_url") |
| 198 | + log_success "Published release with id=$releaseId at $htmlUrl" |
| 199 | +} |
| 200 | +
|
0 commit comments