Skip to content

Commit

Permalink
add format_changelog script
Browse files Browse the repository at this point in the history
  • Loading branch information
momentmaker committed May 3, 2024
1 parent a34a17a commit a6b8ddf
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions tools/ci/format_changelog
Original file line number Diff line number Diff line change
@@ -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 <<EOF
${changelog_content}
## ${tag}
${changesets}
EOF
done
}

set_new_changelog_content() {
append_changelog_content
read -d '' new_changelog <<EOF
${changelog_content}
${current_changelog}
EOF

echo "$new_changelog" > 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 <<EOF
# Changelog Chainlink Core
## ${version} - PREVIEW
EOF

current_changelog=$(cat CHANGELOG.md.tmp)
is_current_version=false
checking_current_changeset=false
changesets_index=0
tags_list=( "#nops" "#added" "#changed" "#removed" "#updated" "#deprecation_notice" "#breaking_change" "#db_update" "#wip" "#bugfix" "#internal" "#untagged")

create_changesets_json
create_tags_json

while IFS= read -r line; do
# break when hits the next version
if [[ $line == "## "* ]] && [[ $is_current_version = true ]]; then
break
fi

# look for the latest version
if [[ $line == "## "* ]]; then
is_current_version=true
fi

if [[ $is_current_version = true ]]; then
# saving each changeset to changeset.json
# check for start of changeset entry as it could be multi-lined entry
if [[ $line == "- ["* ]] && [[ $checking_current_changeset = true ]]; then
checking_current_changeset=false
changesets_index=$((changesets_index+1))
append_changeset_content "$line"
elif [[ $line == "- ["* ]] && [[ $checking_current_changeset = false ]]; then
checking_current_changeset=true
changesets_index=$((changesets_index+1))
append_changeset_content "$line"
elif [[ $line != "##"* ]]; then
append_changeset_content "$line"
fi
fi
done < CHANGELOG.md

match_tags
set_new_changelog_content
cleanup

0 comments on commit a6b8ddf

Please sign in to comment.