From a6b8ddfdfb967e9ee32479177b4a9dbb8e1523d1 Mon Sep 17 00:00:00 2001 From: Frank Zhu Date: Fri, 3 May 2024 15:02:18 -0500 Subject: [PATCH] add format_changelog script --- tools/ci/format_changelog | 134 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100755 tools/ci/format_changelog diff --git a/tools/ci/format_changelog b/tools/ci/format_changelog new file mode 100755 index 00000000000..a939b021656 --- /dev/null +++ b/tools/ci/format_changelog @@ -0,0 +1,134 @@ +#!/usr/bin/env bash + +create_changesets_json() { + echo "[[]]" > changesets.json +} + +create_tags_json() { + json="{}" + for tag in "${tags_list[@]}"; do + tag=${tag:1} + json=$(jq --arg k "$tag" '.[$k] = []' <<< "$json") + done + echo "$json" > tags.json +} + +append_changeset_content() { + if [[ $1 != "" ]]; then + jq --argjson idx "$changesets_index" --arg str "$1" \ + '.[$idx] += [$str]' changesets.json > tmp.json && mv tmp.json changesets.json + fi +} + +append_changelog_content() { + for tag in "${tags_list[@]}"; do + tag=${tag:1} + array_length=$(jq -r --arg key "$tag" '.[$key] | length' tags.json) + if [[ $array_length -eq 0 ]]; then + continue + fi + changesets=$(jq -r --arg key "$tag" '.[$key] | join("\n\n")' tags.json) + read -d '' changelog_content < CHANGELOG.md +} + +# checks for tags in each changeset entry and append to tags.json +match_tags() { + changesets_with_index=$(jq -r 'to_entries | .[] | "\(.key) \(.value | join(" "))"' changesets.json) + + echo "$changesets_with_index" | while IFS= read -r line; do + index="${line%% *}" + changeset_content="${line#* }" + changeset_formatted=$(jq -r --argjson idx "$index" '.[$idx] | join("\n")' changesets.json) + found_tag="" + for tag in "${tags_list[@]}"; do + if [[ "$changeset_content" =~ $tag ]]; then + found_tag=${tag:1} + jq --arg key "$found_tag" --arg val "$changeset_formatted" \ + '.[$key] += [$val]' tags.json > tmp.json && mv tmp.json tags.json + fi + done + if [[ $found_tag == "" ]] && [[ ! -z $changeset_content ]]; then + found_tag="untagged" + jq --arg key "$found_tag" --arg val "$changeset_formatted" \ + '.[$key] += [$val]' tags.json > tmp.json && mv tmp.json tags.json + fi + done +} + +cleanup() { + rm -f CHANGELOG.md.tmp + rm -f changesets.json + rm -f tags.json +} + +### SCRIPT STARTS HERE ### +cp CHANGELOG.md CHANGELOG.md.tmp +tail -n +2 CHANGELOG.md > CHANGELOG.md.tmp + +pnpm changeset version + +version=$(jq -r '.version' package.json) +read -d '' changelog_content <