feat(script): add changelog.sh for doing changelogs
This commit is contained in:
parent
1121858825
commit
ee8d4f1bd4
@ -20,12 +20,11 @@ reverse() {
|
||||
}
|
||||
|
||||
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() {
|
||||
sha="$(git rev-parse --short HEAD)"
|
||||
config=$(jq '.sha |= $sha | .version |= $version' --arg sha "$sha" --arg version "$1" $config_file)
|
||||
config=$(jq '.sha |= $sha | .version |= $version' --arg sha "$1" --arg version "$2" $config_file)
|
||||
echo "$config" > $config_file
|
||||
}
|
||||
|
||||
@ -95,6 +94,16 @@ calculate_version() {
|
||||
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() {
|
||||
since_hash="$(jq '.sha' < $config_file | tr -d '"')"
|
||||
if [ -n "$since_hash" ]; then
|
||||
@ -104,6 +113,14 @@ get_git_commits() {
|
||||
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() {
|
||||
git show -s --format=%s "$1"
|
||||
}
|
||||
@ -165,23 +182,47 @@ get_breaking_change() {
|
||||
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() {
|
||||
is_major=0
|
||||
is_minor=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"
|
||||
features="### Features\n"
|
||||
bugfixes="### Bug fixes\n"
|
||||
builds="### Build System"
|
||||
chores="### Chores\n"
|
||||
test="### Chores\n"
|
||||
breakingchanges=""
|
||||
features=""
|
||||
bugfixes=""
|
||||
builds=""
|
||||
chores=""
|
||||
cis=""
|
||||
docs=""
|
||||
styles=""
|
||||
refactors=""
|
||||
perfs=""
|
||||
tests=""
|
||||
reverts=""
|
||||
|
||||
for commit in $(get_git_commits); do
|
||||
git_subject=$(get_git_commit_subject "$commit")
|
||||
git_body=$(get_git_commit_body "$commit")
|
||||
hash=$(get_git_commit_hash "$commit")
|
||||
|
||||
type=$(get_commit_type "$git_subject")
|
||||
scope=$(get_commit_scope "$git_subject")
|
||||
@ -190,13 +231,7 @@ generate_changelog_entry() {
|
||||
footer=$(get_commit_footer "$git_body")
|
||||
breakingchange=$(get_breaking_change "$footer")
|
||||
is_breaking=$(is_breaking_change "$git_subject" "$footer")
|
||||
|
||||
if [ -n "$scope" ]; then
|
||||
entry="**$scope:** $description"
|
||||
else
|
||||
entry="$description"
|
||||
fi
|
||||
entry=$(echo "$entry" | sed -e '2,$s/^[ ]*/ /')
|
||||
line=$(format_entry_line "$description" "$scope" "$hash")
|
||||
|
||||
if [ "$is_breaking" -eq 1 ] && [ -z "$breakingchange" ]; then
|
||||
breakingchange="$body"
|
||||
@ -211,41 +246,48 @@ generate_changelog_entry() {
|
||||
case "$type" in
|
||||
feat)
|
||||
is_minor=1
|
||||
features="$features\n* $entry\n"
|
||||
features="$features\n* $line\n"
|
||||
;;
|
||||
|
||||
fix)
|
||||
is_patch=1
|
||||
bugfixes="$bugfixes\n* $entry\n"
|
||||
bugfixes="$bugfixes\n* $line\n"
|
||||
;;
|
||||
|
||||
build)
|
||||
builds="$builds\n* $entry\n"
|
||||
builds="$builds\n* $line\n"
|
||||
;;
|
||||
|
||||
chore)
|
||||
chores="$chores\n* $entry\n"
|
||||
chores="$chores\n* $line\n"
|
||||
;;
|
||||
|
||||
ci)
|
||||
cis="$cis\n* $line\n"
|
||||
;;
|
||||
|
||||
docs)
|
||||
docs="$docs\n* $line\n"
|
||||
;;
|
||||
|
||||
style)
|
||||
styles="$styles\n* $line\n"
|
||||
;;
|
||||
|
||||
refactor)
|
||||
refactors="$refactors\n* $line\n"
|
||||
;;
|
||||
|
||||
perf)
|
||||
perfs="$perfs\n* $line\n"
|
||||
;;
|
||||
|
||||
test)
|
||||
tests="$tests\n* $line\n"
|
||||
;;
|
||||
|
||||
revert)
|
||||
reverts="$reverts\n* $line\n"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
@ -258,24 +300,49 @@ generate_changelog_entry() {
|
||||
version=$(calculate_version "$version" "patch")
|
||||
fi
|
||||
|
||||
update_config "$version"
|
||||
update_config "$to_hash" "$version"
|
||||
|
||||
entry=""
|
||||
|
||||
if [ "$breakingchanges" != "### BREAKING CHANGES\n" ]; then
|
||||
entry="$entry$breakingchanges\n\n"
|
||||
if [ -n "$breakingchanges" ]; then
|
||||
entry="$entry### BREAKING CHANGES\n$breakingchanges\n\n"
|
||||
fi
|
||||
if [ "$bugfixes" != "### Bug fixes\n" ]; then
|
||||
entry="$entry$bugfixes\n\n"
|
||||
if [ -n "$bugfixes" ]; then
|
||||
entry="$entry### Bug fixes\n$bugfixes\n\n"
|
||||
fi
|
||||
if [ "$features" != "### Features\n" ]; then
|
||||
entry="$entry$features\n\n"
|
||||
if [ -n "$features" ]; then
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
echo "$entry"
|
||||
}
|
||||
|
||||
@ -300,7 +367,7 @@ new_changelog_entry() {
|
||||
init() {
|
||||
if [ ! -f "$config_file" ]; then
|
||||
echo "Generating a config ... "
|
||||
generate_config
|
||||
generate_config "$(get_git_url)"
|
||||
new_changelog
|
||||
else
|
||||
echo "Using existing config"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user