Просмотр исходного кода

Merge pull request #9971 from lafirest/refactor/format_changes

chore: refactor the format-changelog script
lafirest 3 лет назад
Родитель
Сommit
8a822c664c

+ 0 - 0
changes/ce/.gitkeep


+ 0 - 0
changes/ee/.gitkeep


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

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

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

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

+ 40 - 37
scripts/format-changelog.sh

@@ -3,20 +3,27 @@ set -euo pipefail
 shopt -s nullglob
 export LANG=C.UTF-8
 
-[ "$#" -ne 2 ] && {
-    echo "Usage $0 <EMQX version> <en|zh>" 1>&2;
+[ "$#" -ne 4 ] && {
+    echo "Usage $0 <emqx|emqx-enterprise> <LAST TAG> <VERSION> <OUTPUT DIR>" 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 <<EOF
-# ${version}
-
-## Enhancements
-
-$(section feat)
-
-$(section perf)
-
-## Bug fixes
-
-$(section fix)
-EOF
-elif [ "${language}" = "zh" ]; then
-     cat <<EOF
-# ${version}
-
-## 增强
-
-$(section feat)
+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
+}
 
-$(section perf)
+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[@]}")
 
-$(section fix)
-EOF
-else
-    echo "Invalid language ${language}" 1>&2;
-    exit 1
-fi
+for language in "${languages[@]}"; do
+    generate "$language"
+done

+ 18 - 4
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}"
 }