feat(script): add changelog.sh for doing changelogs

This commit is contained in:
Rick Barenthin 2022-05-25 00:55:58 +02:00
parent 1121858825
commit ee8d4f1bd4

View File

@ -20,12 +20,11 @@ reverse() {
} }
generate_config() { generate_config() {
jq --null-input --arg version "0.0.0" --arg sha "" '{ "version": $version, "sha": $sha}' > $config_file jq --null-input --arg version "0.0.0" --arg sha "" --arg url "$1" '{ "version": $version, "sha": $sha, "url": $url}' > $config_file
} }
update_config() { update_config() {
sha="$(git rev-parse --short HEAD)" config=$(jq '.sha |= $sha | .version |= $version' --arg sha "$1" --arg version "$2" $config_file)
config=$(jq '.sha |= $sha | .version |= $version' --arg sha "$sha" --arg version "$1" $config_file)
echo "$config" > $config_file echo "$config" > $config_file
} }
@ -95,6 +94,16 @@ calculate_version() {
echo "${version_number}${pre_release}${build}" echo "${version_number}${pre_release}${build}"
} }
get_git_url() {
url=$(git config --get remote.origin.url)
if ! echo "$url" | grep -q "^http"; then
url=$(echo "$url" | cut -d@ -f2 | sed 's/:/\//')
url="https://$url"
fi
echo "$url" | sed 's/\.git$//'
}
get_git_commits() { get_git_commits() {
since_hash="$(jq '.sha' < $config_file | tr -d '"')" since_hash="$(jq '.sha' < $config_file | tr -d '"')"
if [ -n "$since_hash" ]; then if [ -n "$since_hash" ]; then
@ -104,6 +113,14 @@ get_git_commits() {
fi fi
} }
get_git_commit_hash() {
git rev-parse --short "$1"
}
get_git_first_commit_hash() {
git rev-list --max-parents=0 --abbrev-commit HEAD
}
get_git_commit_subject() { get_git_commit_subject() {
git show -s --format=%s "$1" git show -s --format=%s "$1"
} }
@ -165,23 +182,47 @@ get_breaking_change() {
fi fi
} }
format_entry_line() {
if [ -n "$2" ]; then
line="**$2:** $1"
else
line="$1"
fi
url="$(jq '.url' < $config_file | tr -d '"')"
echo "$line ([$3]($url/commit/$3))" | sed -e '2,$s/^[ ]*/ /'
}
generate_changelog_entry() { generate_changelog_entry() {
is_major=0 is_major=0
is_minor=0 is_minor=0
is_patch=0 is_patch=0
version="$(jq '.version' < $config_file | tr -d '"')" version=$(jq '.version' < $config_file | tr -d '"')
from_hash=$(jq '.sha' < $config_file | tr -d '"')
if [ -z "$from_hash" ]; then
from_hash=$(get_git_first_commit_hash)
fi
to_hash=$(get_git_commit_hash HEAD)
breakingchanges="### BREAKING CHANGES\n" breakingchanges=""
features="### Features\n" features=""
bugfixes="### Bug fixes\n" bugfixes=""
builds="### Build System" builds=""
chores="### Chores\n" chores=""
test="### Chores\n" cis=""
docs=""
styles=""
refactors=""
perfs=""
tests=""
reverts=""
for commit in $(get_git_commits); do for commit in $(get_git_commits); do
git_subject=$(get_git_commit_subject "$commit") git_subject=$(get_git_commit_subject "$commit")
git_body=$(get_git_commit_body "$commit") git_body=$(get_git_commit_body "$commit")
hash=$(get_git_commit_hash "$commit")
type=$(get_commit_type "$git_subject") type=$(get_commit_type "$git_subject")
scope=$(get_commit_scope "$git_subject") scope=$(get_commit_scope "$git_subject")
@ -190,13 +231,7 @@ generate_changelog_entry() {
footer=$(get_commit_footer "$git_body") footer=$(get_commit_footer "$git_body")
breakingchange=$(get_breaking_change "$footer") breakingchange=$(get_breaking_change "$footer")
is_breaking=$(is_breaking_change "$git_subject" "$footer") is_breaking=$(is_breaking_change "$git_subject" "$footer")
line=$(format_entry_line "$description" "$scope" "$hash")
if [ -n "$scope" ]; then
entry="**$scope:** $description"
else
entry="$description"
fi
entry=$(echo "$entry" | sed -e '2,$s/^[ ]*/ /')
if [ "$is_breaking" -eq 1 ] && [ -z "$breakingchange" ]; then if [ "$is_breaking" -eq 1 ] && [ -z "$breakingchange" ]; then
breakingchange="$body" breakingchange="$body"
@ -211,41 +246,48 @@ generate_changelog_entry() {
case "$type" in case "$type" in
feat) feat)
is_minor=1 is_minor=1
features="$features\n* $entry\n" features="$features\n* $line\n"
;; ;;
fix) fix)
is_patch=1 is_patch=1
bugfixes="$bugfixes\n* $entry\n" bugfixes="$bugfixes\n* $line\n"
;; ;;
build) build)
builds="$builds\n* $entry\n" builds="$builds\n* $line\n"
;; ;;
chore) chore)
chores="$chores\n* $entry\n" chores="$chores\n* $line\n"
;; ;;
ci) ci)
cis="$cis\n* $line\n"
;; ;;
docs) docs)
docs="$docs\n* $line\n"
;; ;;
style) style)
styles="$styles\n* $line\n"
;; ;;
refactor) refactor)
refactors="$refactors\n* $line\n"
;; ;;
perf) perf)
perfs="$perfs\n* $line\n"
;; ;;
test) test)
tests="$tests\n* $line\n"
;; ;;
revert) revert)
reverts="$reverts\n* $line\n"
;; ;;
esac esac
done done
@ -258,24 +300,49 @@ generate_changelog_entry() {
version=$(calculate_version "$version" "patch") version=$(calculate_version "$version" "patch")
fi fi
update_config "$version" update_config "$to_hash" "$version"
entry="" entry=""
if [ -n "$breakingchanges" ]; then
if [ "$breakingchanges" != "### BREAKING CHANGES\n" ]; then entry="$entry### BREAKING CHANGES\n$breakingchanges\n\n"
entry="$entry$breakingchanges\n\n"
fi fi
if [ "$bugfixes" != "### Bug fixes\n" ]; then if [ -n "$bugfixes" ]; then
entry="$entry$bugfixes\n\n" entry="$entry### Bug fixes\n$bugfixes\n\n"
fi fi
if [ "$features" != "### Features\n" ]; then if [ -n "$features" ]; then
entry="$entry$features\n\n" entry="$entry### Features\n$features\n\n"
fi
if [ -n "$reverts" ]; then
entry="$entry### Reverts\n$reverts\n\n"
fi
if [ -n "$builds" ]; then
entry="$entry### Build System\n$builds\n\n"
fi
if [ -n "$chores" ]; then
entry="$entry### Chores\n$chores\n\n"
fi
if [ -n "$cis" ]; then
entry="$entry### CIs\n$cis\n\n"
fi
if [ -n "$docs" ]; then
entry="$entry### Docs\n$docs\n\n"
fi
if [ -n "$tests" ]; then
entry="$entry### Tests\n$tests\n\n"
fi
if [ -n "$styles" ]; then
entry="$entry### Styles\n$styles\n\n"
fi
if [ -n "$refactors" ]; then
entry="$entry### Refactors\n$refactors\n\n"
fi
if [ -n "$perfs" ]; then
entry="$entry### Perfs\n$perfs\n\n"
fi fi
if [ -n "$entry" ]; then if [ -n "$entry" ]; then
entry="## [$version] ($(date '+%Y-%m-%d'))\n\n\n$entry" url="$(jq '.url' < $config_file | tr -d '"')"
entry="## [$version]($url/compare/$from_hash...$to_hash) ($(date '+%Y-%m-%d'))\n\n\n$entry"
fi fi
echo "$entry" echo "$entry"
} }
@ -300,7 +367,7 @@ new_changelog_entry() {
init() { init() {
if [ ! -f "$config_file" ]; then if [ ! -f "$config_file" ]; then
echo "Generating a config ... " echo "Generating a config ... "
generate_config generate_config "$(get_git_url)"
new_changelog new_changelog
else else
echo "Using existing config" echo "Using existing config"