Skip to content

Commit

Permalink
Working on overall improving the code so it's easier to digest (#6)
Browse files Browse the repository at this point in the history
* Working on overall improving the code so it's easier to digest

This should make maintaining and managing much easier

* GHA improvement

* Fix CI pipeline

* Try to get the right branch name

* Use bats for testing

* Update github action
  • Loading branch information
DragosDumitrache authored Jan 17, 2024
1 parent 44846e9 commit 2deb6af
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 121 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/test-and-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
if: github.event_name == 'pull_request'
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- uses: actions/checkout@v4
if: github.event_name != 'pull_request'
with:
fetch-depth: 0
- name: Run tests
run: |
./run_tests.sh
sudo apt-get upgrade && apt-get update
sudo apt-get install -y bats
./test.sh
- uses: ./versioner/
id: versioner
- name: publish tag
Expand Down
99 changes: 55 additions & 44 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
#!/usr/bin/env bash
source /work/version.sh
#!/usr/bin/env bats
set -e
source ./version.sh

source critic.sh
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}

@test "Version if not git repo" {
result=$(semver "1")
[ "$result" == "1.0.0-SNAPSHOT" ]
}

_describe "Version if not git repo"
_test "Should be 1.0.0-SNAPSHOT" semver "1"
_assert _output_equals "1.0.0-SNAPSHOT"
@test "Version on first commit default branch" {
result=$(_calculate_version "master" "vskd341" "0" "0" "master")
[ "$result" == "0.0.0" ]
}

_describe "Version on first commit default branch"
_test "Should be 0.0.0" calculate_version "master" "vskd341" "0" "0" "master"
_assert _output_equals "0.0.0"
@test "Version on second commit default branch" {
result=$(_calculate_version "master" "0.0.2" "0" "0" "master")
[ "$result" == "0.0.2" ]
}

_describe "Version on second commit default branch"
_test "Should be 0.0.2" calculate_version "master" "0.0.2" "0" "0" "master"
_assert _output_equals "0.0.2"
@test "Version on fifth commit default branch" {
result=$(_calculate_version "master" "0.0.6" "0" "0" "master")
[ "$result" == "0.0.6" ]
}

_describe "Version on fifth commit default branch"
_test "Should be 0.0.6" calculate_version "master" "0.0.6" "0" "0" "master"
_assert _output_equals "0.0.6"
@test "Version on with different minor on default branch" {
result=$(_calculate_version "master" "0.0.1-5-aaghas27" "0" "1" "master")
[ "$result" == "0.1.0" ]
}

_describe "Version on with different minor on default branch"
_test "Should be 0.1.0" calculate_version "master" "0.0.1-5-aaghas27" "0" "1" "master"
_assert _output_equals "0.1.0"
@test "Version on with different major on default branch" {
result=$(_calculate_version "master" "0.0.1-5-aaghas27" "1" "0" "master")
[ "$result" == "1.0.0" ]
}

_describe "Version on with different major on default branch"
_test "Should be 1.0.0" calculate_version "master" "0.0.1-5-aaghas27" "1" "0" "master"
_assert _output_equals "1.0.0"
@test "Version on with different major and minor on default branch" {
result=$(_calculate_version "master" "0.0.1-5-aaghas27" "1" "1" "master")
[ "$result" == "1.1.0" ]
}

_describe "Version on with different major and minor on default branch"
_test "Should be 1.1.0" calculate_version "master" "0.0.1-5-aaghas27" "1" "1" "master"
_assert _output_equals "1.1.0"
@test "Version on second commit on feature branch" {
result=$(_calculate_version "feature" "0.0.1-1-aaghas27" "0" "0" "master")
[ "$result" == "0.0.1-1-aaghas27-feature" ]
}

_describe "Version on second commit on feature branch"
_test "Should be 0.0.1-1-aaghas27-feature" calculate_version "feature" "0.0.1-1-aaghas27" "0" "0" "master"
_assert _output_equals "0.0.1-1-aaghas27-feature"
@test "Version on fifth commit on feature branch" {
result=$(_calculate_version "feature" "0.0.1-5-aaghas27" "0" "0" "master")
[ "$result" == "0.0.1-5-aaghas27-feature" ]
}

