Skip to content

Commit

Permalink
fix_: tag version script (#5823)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin authored Sep 11, 2024
1 parent fb150f3 commit bcaf8eb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 49 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/commit-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ jobs:
echo "exit_code=$exit_code" >> $GITHUB_OUTPUT
if [[ $exit_code -ne 0 ]]; then
invalid_commit_messages=$(echo $output | sed '1d;$d')
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "error_message<<$EOF" >> "$GITHUB_ENV"
echo "$output" >> "$GITHUB_ENV"
echo "invalid_commit_messages" >> "$GITHUB_ENV"
echo "$EOF" >> "$GITHUB_ENV"
else
has_breaking_changes=$(echo "$output" | sed -n '2p')
has_breaking_changes=$(echo "$output" | tail -n 1)
echo "has_breaking_changes=$has_breaking_changes" >> $GITHUB_OUTPUT
fi
Expand Down
34 changes: 1 addition & 33 deletions _assets/scripts/commit_check.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
#!/usr/bin/env bash

set -euo pipefail

parse_commits() {

BASE_BRANCH=${BASE_BRANCH:-develop}

start_commit=${1:-origin/${BASE_BRANCH}}
end_commit=${2:-HEAD}
is_breaking_change=false
exit_code=0

echo "checking commits between: $start_commit $end_commit"
# Run the loop in the current shell using process substitution
while IFS= read -r message || [ -n "$message" ]; do
# Check if commit message follows conventional commits format
if [[ $message =~ ^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?(\_|!):.*$ ]]; then
# Check for breaking changes
if [[ ${BASH_REMATCH[3]} == *'!'* ]]; then
is_breaking_change=true
fi
else
echo "Commit message \"$message\" is not well-formed"
exit_code=1
fi
done < <(git log --format=%s "$start_commit".."$end_commit")

if [[ $exit_code -ne 0 ]]; then
exit ${exit_code}
fi

echo "$is_breaking_change"
}

source _assets/scripts/parse_commits.sh
parse_commits "$@"
38 changes: 38 additions & 0 deletions _assets/scripts/parse_commits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

# The output of this script is as follows:
# 1. One line "checking commits between: <start_commit> <end_commit>"
# 2. One line for each commit message that is not well-formed
# 3. One line with the value of "is_breaking_change" (true/false)

set -euo pipefail

source _assets/scripts/colors.sh

parse_commits() {

BASE_BRANCH=${BASE_BRANCH:-develop}

start_commit=${1:-origin/${BASE_BRANCH}}
end_commit=${2:-HEAD}
is_breaking_change=false
exit_code=0

echo -e "${GRN}Checking commits between:${RST} $start_commit $end_commit"
# Run the loop in the current shell using process substitution
while IFS= read -r message || [ -n "$message" ]; do
# Check if commit message follows conventional commits format
if [[ $message =~ ^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?(\_|!):.*$ ]]; then
# Check for breaking changes
if [[ ${BASH_REMATCH[3]} == *'!'* ]]; then
is_breaking_change=true
fi
else
echo -e "${YLW}Commit message is not well-formed:${RST} \"$message\""
exit_code=1
fi
done < <(git log --format=%s "$start_commit".."$end_commit")

echo "$is_breaking_change"
exit ${exit_code}
}
47 changes: 33 additions & 14 deletions _assets/scripts/tag_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

set -euo pipefail

source _assets/scripts/commit_check.sh
source _assets/scripts/parse_commits.sh
source _assets/scripts/colors.sh

get_latest_tag() {
# Get the latest tag on develop
Expand All @@ -28,26 +29,44 @@ bump_version() {
}

calculate_new_version() {
# Get the latest tag
latest_tag=$(get_latest_tag)

echo "calculating new tag from $latest_tag and $1" >&2
target_commit=$1
latest_tag=$2

# Parse commits to determine if there are breaking changes
is_breaking_change=$(parse_commits "$latest_tag" "$1")
output=$(parse_commits "$latest_tag" "$target_commit")
exit_code=$?

# Bump version accordingly
echo "$(bump_version "$latest_tag" "$is_breaking_change")"
}
a=$(echo "$output" | sed '$d')
is_breaking_change=$(echo "$output" | tail -n 1)

echo "$a" >&2

main() {
new_version=$(calculate_new_version "$1")
echo "calculated new version: $new_version" >&2

git tag -a "$new_version" "$1" -m "release $new_version"
if [[ $is_breaking_change == 'true' ]]; then
echo -e "${YLW}Breaking change detected${RST}" >&2
fi

if [[ $exit_code -ne 0 && $is_breaking_change != true ]]; then
echo -e "${YLW}Some commits are ill-formed, can not to auto-calculate new version${RST}" >&2
read -p "Any of the commits above have a breaking change? (y/n): " yn
case $yn in
[Yy]* ) is_breaking_change=true;;
[Nn]* ) is_breaking_change=false;;
* ) echo "Please answer yes or no."; exit 1;;
esac
fi

# Bump version accordingly
bump_version "$latest_tag" "$is_breaking_change"
}

latest_tag=$(get_latest_tag)
echo -e "${GRN}Latest tag found:${RST} $latest_tag" >&2

target_commit=${1:-HEAD}
echo -e "${GRN}Calculating new version for:${RST} $target_commit" >&2

new_version=$(calculate_new_version "$target_commit" "$latest_tag")
echo -e "${GRN}Calculated new version:${RST} $new_version" >&2

main "$target_commit"
git tag -a "$new_version" "$target_commit" -m "release $new_version"

0 comments on commit bcaf8eb

Please sign in to comment.