Skip to content

Commit 4be4819

Browse files
Merge pull request #106 from codacy/ls/add-custom-version
feat: add flag to skip prerelease creation
2 parents ac0f426 + 49f1324 commit 4be4819

File tree

5 files changed

+61
-26
lines changed

5 files changed

+61
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ You can configure the action with various inputs, a list of which has been provi
189189
| dev-branch | The name of the development branch | dev |
190190
| minor-identifier | The string used to identify a minor release (wrap with '/' to match using a regular expression) | feature: |
191191
| major-identifier | The string used to identify a major release (wrap with '/' to match using a regular expression) | breaking: |
192+
| skip-prerelease | When true, release-branch and dev-branch are effectively ignored | false |
192193
| prefix | The prefix used for the version name | |
193194
| log-paths | The paths used to calculate changes (comma-separated) | |
194195

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ inputs:
2525
description: 'The string or regex to identify a major release commit'
2626
required: true
2727
default: 'breaking:'
28+
skip-prerelease:
29+
description: 'Skip prerelease part of the version. When true, release-branch and dev-branch are effectively ignored'
30+
required: true
31+
default: 'false'
2832
prefix:
2933
description: 'The prefix to use in the version'
3034
required: false
@@ -63,6 +67,7 @@ runs:
6367
--dev-branch "${{ inputs.dev-branch }}" \
6468
--minor-identifier="${{ inputs.minor-identifier }}" \
6569
--major-identifier="${{ inputs.major-identifier }}" \
70+
${{ inputs.skip-prerelease == 'true' && '--skip-prerelease' || '' }} \
6671
--version-prefix "${{ inputs.prefix }}")
6772
6873
echo "previous-version=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT
@@ -77,6 +82,7 @@ runs:
7782
--dev-branch "${{ inputs.dev-branch }}" \
7883
--minor-identifier="${{ inputs.minor-identifier }}" \
7984
--major-identifier="${{ inputs.major-identifier }}" \
85+
${{ inputs.skip-prerelease == 'true' && '--skip-prerelease' || '' }} \
8086
--version-prefix "${{ inputs.prefix }}")
8187
8288
echo "version=$VERSION" >> $GITHUB_OUTPUT

spec/git-version-spec.cr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ describe GitVersion do
9898
end
9999
end
100100

101+
it "should skip prerelease component in the version number when configured" do
102+
tmp = InTmp.new
103+
104+
begin
105+
tmp.exec %(git init)
106+
tmp.exec %(git checkout -b master)
107+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
108+
tmp.exec %(git tag "1.0.0")
109+
110+
tmp.exec %(git checkout -b my-test.branch)
111+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2")
112+
113+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", true)
114+
115+
version = git.get_new_version
116+
117+
version.should eq("1.0.1")
118+
ensure
119+
tmp.cleanup
120+
end
121+
end
122+
101123
it "should properly bump the version" do
102124
tmp = InTmp.new
103125

src/git-version.cr

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module GitVersion
1212

1313
class Git
1414
def initialize(@dev_branch : String, @release_branch : String, @minor_identifier : String, @major_identifier : String,
15-
@folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "")
15+
@folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "", @skip_prerelease : Bool = false)
1616
@major_id_is_regex = false
1717
@minor_id_is_regex = false
1818
if match = /\/(.*)\//.match(@major_identifier)
@@ -195,32 +195,36 @@ module GitVersion
195195

196196
cb = current_branch_or_tag
197197

198-
if cb == @release_branch
199-
#
200-
elsif cb == @dev_branch
201-
prerelease = [DEV_BRANCH_SUFFIX, commits_distance(previous_tag), current_commit_hash()] of String | Int32
202-
previous_version =
203-
SemanticVersion.new(
204-
previous_version.major,
205-
previous_version.minor,
206-
previous_version.patch,
207-
SemanticVersion::Prerelease.new(prerelease),
208-
nil
209-
)
210-
else
211-
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")[0,30]
212-
prerelease = [branch_sanitized_name, commits_distance(previous_tag), current_commit_hash()] of String | Int32
213-
previous_version =
214-
SemanticVersion.new(
215-
previous_version.major,
216-
previous_version.minor,
217-
previous_version.patch,
218-
SemanticVersion::Prerelease.new(prerelease),
219-
nil
220-
)
221-
end
198+
if ! @skip_prerelease
199+
cb = current_branch_or_tag
222200

201+
if cb == @release_branch
202+
#
203+
elsif cb == @dev_branch
204+
prerelease = [DEV_BRANCH_SUFFIX, commits_distance(previous_tag), current_commit_hash()] of String | Int32
205+
previous_version =
206+
SemanticVersion.new(
207+
previous_version.major,
208+
previous_version.minor,
209+
previous_version.patch,
210+
SemanticVersion::Prerelease.new(prerelease),
211+
nil
212+
)
213+
else
214+
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")[0,30]
215+
prerelease = [branch_sanitized_name, commits_distance(previous_tag), current_commit_hash()] of String | Int32
216+
previous_version =
217+
SemanticVersion.new(
218+
previous_version.major,
219+
previous_version.minor,
220+
previous_version.patch,
221+
SemanticVersion::Prerelease.new(prerelease),
222+
nil
223+
)
224+
end
225+
end
223226
return add_prefix(previous_version.to_s)
224227
end
228+
225229
end
226230
end

src/main.cr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dev_branch = "dev"
99
release_branch = "master"
1010
minor_identifier = "feature:"
1111
major_identifier = "breaking:"
12+
skip_prerelease = false
1213
prefix = ""
1314
log_paths = ""
1415

@@ -23,6 +24,7 @@ OptionParser.parse do |parser|
2324
"Specifies the string or regex to identify a minor release commit with") { |identifier| minor_identifier = identifier }
2425
parser.on("--major-identifier=IDENTIFIER",
2526
"Specifies the string or regex to identify a major release commit with") { |identifier| major_identifier = identifier }
27+
parser.on("--skip-prerelease BRANCH", "Skip the prerelase part of the version") { skip_prerelease=true }
2628
parser.on("-p PREFIX", "--version-prefix=PREFIX", "Specifies a version prefix") { |p| prefix = p }
2729
parser.on("-l PATH", "--log-paths=PATH", "") { |path| log_paths = path }
2830
parser.on("--previous-version", "Returns the previous tag instead of calculating a new one") { previous_version=true }
@@ -34,7 +36,7 @@ OptionParser.parse do |parser|
3436
end
3537
end
3638

37-
git = GitVersion::Git.new(dev_branch, release_branch, minor_identifier, major_identifier, folder, prefix, log_paths)
39+
git = GitVersion::Git.new(dev_branch, release_branch, minor_identifier, major_identifier, folder, prefix, log_paths, skip_prerelease)
3840

3941
if previous_version
4042
puts "#{git.get_previous_version}"

0 commit comments

Comments
 (0)