_describe "Version on fifth commit on feature branch"
_test "Should be 0.0.1-5-aaghas27-feature" calculate_version "feature" "0.0.1-5-aaghas27" "0" "0" "master"
_assert _output_equals "0.0.1-5-aaghas27-feature"
@test "Version on feature branch with different minor" {
result=$(_calculate_version "feature" "0.0.1-5-aaghas27" "0" "1" "master")
[ "$result" == "0.0.1-5-aaghas27-feature" ]
}

_describe "Version on feature branch with different minor"
_test "Should be 0.0.1-5-aaghas27-feature" calculate_version "feature" "0.0.1-5-aaghas27" "0" "1" "master"
_assert _output_equals "0.0.1-5-aaghas27-feature"
@test "Version on feature branch with different major" {
result=$(_calculate_version "feature" "0.0.1-5-aaghas27" "1" "0" "master")
[ "$result" == "0.0.1-5-aaghas27-feature" ]
}

_describe "Version on feature branch with different major"
_test "Should be 0.0.1-5-aaghas27-feature" calculate_version "feature" "0.0.1-5-aaghas27" "1" "0" "master"
_assert _output_equals "0.0.1-5-aaghas27-feature"

_describe "Version on feature branch with different major and minor"
_test "Should be 0.0.1-5-aaghas27-feature" calculate_version "feature" "0.0.1-5-aaghas27" "1" "1" "master"
_assert _output_equals "0.0.1-5-aaghas27-feature"

_describe "Version on default branch even if tag starts with v"
_test "Should be 0.0.2" calculate_version "master" "0.0.2" "0" "0" "master"
_assert _output_equals "0.0.2"
@test "Version on feature branch with different major and minor" {
result=$(_calculate_version "feature" "0.0.1-5-aaghas27" "1" "1" "master")
[ "$result" == "0.0.1-5-aaghas27-feature" ]
}
124 changes: 56 additions & 68 deletions version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,101 @@

#source lib.sh

