Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ _None_

### New Features

_None_
- Added optional `exclude` parameter to `find_previous_tag` action, mapping to git's `--exclude` flag. This allows skipping beta/pre-release tags when searching for the previous stable release tag [#696]

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Actions
class FindPreviousTagAction < Action
def self.run(params)
tag_pattern = params[:pattern]
exclude_pattern = params[:exclude]

# Make sure we have all the latest tags fetched locally
Actions.sh('git', 'fetch', '--tags', '--force') { nil }
Expand All @@ -17,6 +18,7 @@ def self.run(params)
# Finally find the previous tag matching the provided pattern, and that is not the current commit
git_cmd = %w[git describe --tags --abbrev=0]
git_cmd += ['--match', tag_pattern] unless tag_pattern.nil?
git_cmd += ['--exclude', exclude_pattern] unless exclude_pattern.nil?
git_cmd += ['--exclude', current_commit_tag] unless current_commit_tag.empty?
Actions.sh(*git_cmd) { |exit_status, stdout, _| exit_status.success? ? stdout.chomp : nil }
end
Expand All @@ -38,7 +40,8 @@ def self.details
Uses `git describe --tags --abbrev=0 --match … --exclude …` to find the previous git tag
reachable from the current commit and that matches a specific naming pattern

e.g. `find_previous_tag(pattern: '12.3.*.*')`, `find_previous_tag(pattern: '12.3-rc-*')`
e.g. `find_previous_tag(pattern: '12.3.*.*')`, `find_previous_tag(pattern: '12.3-rc-*')`,
`find_previous_tag(pattern: 'v*', exclude: '*beta*')`
DETAILS
end

Expand All @@ -49,6 +52,11 @@ def self.available_options
optional: true,
default_value: nil,
type: String),
FastlaneCore::ConfigItem.new(key: :exclude,
description: 'An _fnmatch_-style pattern of tags to exclude from the search (maps to `git describe --exclude`)',
optional: true,
default_value: nil,
type: String),
]
end

Expand Down
32 changes: 32 additions & 0 deletions spec/find_previous_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,38 @@ def stub_main_command(expected_command, stdout:, success: true)
expect(tag).to eq('12.2')
end

it 'excludes tags matching the exclude pattern' do
# Arrange
stub_current_commit_tag(nil)
stub_main_command(
%w[git describe --tags --abbrev=0 --match v* --exclude *beta*],
stdout: 'v1.7.3'
)
# Act
tag = run_described_fastlane_action(
pattern: 'v*',
exclude: '*beta*'
)
# Assert
expect(tag).to eq('v1.7.3')
end

it 'excludes both the exclude pattern and the current commit tag' do
# Arrange
stub_current_commit_tag('v1.8.0')
stub_main_command(
%w[git describe --tags --abbrev=0 --match v* --exclude *beta* --exclude v1.8.0],
stdout: 'v1.7.3'
)
# Act
tag = run_described_fastlane_action(
pattern: 'v*',
exclude: '*beta*'
)
# Assert
expect(tag).to eq('v1.7.3')
end

it 'returns nil if no previous commit could be found' do
# Arrange
stub_current_commit_tag(nil)
Expand Down