feat: Add release.sh script and detect breaking changes (#5366)

This commit introduces three new scripts:

- `release.sh` To be run by a user on their local machine to preview and
  create a new release (tag + push)
- `check_commit_metadata.sh` For e.g. detecting breaking changes
- `genereate_release_notes.sh` To display the generated release notes,
  used for previews and in `publish_release.sh`

The `release.sh` script can be run without arguments, and it will
automatically determine if we're to do a patch or minor release. A minor
release can be forced via `--minor` flag.

Breaking changes can be annotated either via commit/merge title prefix
(`feat!:`, `feat(api)!:`), or by adding the `release/breaking` label to
the PR that was merged (on GitHub).

Related #5233
This commit is contained in:
Mathias Fredriksson
2022-12-15 15:41:30 +02:00
committed by GitHub
parent 4bc420dc48
commit e96fdbed26
6 changed files with 390 additions and 16 deletions
+142
View File
@@ -0,0 +1,142 @@
#!/usr/bin/env bash
# Usage: source ./check_commit_metadata.sh <from revision> <to revision>
# Usage: ./check_commit_metadata.sh <from revision> <to revision>
#
# Example: ./check_commit_metadata.sh v0.13.1 971e3678
#
# When sourced, this script will populate the COMMIT_METADATA_* variables
# with the commit metadata for each commit in the revision range.
#
# Because this script does some expensive lookups via the GitHub API, its
# results will be cached in the environment and restored if this script is
# sourced a second time with the same arguments.
set -euo pipefail
# shellcheck source=scripts/lib.sh
source "$(dirname "${BASH_SOURCE[0]}")/../lib.sh"
from_ref=${1:-}
to_ref=${2:-}
if [[ -z $from_ref ]]; then
error "No from_ref specified"
fi
if [[ -z $to_ref ]]; then
error "No to_ref specified"
fi
range="$from_ref..$to_ref"
# Check dependencies.
dependencies gh
COMMIT_METADATA_BREAKING=0
declare -A COMMIT_METADATA_TITLE COMMIT_METADATA_CATEGORY
main() {
# Match a commit prefix pattern, e.g. feat: or feat(site):.
prefix_pattern="^([a-z]+)(\([a-z]*\))?:"
# If a commit contains this title prefix or the source PR contains the
# label, patch releases will not be allowed.
# This regex matches both `feat!:` and `feat(site)!:`.
breaking_title="^[a-z]+(\([a-z]*\))?!:"
breaking_label=release/breaking
breaking_category=breaking
# Get abbreviated and full commit hashes and titles for each commit.
mapfile -t commits < <(git log --no-merges --pretty=format:"%h %H %s" "$range")
# If this is a tag, use rev-list to find the commit it points to.
from_commit=$(git rev-list -n 1 "$from_ref")
# Get the committer date of the commit so that we can list PRs merged.
from_commit_date=$(git show --no-patch --date=short --format=%cd "$from_commit")
# Get the labels for all PRs merged since the last release, this is
# inexact based on date, so a few PRs part of the previous release may
# be included.
#
# Example output:
#
# 27386d49d08455b6f8fbf2c18f38244d03fda892 label:security
# d9f2aaf3b430d8b6f3d5f24032ed6357adaab1f1
# fd54512858c906e66f04b0744d8715c2e0de97e6 label:stale label:enhancement
mapfile -t pr_labels_raw < <(
gh pr list \
--base main \
--state merged \
--limit 10000 \
--search "merged:>=$from_commit_date" \
--json mergeCommit,labels \
--jq '.[] | .mergeCommit.oid + " " + (["label:" + .labels[].name] | join(" "))'
)
declare -A labels
for entry in "${pr_labels_raw[@]}"; do
commit_sha_long=${entry%% *}
all_labels=${entry#* }
labels[$commit_sha_long]=$all_labels
done
for commit in "${commits[@]}"; do
mapfile -d ' ' -t parts <<<"$commit"
commit_sha_short=${parts[0]}
commit_sha_long=${parts[1]}
commit_prefix=${parts[2]}
# Safety-check, guarantee all commits had their metadata fetched.
if [[ ! -v labels[$commit_sha_long] ]]; then
error "Metadata missing for commit $commit_sha_short"
fi
# Store the commit title for later use.
title=${parts[*]:2}
title=${title%$'\n'}
COMMIT_METADATA_TITLE[$commit_sha_short]=$title
# First, check the title for breaking changes. This avoids doing a
# GH API request if there's a match.
if [[ $commit_prefix =~ $breaking_title ]] || [[ ${labels[$commit_sha_long]} = *"label:$breaking_label"* ]]; then
COMMIT_METADATA_CATEGORY[$commit_sha_short]=$breaking_category
COMMIT_METADATA_BREAKING=1
continue
fi
if [[ $commit_prefix =~ $prefix_pattern ]]; then
commit_prefix=${BASH_REMATCH[1]}
fi
case $commit_prefix in
feat | fix)
COMMIT_METADATA_CATEGORY[$commit_sha_short]=$commit_prefix
;;
*)
COMMIT_METADATA_CATEGORY[$commit_sha_short]=other
;;
esac
done
}
declare_print_commit_metadata() {
declare -p COMMIT_METADATA_BREAKING COMMIT_METADATA_TITLE COMMIT_METADATA_CATEGORY
}
export_commit_metadata() {
_COMMIT_METADATA_CACHE="${range}:$(declare_print_commit_metadata)"
export _COMMIT_METADATA_CACHE COMMIT_METADATA_BREAKING COMMIT_METADATA_TITLE COMMIT_METADATA_CATEGORY
}
# _COMMIT_METADATA_CACHE is used to cache the results of this script in
# the environment because bash arrays are not passed on to subscripts.
if [[ ${_COMMIT_METADATA_CACHE:-} == "${range}:"* ]]; then
eval "${_COMMIT_METADATA_CACHE#*:}"
else
main
fi
export_commit_metadata
# Make it easier to debug this script by printing the associative array
# when it's not sourced.
if ! issourced; then
declare_print_commit_metadata
fi
+121
View File
@@ -0,0 +1,121 @@
#!/usr/bin/env bash
# Usage: ./generate_release_notes.sh --old-version <old version> --new-version <new version> --ref <ref>
#
# Example: ./generate_release_notes.sh --old-version v0.13.0 --new-version v0.13.1 --ref 1e6b244c
#
# This script generates release notes for the given version. It will generate
# release notes for all commits between the old version and the new version.
#
# Ref must be set to the commit that the new version will be tagget at. This
# is used to determine the commits that are included in the release. If the
# commit is already tagged, ref can be set to the tag name.
set -euo pipefail
# shellcheck source=scripts/lib.sh
source "$(dirname "${BASH_SOURCE[0]}")/../lib.sh"
old_version=
new_version=
ref=
args="$(getopt -o '' -l old-version:,new-version:,ref: -- "$@")"
eval set -- "$args"
while true; do
case "$1" in
--old-version)
old_version="$2"
shift 2
;;
--new-version)
new_version="$2"
shift 2
;;
--ref)
ref="$2"
shift 2
;;
--)
shift
break
;;
*)
error "Unrecognized option: $1"
;;
esac
done
# Check dependencies.
dependencies gh sort
if [[ -z $old_version ]]; then
error "No old version specified"
fi
if [[ -z $new_version ]]; then
error "No new version specified"
fi
if [[ -z $ref ]]; then
error "No ref specified"
fi
# shellcheck source=scripts/release/check_commit_metadata.sh
source "$SCRIPT_DIR/release/check_commit_metadata.sh" "${old_version}" "${ref}"
# Sort commits by title prefix, then by date, only return sha at the end.
mapfile -t commits < <(git log --no-merges --pretty=format:"%ct %h %s" "${old_version}..${ref}" | sort -k3,3 -k1,1n | cut -d' ' -f2)
breaking_changelog=
feat_changelog=
fix_changelog=
other_changelog=
for commit in "${commits[@]}"; do
line="- $commit ${COMMIT_METADATA_TITLE[$commit]}\n"
case "${COMMIT_METADATA_CATEGORY[$commit]}" in
breaking)
breaking_changelog+="$line"
;;
feat)
feat_changelog+="$line"
;;
fix)
fix_changelog+="$line"
;;
*)
other_changelog+="$line"
;;
esac
done
changelog="$(
if ((${#breaking_changelog} > 0)); then
echo -e "### BREAKING CHANGES\n"
echo -e "$breaking_changelog"
fi
if ((${#feat_changelog} > 0)); then
echo -e "### Features\n"
echo -e "$feat_changelog"
fi
if ((${#fix_changelog} > 0)); then
echo -e "### Bug fixes\n"
echo -e "$fix_changelog"
fi
if ((${#other_changelog} > 0)); then
echo -e "### Other changes\n"
echo -e "$other_changelog"
fi
)"
image_tag="$(execrelative ./image_tag.sh --version "$new_version")"
echo -e "## Changelog
$changelog
Compare: [\`${old_version}...${new_version}\`](https://github.com/coder/coder/compare/${old_version}...${new_version})
## Container image
- \`docker pull $image_tag\`
"
+151
View File
@@ -0,0 +1,151 @@
#!/usr/bin/env bash
# This script generates release notes and publishes all of the given assets to
# GitHub releases. Depends on GitHub CLI.
#
# THIS IS NOT INTENDED TO BE CALLED BY DEVELOPERS! This is called by the release
# pipeline to do the final publish step. If you want to create a release use:
# git tag -a -m "$ver" "$ver" && git push origin "$ver"
#
# Usage: ./publish.sh [--version 1.2.3] [--dry-run] path/to/asset1 path/to/asset2 ...
#
# The supplied images must already be pushed to the registry or this will fail.
# Also, the source images cannot be in a different registry than the target
# image generated by ./image_tag.sh.
# The supplied assets will be uploaded to the GitHub release as-is, as well as a
# file containing checksums.
#
# If no version is specified, defaults to the version from ./version.sh. The
# script will exit early if the branch is not tagged with the provided version
# (plus the "v" prefix) unless run with --dry-run.
#
# If the --dry-run parameter is supplied, the release will not be published to
# GitHub at all.
#
# Returns the link to the created GitHub release (unless --dry-run was
# specified).
set -euo pipefail
# shellcheck source=scripts/lib.sh
source "$(dirname "${BASH_SOURCE[0]}")/../lib.sh"
if [[ "${CI:-}" == "" ]]; then
error "This script must be run in CI"
fi
version=""
dry_run=0
args="$(getopt -o "" -l version:,dry-run -- "$@")"
eval set -- "$args"
while true; do
case "$1" in
--version)
version="$2"
shift 2
;;
--dry-run)
dry_run=1
shift
;;
--)
shift
break
;;
*)
error "Unrecognized option: $1"
;;
esac
done
# Check dependencies
dependencies gh
# Remove the "v" prefix.
version="${version#v}"
if [[ "$version" == "" ]]; then
version="$(execrelative ./version.sh)"
fi
# realpath-ify all input files so we can cdroot below.
files=()
for f in "$@"; do
if [[ ! -e "$f" ]]; then
error "File not found: $f"
fi
files+=("$(realpath "$f")")
done
if [[ "${#files[@]}" == 0 ]]; then
error "No files supplied"
fi
if [[ "$dry_run" == 0 ]] && [[ "$version" == *dev* ]]; then
error "Cannot publish a dev version to GitHub"
fi
# The git commands need to be executed from within the repository.
cdroot
# Verify that we're currently checked out on the supplied tag.
new_tag="v$version"
if [[ "$(git describe --always)" != "$new_tag" ]]; then
if [[ "$dry_run" == 0 ]]; then
error "The provided version '$new_tag' does not match the current git describe output '$(git describe --always)'"
fi
log "The provided version does not match the current git tag, but --dry-run was supplied so continuing..."
fi
# This returns the tag before the current tag.
old_tag="$(git describe --abbrev=0 HEAD^1)"
# For dry-run builds we want to use the SHA instead of the tag, because the new
# tag probably doesn't exist.
new_ref="$new_tag"
if [[ "$dry_run" == 1 ]]; then
new_ref="$(git rev-parse --short HEAD)"
fi
# shellcheck source=scripts/release/check_commit_metadata.sh
source "$SCRIPT_DIR/release/check_commit_metadata.sh" "$old_tag" "$new_ref"
# Craft the release notes.
release_notes="$(execrelative ./generate_release_notes.sh --old-version "$old_tag" --new-version "$new_tag" --ref "$new_ref")"
release_notes_file="$(mktemp)"
echo "$release_notes" >"$release_notes_file"
# Create temporary release folder so we can generate checksums. Both the
# sha256sum and gh binaries support symlinks as input files so this works well.
temp_dir="$(mktemp -d)"
for f in "${files[@]}"; do
ln -s "$f" "$temp_dir/"
done
# Generate checksums file which will be uploaded to the GitHub release.
pushd "$temp_dir"
sha256sum ./* | sed -e 's/\.\///' - >"coder_${version}_checksums.txt"
popd
log "--- Creating release $new_tag"
log
log "Description:"
echo "$release_notes" | sed -e 's/^/\t/' - 1>&2
log
log "Contents:"
pushd "$temp_dir"
find ./* 2>&1 | sed -e 's/^/\t/;s/\.\///' - 1>&2
popd
log
log
# We pipe `true` into `gh` so that it never tries to be interactive.
true |
maybedryrun "$dry_run" gh release create \
--title "$new_tag" \
--notes-file "$release_notes_file" \
"$new_tag" \
"$temp_dir"/*
rm -rf "$temp_dir"
rm -rf "$release_notes_file"