Pārlūkot izejas kodu

Merge pull request #10002 from zmstone/0220-fix-changelog-formatter

[build]: Make it easier to format change logs for alpha and beta releases
Zaiming (Stone) Shi 3 gadi atpakaļ
vecāks
revīzija
8bee16b36b

changes/ee/feat-9932-en.md → changes/ee/feat-9932.en.md


changes/ee/feat-9932-zh.md → changes/ee/feat-9932.zh.md


+ 0 - 12
scripts/changelog-lang-templates/en

@@ -1,12 +0,0 @@
-# ${version}
-
-## Enhancements
-
-$(section feat)
-
-$(section perf)
-
-## Bug fixes
-
-$(section fix)
-

+ 0 - 12
scripts/changelog-lang-templates/zh

@@ -1,12 +0,0 @@
-# ${version}
-
-## 增强
-
-$(section feat)
-
-$(section perf)
-
-## 修复
-
-$(section fix)
-

+ 0 - 69
scripts/format-changelog.sh

@@ -1,69 +0,0 @@
-#!/bin/bash
-set -euo pipefail
-shopt -s nullglob
-export LANG=C.UTF-8
-
-[ "$#" -ne 4 ] && {
-    echo "Usage $0 <emqx|emqx-enterprise> <LAST TAG> <VERSION> <OUTPUT DIR>" 1>&2;
-    exit 1
-}
-
-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=("")
-
-echo "generated changelogs from tag:${last_tag} to HEAD"
-
-item() {
-    local filename pr indent
-    filename="${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}"
-        indent="  "
-    done < "${filename}"
-    echo
-}
-
-section() {
-    local prefix=$1
-    for file in "${changes[@]}"; do
-        if [[ $file =~ .*$prefix-.*$language.md ]]; then
-            item "$file"
-        fi
-    done
-}
-
-generate() {
-    local language=$1
-    local output="$output_dir/${version}_$language.md"
-    local template_file="$templates_dir/$language"
-    local template
-    if [ -f "$template_file" ]; then
-        template=$(cat "$template_file")
-        eval "echo \"$template\" > $output"
-    else
-        echo "Invalid language ${language}" 1>&2;
-        exit 1
-    fi
-}
-
-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

+ 3 - 3
scripts/rel/cut.sh

@@ -223,9 +223,9 @@ generate_changelog () {
             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
+    ./scripts/rel/format-changelog.sh -b "${from_tag}" -l 'en' -v "$TAG" > "changes/${TAG}.en.md"
+    ./scripts/rel/format-changelog.sh -b "${from_tag}" -l 'zh' -v "$TAG" > "changes/${TAG}.zh.md"
+    git add changes/"${TAG}".*.md
     [ -n "$(git status -s)" ] && git commit -m "chore: Generate changelog for ${TAG}"
 }
 

+ 143 - 0
scripts/rel/format-changelog.sh

@@ -0,0 +1,143 @@
+#!/bin/bash
+set -euo pipefail
+shopt -s nullglob
+export LANG=C.UTF-8
+
+logerr() {
+    echo "$(tput setaf 1)ERROR: $1$(tput sgr0)" 1>&2
+}
+
+usage() {
+    cat <<EOF
+$0 [option]
+options:
+  -h|--help: print this usages info
+  -b|--base:
+    The git tag of compare base to find included changes.
+    e.g. v5.0.18, e5.0.0 etc.
+  -v|--version:
+    The tag to be released
+    e.g. v5.0.19, e5.0.1-alpha.1 etc.
+  -l|--lang: en | zh
+EOF
+}
+
+while [ "$#" -gt 0 ]; do
+    case $1 in
+        -h|--help)
+            usage
+            exit 0
+            ;;
+        -b|--base)
+            shift
+            BASE_TAG="$1"
+            shift
+            ;;
+        -v|--version)
+            shift
+            TEMPLATE_VSN_HEADING="$1"
+            shift
+            ;;
+        -l|--lang)
+            shift
+            LANGUAGE="$1"
+            shift
+            ;;
+        *)
+            logerr "Unknown option $1"
+            exit 1
+            ;;
+    esac
+done
+
+case "${LANGUAGE:-}" in
+    en|zh)
+        true
+        ;;
+    *)
+        logerr "-l|--lang must be 'en' or 'zh'"
+        exit 1
+        ;;
+esac
+
+case "${BASE_TAG:-}" in
+    v*)
+        PROFILE="emqx"
+        ;;
+    e*)
+        PROFILE="emqx-enterprise"
+        ;;
+    *)
+        logerr "Unsupported -b|--base option, must be v* or e*"
+        exit 1
+        ;;
+esac
+
+TEMPLATE_VSN_HEADING="${TEMPLATE_VSN_HEADING:-<VSN-TAG>}"
+
+top_dir="$(git rev-parse --show-toplevel)"
+declare -a PRS
+PRS=("")
+
+format_one_pr() {
+    local filename pr_num indent
+    filename="${1}"
+    pr_num="$(echo "${filename}" | sed -E 's/.*-([0-9]+)\.[a-z]+\.md$/\1/')"
+    re='^[0-9]+$'
+    if ! [[ $pr_num =~ $re ]]; then
+        logerr "bad filename format: $filename"
+    fi
+    indent="- [#${pr_num}](https://github.com/emqx/emqx/pull/${pr_num}) "
+    while read -r line; do
+        echo "${indent}${line}"
+        indent="  "
+    done < "${filename}"
+    echo
+}
+
+section() {
+    local prefix=$1
+    for file in "${PRS[@]}"; do
+        if [[ $file =~ .*$prefix-.*$LANGUAGE.md ]]; then
+            format_one_pr "$file"
+        fi
+    done
+}
+
+changes_dir=("$top_dir/changes/ce")
+if [ "$PROFILE" == "emqx-enterprise" ]; then
+    changes_dir+=("$top_dir/changes/ee")
+fi
+
+while read -d "" -r file; do
+   PRS+=("$file")
+done < <(git diff --name-only -z -a "tags/${BASE_TAG}...HEAD" "${changes_dir[@]}")
+
+TEMPLATE_FEAT_CHANGES="$(section 'feat')"
+TEMPLATE_PERF_CHANGES="$(section 'perf')"
+TEMPLATE_FIX_CHANGES="$(section 'fix')"
+
+case "$LANGUAGE" in
+    en)
+        TEMPLATE_ENH_HEADING="Enhancements"
+        TEMPLATE_FIX_HEADING="Bug Fixes"
+        ;;
+    zh)
+        TEMPLATE_ENH_HEADING="增强"
+        TEMPLATE_FIX_HEADING="修复"
+        ;;
+esac
+
+cat <<EOF
+# ${TEMPLATE_VSN_HEADING}
+
+## ${TEMPLATE_ENH_HEADING}
+
+${TEMPLATE_FEAT_CHANGES}
+
+${TEMPLATE_PERF_CHANGES}
+
+## ${TEMPLATE_FIX_HEADING}
+
+${TEMPLATE_FIX_CHANGES}
+EOF