From 774aef254f52e620a9631e168d81ce0c54c3d1c1 Mon Sep 17 00:00:00 2001 From: Jackie Weng Date: Wed, 20 Nov 2024 23:10:27 +1300 Subject: [PATCH 1/4] feat: support for custom version suffix --- action.yml | 9 +++- spec/git-version-spec.cr | 114 +++++++++++++++++++++++++++++++++++++++ src/git-version.cr | 22 ++++---- src/main.cr | 3 +- 4 files changed, 134 insertions(+), 14 deletions(-) diff --git a/action.yml b/action.yml index 4080496..b40095a 100644 --- a/action.yml +++ b/action.yml @@ -28,6 +28,9 @@ inputs: prefix: description: 'The prefix to use in the version' required: false + suffix: + description: "The suffix to use in the version" + required: false log-paths: description: 'The paths to be used to calculate changes (comma-separated)' required: false @@ -63,7 +66,8 @@ runs: --dev-branch "${{ inputs.dev-branch }}" \ --minor-identifier="${{ inputs.minor-identifier }}" \ --major-identifier="${{ inputs.major-identifier }}" \ - --version-prefix "${{ inputs.prefix }}") + --version-prefix "${{ inputs.prefix }}") \ + --version-suffix "${{ inputs.suffix }}") echo "previous-version=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT echo "Previous Version: $PREVIOUS_VERSION" @@ -77,7 +81,8 @@ runs: --dev-branch "${{ inputs.dev-branch }}" \ --minor-identifier="${{ inputs.minor-identifier }}" \ --major-identifier="${{ inputs.major-identifier }}" \ - --version-prefix "${{ inputs.prefix }}") + --version-prefix "${{ inputs.prefix }}") \ + --version-suffix "${{ inputs.suffix }}") echo "version=$VERSION" >> $GITHUB_OUTPUT echo "New Version: $VERSION" diff --git a/spec/git-version-spec.cr b/spec/git-version-spec.cr index 3ec3e7f..5a52d72 100644 --- a/spec/git-version-spec.cr +++ b/spec/git-version-spec.cr @@ -547,6 +547,120 @@ describe GitVersion do end end + it "should properly manage suffixes" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 1") + tmp.exec %(git tag "1.1.0-backend") + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2") + + version = git.get_new_version + version.should eq("1.1.1-backend") + ensure + tmp.cleanup + end + end + + it "non-suffixed tags should be ignored if suffix is enabled" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "1.0.0") + + version = git.get_new_version + version.should eq("0.0.1-backend") + ensure + tmp.cleanup + end + end + + it "should properly manage a tag with only suffix" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "-backend") + + version = git.get_new_version + version.should eq("0.0.1-backend") + ensure + tmp.cleanup + end + end + + it "should properly manage combination of prefixes and suffixes" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 1") + tmp.exec %(git tag "v1.1.0-backend") + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2") + + version = git.get_new_version + version.should eq("v1.1.1-backend") + ensure + tmp.cleanup + end + end + + it "non-prefixed or suffixed tags should be ignored if prefix and suffix are enabled" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "1.0.0") + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "v1.0.0") + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "1.0.0-backend") + + version = git.get_new_version + version.should eq("v0.0.1-backend") + ensure + tmp.cleanup + end + end + + it "should properly manage a tag with only prefix and suffix" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "v-backend") + + version = git.get_new_version + version.should eq("v0.0.1-backend") + ensure + tmp.cleanup + end + end + it "should count the commits distance" do tmp = InTmp.new diff --git a/src/git-version.cr b/src/git-version.cr index d49959b..b5d0cb4 100644 --- a/src/git-version.cr +++ b/src/git-version.cr @@ -12,7 +12,7 @@ module GitVersion class Git def initialize(@dev_branch : String, @release_branch : String, @minor_identifier : String, @major_identifier : String, - @folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "") + @folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "", @suffix : String = "") @major_id_is_regex = false @minor_id_is_regex = false if match = /\/(.*)\//.match(@major_identifier) @@ -26,12 +26,12 @@ module GitVersion # end - private def add_prefix(version : String) : String - return "#{@prefix}#{version}" + private def add_prefix_suffix(version : String) : String + return "#{@prefix}#{version}#{@suffix}" end - private def strip_prefix(version : String) : String | Nil - version.lchop?(@prefix) + private def strip_prefix_suffix(version : String) : String | Nil + version.lchop?(@prefix).try &.rchop?(@suffix) end private def exec(cmd) @@ -98,7 +98,7 @@ module GitVersion return [] of String end - def get_previous_tag_and_version: Tuple(String | Nil, SemanticVersion) + def get_previous_tag_and_version : Tuple(String | Nil, SemanticVersion) cb = current_branch_or_tag branch_tags = tags_by_branch(cb) @@ -108,11 +108,11 @@ module GitVersion branch_tags.each do |tag| begin - tag_without_prefix = strip_prefix(tag) - if tag_without_prefix.nil? + tag_without_prefix_suffix = strip_prefix_suffix(tag) + if tag_without_prefix_suffix.nil? next end - current_version = SemanticVersion.parse(tag_without_prefix) + current_version = SemanticVersion.parse(tag_without_prefix_suffix) if !current_version.prerelease.identifiers.empty? next elsif (previous_version < current_version) @@ -128,7 +128,7 @@ module GitVersion def get_previous_version: String lt, lv = get_previous_tag_and_version - return lt ? lt : add_prefix(lv.to_s) + return lt ? lt : add_prefix_suffix(lv.to_s) end def get_new_version @@ -214,7 +214,7 @@ module GitVersion ) end - return add_prefix(previous_version.to_s) + return add_prefix_suffix(previous_version.to_s) end end end diff --git a/src/main.cr b/src/main.cr index 6de97d0..1ee645f 100644 --- a/src/main.cr +++ b/src/main.cr @@ -24,6 +24,7 @@ OptionParser.parse do |parser| parser.on("--major-identifier=IDENTIFIER", "Specifies the string or regex to identify a major release commit with") { |identifier| major_identifier = identifier } parser.on("-p PREFIX", "--version-prefix=PREFIX", "Specifies a version prefix") { |p| prefix = p } + parser.on("-s SUFFIX", "--version-suffix=SUFFIX", "Specifies a version suffix") { |s| suffix = s } parser.on("-l PATH", "--log-paths=PATH", "") { |path| log_paths = path } parser.on("--previous-version", "Returns the previous tag instead of calculating a new one") { previous_version=true } parser.on("-h", "--help", "Show this help") { puts parser } @@ -34,7 +35,7 @@ OptionParser.parse do |parser| end end -git = GitVersion::Git.new(dev_branch, release_branch, minor_identifier, major_identifier, folder, prefix, log_paths) +git = GitVersion::Git.new(dev_branch, release_branch, minor_identifier, major_identifier, folder, prefix, log_paths, suffix) if previous_version puts "#{git.get_previous_version}" From 5936174472ee63c23cbcf2c33c26c895571e7929 Mon Sep 17 00:00:00 2001 From: Jackie Weng Date: Wed, 20 Nov 2024 23:23:15 +1300 Subject: [PATCH 2/4] fix: missing variable declaration --- src/git-version.cr | 2 +- src/main.cr | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/git-version.cr b/src/git-version.cr index b5d0cb4..8829e44 100644 --- a/src/git-version.cr +++ b/src/git-version.cr @@ -126,7 +126,7 @@ module GitVersion return {previous_tag, previous_version} end - def get_previous_version: String + def get_previous_version : String lt, lv = get_previous_tag_and_version return lt ? lt : add_prefix_suffix(lv.to_s) end diff --git a/src/main.cr b/src/main.cr index 1ee645f..f2e9293 100644 --- a/src/main.cr +++ b/src/main.cr @@ -10,6 +10,7 @@ release_branch = "master" minor_identifier = "feature:" major_identifier = "breaking:" prefix = "" +suffix = "" log_paths = "" folder = FileUtils.pwd From 8514d35c9de57d12e28d6fc13353af7a7a51b071 Mon Sep 17 00:00:00 2001 From: Jackie Weng Date: Thu, 21 Nov 2024 08:59:06 +1300 Subject: [PATCH 3/4] fix: command syntax error --- action.yml | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/action.yml b/action.yml index b40095a..d93d5d6 100644 --- a/action.yml +++ b/action.yml @@ -1,46 +1,46 @@ name: Git Version author: "Codacy" -description: 'Semver versioning based on the git history and commit messages of your repository.' +description: "Semver versioning based on the git history and commit messages of your repository." branding: - icon: 'git-branch' - color: 'gray-dark' + icon: "git-branch" + color: "gray-dark" inputs: tool-version: - description: 'The version of the tool to be ran' + description: "The version of the tool to be ran" required: true default: latest release-branch: - description: 'The name of the release branch' + description: "The name of the release branch" required: true default: master dev-branch: - description: 'The name of the dev branch' + description: "The name of the dev branch" required: true default: dev minor-identifier: - description: 'The string or regex to identify a minor release commit' + description: "The string or regex to identify a minor release commit" required: true - default: 'feature:' + default: "feature:" major-identifier: - description: 'The string or regex to identify a major release commit' + description: "The string or regex to identify a major release commit" required: true - default: 'breaking:' + default: "breaking:" prefix: - description: 'The prefix to use in the version' + description: "The prefix to use in the version" required: false suffix: description: "The suffix to use in the version" required: false log-paths: - description: 'The paths to be used to calculate changes (comma-separated)' + description: "The paths to be used to calculate changes (comma-separated)" required: false default: ./ outputs: version: - description: 'The value of the new pre-calculated tag' + description: "The value of the new pre-calculated tag" value: ${{ steps.version.outputs.version }} previous-version: - description: 'Contains the value of previous tag, before calculating a new one' + description: "Contains the value of previous tag, before calculating a new one" value: ${{ steps.previous-version.outputs.previous-version }} runs: using: "composite" @@ -66,7 +66,7 @@ runs: --dev-branch "${{ inputs.dev-branch }}" \ --minor-identifier="${{ inputs.minor-identifier }}" \ --major-identifier="${{ inputs.major-identifier }}" \ - --version-prefix "${{ inputs.prefix }}") \ + --version-prefix "${{ inputs.prefix }}" \ --version-suffix "${{ inputs.suffix }}") echo "previous-version=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT @@ -81,7 +81,7 @@ runs: --dev-branch "${{ inputs.dev-branch }}" \ --minor-identifier="${{ inputs.minor-identifier }}" \ --major-identifier="${{ inputs.major-identifier }}" \ - --version-prefix "${{ inputs.prefix }}") \ + --version-prefix "${{ inputs.prefix }}" \ --version-suffix "${{ inputs.suffix }}") echo "version=$VERSION" >> $GITHUB_OUTPUT From 3d280b9955d28c06082cc3f1d11ccb379639937b Mon Sep 17 00:00:00 2001 From: Jackie Weng Date: Thu, 21 Nov 2024 09:00:46 +1300 Subject: [PATCH 4/4] chore: revert unnecessary quote changes --- action.yml | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/action.yml b/action.yml index d93d5d6..d22f82e 100644 --- a/action.yml +++ b/action.yml @@ -1,46 +1,46 @@ name: Git Version author: "Codacy" -description: "Semver versioning based on the git history and commit messages of your repository." +description: 'Semver versioning based on the git history and commit messages of your repository.' branding: - icon: "git-branch" - color: "gray-dark" + icon: 'git-branch' + color: 'gray-dark' inputs: tool-version: - description: "The version of the tool to be ran" + description: 'The version of the tool to be ran' required: true default: latest release-branch: - description: "The name of the release branch" + description: 'The name of the release branch' required: true default: master dev-branch: - description: "The name of the dev branch" + description: 'The name of the dev branch' required: true default: dev minor-identifier: - description: "The string or regex to identify a minor release commit" + description: 'The string or regex to identify a minor release commit' required: true - default: "feature:" + default: 'feature:' major-identifier: - description: "The string or regex to identify a major release commit" + description: 'The string or regex to identify a major release commit' required: true - default: "breaking:" + default: 'breaking:' prefix: - description: "The prefix to use in the version" + description: 'The prefix to use in the version' required: false suffix: - description: "The suffix to use in the version" + description: 'The suffix to use in the version' required: false log-paths: - description: "The paths to be used to calculate changes (comma-separated)" + description: 'The paths to be used to calculate changes (comma-separated)' required: false default: ./ outputs: version: - description: "The value of the new pre-calculated tag" + description: 'The value of the new pre-calculated tag' value: ${{ steps.version.outputs.version }} previous-version: - description: "Contains the value of previous tag, before calculating a new one" + description: 'Contains the value of previous tag, before calculating a new one' value: ${{ steps.previous-version.outputs.previous-version }} runs: using: "composite"