From 6599c44213337c67688712a985d068ef791c052e Mon Sep 17 00:00:00 2001 From: firest Date: Wed, 15 Feb 2023 14:00:40 +0800 Subject: [PATCH] chore: refactor the format-changelog script --- changes/ce/.gitkeep | 0 changes/ee/.gitkeep | 0 scripts/changelog-lang-templates/en | 12 +++++ scripts/changelog-lang-templates/zh | 12 +++++ scripts/format-changelog.sh | 79 +++++++++++++++-------------- scripts/rel/cut.sh | 22 ++++++-- 6 files changed, 83 insertions(+), 42 deletions(-) create mode 100644 changes/ce/.gitkeep create mode 100644 changes/ee/.gitkeep create mode 100644 scripts/changelog-lang-templates/en create mode 100644 scripts/changelog-lang-templates/zh diff --git a/changes/ce/.gitkeep b/changes/ce/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/changes/ee/.gitkeep b/changes/ee/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/scripts/changelog-lang-templates/en b/scripts/changelog-lang-templates/en new file mode 100644 index 000000000..05c218c7e --- /dev/null +++ b/scripts/changelog-lang-templates/en @@ -0,0 +1,12 @@ +# ${version} + +## Enhancements + +$(section feat) + +$(section perf) + +## Bug fixes + +$(section fix) + diff --git a/scripts/changelog-lang-templates/zh b/scripts/changelog-lang-templates/zh new file mode 100644 index 000000000..2bafd99d7 --- /dev/null +++ b/scripts/changelog-lang-templates/zh @@ -0,0 +1,12 @@ +# ${version} + +## 增强 + +$(section feat) + +$(section perf) + +## 修复 + +$(section fix) + diff --git a/scripts/format-changelog.sh b/scripts/format-changelog.sh index c7b413cc8..87fbc60a2 100755 --- a/scripts/format-changelog.sh +++ b/scripts/format-changelog.sh @@ -3,20 +3,27 @@ set -euo pipefail shopt -s nullglob export LANG=C.UTF-8 -[ "$#" -ne 2 ] && { - echo "Usage $0 " 1>&2; +[ "$#" -ne 4 ] && { + echo "Usage $0 " 1>&2; exit 1 } -version="${1}" -language="${2}" +profile="${1}" +last_tag="${2}" +version="${3}" +output_dir="${4}" +languages=("en" "zh") +top_dir="$(git rev-parse --show-toplevel)" +templates_dir="$top_dir/scripts/changelog-lang-templates" +declare -a changes +changes=("") -changes_dir="$(git rev-parse --show-toplevel)/changes/${version}" +echo "generated changelogs from tag:${last_tag} to HEAD" item() { local filename pr indent filename="${1}" - pr="$(echo "${filename}" | sed -E 's/.*-([0-9]+)\.(en|zh)\.md$/\1/')" + pr="$(echo "${filename}" | sed -E 's/.*-([0-9]+)\.[a-z]+\.md$/\1/')" indent="- [#${pr}](https://github.com/emqx/emqx/pull/${pr}) " while read -r line; do echo "${indent}${line}" @@ -27,40 +34,36 @@ item() { section() { local prefix=$1 - for i in "${changes_dir}"/"${prefix}"-*."${language}".md; do - item "${i}" + for file in "${changes[@]}"; do + if [[ $file =~ .*$prefix-.*$language.md ]]; then + item "$file" + fi done } -if [ "${language}" = "en" ]; then - cat < $output" + else + echo "Invalid language ${language}" 1>&2; + exit 1 + fi +} -## Enhancements - -$(section feat) - -$(section perf) - -## Bug fixes - -$(section fix) -EOF -elif [ "${language}" = "zh" ]; then - cat <&2; - exit 1 +changes_dir=("$top_dir/changes/ce") +if [ "$profile" == "emqx-enterprise" ]; then + changes_dir+=("$top_dir/changes/ee") fi + +while read -d "" -r file; do + changes+=("$file") +done < <(git diff --name-only -z -a "tags/${last_tag}...HEAD" "${changes_dir[@]}") + +for language in "${languages[@]}"; do + generate "$language" +done diff --git a/scripts/rel/cut.sh b/scripts/rel/cut.sh index e03d5eff4..8d00694ac 100755 --- a/scripts/rel/cut.sh +++ b/scripts/rel/cut.sh @@ -26,6 +26,8 @@ options: --dryrun: Do not actually create the git tag. --skip-appup: Skip checking appup Useful when you are sure that appup is already updated' + --prev-tag: Provide the prev tag to automatically generate changelogs + If this option is absent, the tag found by git describe will be used NOTE: For 5.0 series the current working branch must be 'release-50' for opensource edition and 'release-e50' for enterprise edition. @@ -92,6 +94,11 @@ while [ "$#" -gt 0 ]; do fi shift 2 ;; + --prev-tag) + shift + PREV_TAG="$1" + shift + ;; *) logerr "Unknown option $1" exit 1 @@ -208,10 +215,17 @@ if [ -d "${CHECKS_DIR}" ]; then fi generate_changelog () { - local CHANGES_EN_MD="changes/${TAG}-en.md" CHANGES_ZH_MD="changes/${TAG}-zh.md" - ./scripts/format-changelog.sh "${TAG}" "en" > "$CHANGES_EN_MD" - ./scripts/format-changelog.sh "${TAG}" "zh" > "$CHANGES_ZH_MD" - git add "$CHANGES_EN_MD" "$CHANGES_ZH_MD" + local from_tag="${PREV_TAG:-}" + if [[ -z $from_tag ]]; then + if [ $PROFILE == "emqx" ]; then + from_tag="$(git describe --tags --abbrev=0 --match 'v*')" + else + from_tag="$(git describe --tags --abbrev=0 --match 'e*')" + fi + fi + local output_dir="changes" + ./scripts/format-changelog.sh $PROFILE "${from_tag}" "${TAG}" $output_dir + git add $output_dir [ -n "$(git status -s)" ] && git commit -m "chore: Generate changelog for ${TAG}" }