|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Copied and modified from https://github.com/firebase/extensions/blob/next/.github/workflows/scripts/release.sh. Changes: |
| 16 | +# 1. Read the extension name from the extension.yaml file instead of from the directory name. |
| 17 | + |
| 18 | +#!/bin/bash |
| 19 | +set -e |
| 20 | +set -o pipefail |
| 21 | + |
| 22 | +# Uncomment for testing purposes: |
| 23 | + |
| 24 | +#GITHUB_TOKEN=YOUR_TOKEN_HERE |
| 25 | +#GITHUB_REPOSITORY=invertase/extensions-release-testing |
| 26 | + |
| 27 | +# ------------------- |
| 28 | +# Functions |
| 29 | +# ------------------- |
| 30 | +json_escape() { |
| 31 | + printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))' |
| 32 | +} |
| 33 | + |
| 34 | +# Creates a new GitHub release |
| 35 | +# ARGS: |
| 36 | +# 1: Name of the release (becomes the release title on GitHub) |
| 37 | +# 2: Markdown body of the release |
| 38 | +# 3: Release Git tag |
| 39 | +create_github_release() { |
| 40 | + local response='' |
| 41 | + local release_name=$1 |
| 42 | + local release_body=$2 |
| 43 | + local release_tag=$3 |
| 44 | + |
| 45 | + local body='{ |
| 46 | + "tag_name": "%s", |
| 47 | + "target_commitish": "master", |
| 48 | + "name": "%s", |
| 49 | + "body": %s, |
| 50 | + "draft": false, |
| 51 | + "prerelease": false |
| 52 | + }' |
| 53 | + |
| 54 | + # shellcheck disable=SC2059 |
| 55 | + body=$(printf "$body" "$release_tag" "$release_name" "$release_body") |
| 56 | + response=$(curl --request POST \ |
| 57 | + --url https://api.github.com/repos/${GITHUB_REPOSITORY}/releases \ |
| 58 | + --header "Authorization: Bearer $GITHUB_TOKEN" \ |
| 59 | + --header 'Content-Type: application/json' \ |
| 60 | + --data "$body" \ |
| 61 | + -s) |
| 62 | + |
| 63 | + created=$(echo "$response" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('id', sys.stdin))") |
| 64 | + if [ "$created" != "$response" ]; then |
| 65 | + printf "release created successfully!\n" |
| 66 | + else |
| 67 | + printf "release failed to create; " |
| 68 | + printf "\n%s\n" "$body" |
| 69 | + printf "\n%s\n" "$response" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +# Updates an existing GitHub release |
| 75 | +# ARGS: |
| 76 | +# 1: Name of the release (becomes the release title on GitHub) |
| 77 | +# 2: Markdown body of the release |
| 78 | +# 3: Release Git tag |
| 79 | +# 4: ID of the existing release |
| 80 | +update_github_release() { |
| 81 | + local response='' |
| 82 | + local release_name=$1 |
| 83 | + local release_body=$2 |
| 84 | + local release_tag=$3 |
| 85 | + local release_id=$4 |
| 86 | + |
| 87 | + local body='{ |
| 88 | + "tag_name": "%s", |
| 89 | + "target_commitish": "master", |
| 90 | + "name": "%s", |
| 91 | + "body": %s, |
| 92 | + "draft": false, |
| 93 | + "prerelease": false |
| 94 | + }' |
| 95 | + |
| 96 | + # shellcheck disable=SC2059 |
| 97 | + body=$(printf "$body" "$release_tag" "$release_name" "$release_body") |
| 98 | + response=$(curl --request PATCH \ |
| 99 | + --url "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$release_id" \ |
| 100 | + --header "Authorization: Bearer $GITHUB_TOKEN" \ |
| 101 | + --header 'Content-Type: application/json' \ |
| 102 | + --data "$body" \ |
| 103 | + -s) |
| 104 | + |
| 105 | + updated=$(echo "$response" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('id', sys.stdin))") |
| 106 | + if [ "$updated" != "$response" ]; then |
| 107 | + printf "release updated successfully!\n" |
| 108 | + else |
| 109 | + printf "release failed to update; " |
| 110 | + printf "\n%s\n" "$body" |
| 111 | + printf "\n%s\n" "$response" |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | +} |
| 115 | + |
| 116 | +# Creates or updates a GitHub release |
| 117 | +# ARGS: |
| 118 | +# 1: Extension name |
| 119 | +# 2: Extension version |
| 120 | +# 3: Markdown body to use for the release |
| 121 | +create_or_update_github_release() { |
| 122 | + local response='' |
| 123 | + local release_id='' |
| 124 | + local extension_name=$1 |
| 125 | + local extension_version=$2 |
| 126 | + local release_body=$3 |
| 127 | + local release_tag="$extension_name-v$extension_version" |
| 128 | + local release_name="$extension_name v$extension_version" |
| 129 | + |
| 130 | + response=$(curl --request GET \ |
| 131 | + --url "https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/tags/${release_tag}" \ |
| 132 | + --header "Authorization: Bearer $GITHUB_TOKEN" \ |
| 133 | + --header 'Content-Type: application/json' \ |
| 134 | + --data "$body" \ |
| 135 | + -s) |
| 136 | + |
| 137 | + release_id=$(echo "$response" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('id', 'Not Found'))") |
| 138 | + if [ "$release_id" != "Not Found" ]; then |
| 139 | + existing_release_body=$(echo "$response" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('body', ''))") |
| 140 | + existing_release_body=$(json_escape "$existing_release_body") |
| 141 | + # Only update it if the release body is different (this can happen if a change log is manually updated) |
| 142 | + printf "Existing release (%s) found for %s - " "$release_id" "$release_tag" |
| 143 | + if [ "$existing_release_body" != "$release_body" ]; then |
| 144 | + printf "updating it with updated release body ... " |
| 145 | + update_github_release "$release_name" "$release_body" "$release_tag" "$release_id" |
| 146 | + else |
| 147 | + printf "skipping it as release body is already up to date.\n" |
| 148 | + fi |
| 149 | + else |
| 150 | + response_message=$(echo "$response" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('message'))") |
| 151 | + if [ "$response_message" != "Not Found" ]; then |
| 152 | + echo "Failed to query release '$release_name' -> GitHub API request failed with response: $response_message" |
| 153 | + echo "$response" |
| 154 | + exit 1 |
| 155 | + else |
| 156 | + printf "Creating new release '%s' ... " "$release_tag" |
| 157 | + create_github_release "$release_name" "$release_body" "$release_tag" |
| 158 | + fi |
| 159 | + fi |
| 160 | +} |
| 161 | + |
| 162 | +# ------------------- |
| 163 | +# Main Script |
| 164 | +# ------------------- |
| 165 | + |
| 166 | +# Ensure that the GITHUB_TOKEN env variable is defined |
| 167 | +if [[ -z "$GITHUB_TOKEN" ]]; then |
| 168 | + echo "Missing required GITHUB_TOKEN env variable. Set this on the workflow action or on your local environment." |
| 169 | + exit 1 |
| 170 | +fi |
| 171 | + |
| 172 | +# Ensure that the GITHUB_REPOSITORY env variable is defined |
| 173 | +if [[ -z "$GITHUB_REPOSITORY" ]]; then |
| 174 | + echo "Missing required GITHUB_REPOSITORY env variable. Set this on the workflow action or on your local environment." |
| 175 | + exit 1 |
| 176 | +fi |
| 177 | + |
| 178 | +# Find all extensions based on whether a extension.yaml file exists in the directory |
| 179 | +for i in $(find . -type f -name 'extension.yaml' -exec dirname {} \; | sort -u); do |
| 180 | + # Pluck extension latest name from yaml file |
| 181 | + extension_name=$(awk '/^name: /' "$i/extension.yaml" | sed "s/name: //") |
| 182 | + # Pluck extension latest version from yaml file |
| 183 | + extension_version=$(awk '/^version: /' "$i/extension.yaml" | sed "s/version: //") |
| 184 | + |
| 185 | + changelog_contents="No changelog found for this version." |
| 186 | + |
| 187 | + # Ensure changelog exists |
| 188 | + if [ -f "$i/CHANGELOG.md" ]; then |
| 189 | + # Pluck out change log contents for the latest extension version |
| 190 | + changelog_contents=$(awk -v ver="$extension_version" '/^## Version / { if (p) { exit }; if ($3 == ver) { p=1; next} } p && NF' "$i/CHANGELOG.md") |
| 191 | + else |
| 192 | + echo "WARNING: A changelog could not be found at $i/CHANGELOG.md - a default entry will be used instead." |
| 193 | + fi |
| 194 | + |
| 195 | + # JSON escape the markdown content for the release body |
| 196 | + changelog_contents=$(json_escape "$changelog_contents") |
| 197 | + |
| 198 | + # Creates a new release if it does not exist |
| 199 | + # OR |
| 200 | + # Updates an existing release with updated content (allows updating CHANGELOG.md which will update relevant release body) |
| 201 | + create_or_update_github_release "$extension_name" "$extension_version" "$changelog_contents" |
| 202 | +done |
0 commit comments