From 4f4db7895bee554b37a22e212f2465f59e8d4fa3 Mon Sep 17 00:00:00 2001 From: Nick Charlton Date: Sat, 29 Apr 2023 21:45:25 +0100 Subject: [PATCH] Add a script to generate a draft CHANGELOG update The majority of the effort in making a new release is in putting together a good CHANGELOG. Back in #1968, we added a script to generate a list of templates that had changed since the last (provided) tag. This helped ensure we'd communicate any template changes, whilst requiring little effort to do so. The next step in assembling a CHANGELOG was to put together a list of commits the release would include. By convention this has been: [KEY] [PR NUMBER] Commit Message So far, this has required a lot of manual text manipulation, and when the commit wasn't introduced via a squash on GitHub, the PR reference needed to be tracked down (which could take some time). This script attempts to automate the rest of this process by assembling a draft that needs much less effort to publish. By using the GitHub CLI, we're able to match commits back to the originating pull request and then automate much of the text manipulation which was needed before. We then pull over the template warning checker from before, but in this implementation skip over the `spec` changes, as they shouldn't matter to end users. We can also assume we want changes since the last tag, to remove the need to provide an argument. An example run (trimmed): The following templates have changed since v0.18.0: app/views/administrate/application/_collection.html.erb app/views/administrate/application/_index_header.html.erb If your application overrides any of them, make sure to review your custom templates to ensure that they remain compatible. * [] [#2367] Update to Ruby 3.2.2 * [] [#2371] Adapt to deprecations in the Faker API * [] [#2348] Field::Select to handle ActiveRecord enums correctly https://cli.github.com/ https://git-scm.com/docs/pretty-formats#Documentation/pretty-formats.txt-trailersoptions https://stackoverflow.com/a/18558871 https://stackoverflow.com/a/30035045 --- bin/build-changelog | 38 ++++++++++++++++++++++++++++++++++++++ bin/changelog | 37 ------------------------------------- 2 files changed, 38 insertions(+), 37 deletions(-) create mode 100755 bin/build-changelog delete mode 100755 bin/changelog diff --git a/bin/build-changelog b/bin/build-changelog new file mode 100755 index 0000000000..7c08b975c3 --- /dev/null +++ b/bin/build-changelog @@ -0,0 +1,38 @@ +#!/bin/sh + +set -e + +if ! command -v gh > /dev/null; then + echo "Please install the GitHub CLI: https://cli.github.com/" + exit 1 +fi + +last_release=$(git describe --tag --abbrev=0) + +template_changes=$(git diff --name-only "${last_release}" | grep html.erb) + +echo "The following templates have changed since ${last_release}:" +echo +echo "$template_changes" | while read -r line; do + if case $line in spec*) false;; esac; then + echo " ${line}" + fi +done +echo +echo "If your application overrides any of them, make sure to review your" +echo "custom templates to ensure that they remain compatible." +echo + +revision_range="${last_release}..origin/main" +commit_format="--pretty=tformat:%h %s" +commits_since=$(git log --author="^(?!dependabot).*$" --perl-regexp "${revision_range}" "${commit_format}") + +echo "$commits_since" | while read -r line; do + sha=$(echo "${line}" | awk '{print $1}') + commit_message=$(echo "${line}" | awk '{print substr($0, index($0, " ")+1)}') + + pr_number=$(gh pr list --search "$sha" --state merged --json number --jq '.[].number') + trimmed_commit=$(echo "${commit_message}"| sed "s/(\#$pr_number)//g") + + echo "* [] [#${pr_number}] ${trimmed_commit}" +done diff --git a/bin/changelog b/bin/changelog deleted file mode 100755 index 618f55d60c..0000000000 --- a/bin/changelog +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env ruby - -def silent_run(cmd) - pid = spawn(cmd, out: "/dev/null", err: "/dev/null") - Process.wait(pid) - $?.success? -end - -if ARGV.count != 1 - script = $0 - puts "Usage:" - puts " #{script} " - puts - puts "Example:" - puts " #{script} v0.15.0" - exit 1 -end - -gitref = ARGV.first - -unless silent_run("git cat-file -t #{gitref}") - puts "The given git reference `#{gitref}` does not exist in this repository." - exit 1 -end - -files = `git diff --name-only #{gitref} | grep html.erb` - -if files.empty? - puts "No template changes since #{gitref}" -else - puts "The following templates have changed since #{gitref}:" - puts - puts(files.split.map { |line| " #{line}" }) - puts - puts "If your application overrides any of them, make sure to review your" - puts "custom templates to ensure that they remain compatible." -end