function initialise_version {
function _initialise_version() {
# This will initialise your repository for versioner to be able to consume
if [[ ! -f "version.json" ]]; then
cat <<EOF >version.json
{
"default_branch": "master",
"major": "0",
"minor": "0"
}
{
"default_branch": "master",
"major": "0",
"minor": "0"
}
EOF
fi
}

function is_git_repository {
# Function to check if the current directory is a git repository
function _is_git_repository() {
[ -d ".git" ]
}

function sanitise_version {
maybe_starts_with_v=$1
no_v=${maybe_starts_with_v#v}
no_sha=${no_v%-*}
if [[ $no_sha != "$no_v" ]]; then
no_extra_patch=${no_sha%-*}
major=$(echo "$no_sha" | cut -d '.' -f1)
minor=$(echo "$no_sha" | cut -d '.' -f2)
patch_number=$(echo "$no_extra_patch" | cut -d '.' -f3)
if [[ $major -lt $future_major ]]; then
echo "${future_major}.${future_minor}.0"
function _calculate_version {
git_branch=$1
git_generated_version=$2
next_major=$3
next_minor=$4
default_branch=$5
if [[ ! "${git_generated_version}" =~ ^v?.+\..+\.[^-]+ ]]; then
# This case should never happen
git_generated_version="${next_major}.${next_minor}.0"
fi
final_version=$git_generated_version
if [[ $git_branch == "$default_branch" ]]; then
major=$(echo "$final_version" | cut -d '.' -f1)
minor=$(echo "$final_version" | cut -d '.' -f2)
patch_number=$(echo "$final_version" | cut -d '.' -f3)
if [[ $major -lt $next_major ]]; then
final_version="${next_major}.${next_minor}.0"
else
if [[ $minor -lt $future_minor ]]; then
echo "${major}.${future_minor}.0"
if [[ $minor -lt $next_minor ]]; then
final_version="${major}.${next_minor}.0"
else
git_generated_version="${major}.${minor}.${patch_number}"
echo "$git_generated_version"
final_version="${major}.${minor}.${patch_number}"
fi
fi
else
major=$(echo "$no_sha" | cut -d '.' -f1)
minor=$(echo "$no_sha" | cut -d '.' -f2)
patch_number=$(echo "$no_sha" | cut -d '.' -f3)
if [[ $major -lt $future_major ]]; then
echo "${future_major}.${future_minor}.0"
else
if [[ $minor -lt $future_minor ]]; then
echo "${major}.${future_minor}.0"
else
git_generated_version="${major}.${minor}.${patch_number}"
echo "$git_generated_version"
fi
fi
final_version="${git_generated_version}-${git_branch}"
fi
echo "$final_version" | tr -d '[:space:]'
}

function _extract_branch_name {
git_branch=$(git branch --show-current)
if [[ ! $git_branch ]]; then
git_branch=$(git name-rev --name-only $(git show -s --format=%H))
fi
echo $git_branch
}

function semver {
local git_branch latest_tag major_minor patch commits new_patch default_branch final_version
local next_major next_minor next_patch next_version
is_git_repo=$1

if [ "$is_git_repo" ]; then
echo "1.0.0-SNAPSHOT"
return 0
fi

initialise_version
git_branch=$(git branch --show-current)
if [[ ! $git_branch ]]; then
git_branch=$(git rev-parse --abbrev-ref HEAD)
fi
# If no version.json file is present, add it, then proceed to calculate the version
_initialise_version

git_branch=$(_extract_branch_name)

next_major=$(cat version.json | jq ".major" --raw-output)
next_minor=$(cat version.json | jq ".minor" --raw-output)
next_patch=0
default_branch=$(cat version.json | jq ".default_branch" --raw-output)

# Get the latest tag
latest_tag=$(git tag -l --sort=-creatordate | head -n 1)
if [ $latest_tag ]; then

major_minor=$(echo "$latest_tag" | cut -d '.' -f -2)
patch=$(echo "$latest_tag" | cut -d '.' -f 3)
# Get the list of commits since the latest tag
commits=$(git rev-list --count "$latest_tag"..HEAD)
# shellcheck disable=SC2004
new_patch=$(($commits + $patch))
next_patch=$(($commits + $patch))
next_version="${major_minor}.${next_patch}"
else
new_patch=$patch
next_version="${next_major}.${next_minor}.${next_patch}"
fi
git_generated_version="${major_minor}.${new_patch}"
future_major=$(cat version.json | jq ".major" --raw-output)
future_minor=$(cat version.json | jq ".minor" --raw-output)
default_branch=$(cat version.json | jq ".default_branch" --raw-output)

final_version=$(calculate_version "$git_branch" "$git_generated_version" "$future_major" "$future_minor" "$default_branch")
final_version=$(_calculate_version "$git_branch" "$next_version" "$next_major" "$next_minor" "$default_branch")
echo "$final_version"
}

function calculate_version {
git_branch=$1
git_generated_version=$2
future_major=$3
future_minor=$4
default_branch=$5
if [[ ! "${git_generated_version}" =~ ^v?.+\..+\.[^-]+ ]]; then
git_generated_version="${future_major}.${future_minor}.0"
fi
final_version=$git_generated_version
if [[ $git_branch == "$default_branch" ]]; then
final_version=$(sanitise_version "$git_generated_version")
else
final_version="${git_generated_version}-${git_branch}"
final_version=${final_version#v}
fi

echo "$final_version" | tr -d '[:space:]'
}

if [ "${BASH_SOURCE[0]}" = "$0" ]; then
semver "$(is_git_repository)"
semver "$(_is_git_repository)"
fi
13 changes: 6 additions & 7 deletions versioner/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ outputs:
runs:
using: composite
steps:
- name: Download versioner
shell: bash
working-directory: ${{ github.workspace }}
run: |
curl -o the_versioner -L https://github.com/DragosDumitrache/versioner/releases/download/2.2.3/version.sh
chmod +x the_versioner
- uses: actions/checkout@v4
with:
repository: DragosDumitrache/versioner
ref: master
path: tools
- name: Generate and tag a new version
id: semver
working-directory: ${{ github.workspace }}
shell: bash
run: |
version_tag=$(./the_versioner)
version_tag=$(./tools/version.sh)
echo "Version is: $version_tag"
echo "version=$(echo $version_tag)" >> $GITHUB_OUTPUT

0 comments on commit 2deb6af

Please sign in to comment.