Skip to content

Commit 0a4a20f

Browse files
authored
Add exclude parameter to find_previous_tag action (#696)
2 parents e3a6159 + 4121687 commit 0a4a20f

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ _None_
1010

1111
### New Features
1212

13-
_None_
13+
- 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]
1414

1515
### Bug Fixes
1616

lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_previous_tag.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Actions
88
class FindPreviousTagAction < Action
99
def self.run(params)
1010
tag_pattern = params[:pattern]
11+
exclude_patterns = params[:exclude] || []
1112

1213
# Make sure we have all the latest tags fetched locally
1314
Actions.sh('git', 'fetch', '--tags', '--force') { nil }
@@ -17,6 +18,7 @@ def self.run(params)
1718
# Finally find the previous tag matching the provided pattern, and that is not the current commit
1819
git_cmd = %w[git describe --tags --abbrev=0]
1920
git_cmd += ['--match', tag_pattern] unless tag_pattern.nil?
21+
exclude_patterns.each { |p| git_cmd += ['--exclude', p] }
2022
git_cmd += ['--exclude', current_commit_tag] unless current_commit_tag.empty?
2123
Actions.sh(*git_cmd) { |exit_status, stdout, _| exit_status.success? ? stdout.chomp : nil }
2224
end
@@ -38,7 +40,9 @@ def self.details
3840
Uses `git describe --tags --abbrev=0 --match … --exclude …` to find the previous git tag
3941
reachable from the current commit and that matches a specific naming pattern
4042
41-
e.g. `find_previous_tag(pattern: '12.3.*.*')`, `find_previous_tag(pattern: '12.3-rc-*')`
43+
e.g. `find_previous_tag(pattern: '12.3.*.*')`, `find_previous_tag(pattern: '12.3-rc-*')`,
44+
`find_previous_tag(pattern: 'v*', exclude: ['*beta*'])`,
45+
`find_previous_tag(pattern: 'v*', exclude: ['*alpha*', '*beta*'])`
4246
DETAILS
4347
end
4448

@@ -49,6 +53,11 @@ def self.available_options
4953
optional: true,
5054
default_value: nil,
5155
type: String),
56+
FastlaneCore::ConfigItem.new(key: :exclude,
57+
description: 'An array of _fnmatch_-style patterns of tags to exclude from the search (maps to `git describe --exclude`)',
58+
optional: true,
59+
default_value: nil,
60+
type: Array),
5261
]
5362
end
5463

spec/find_previous_tag_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,70 @@ def stub_main_command(expected_command, stdout:, success: true)
6565
expect(tag).to eq('12.2')
6666
end
6767

68+
it 'excludes tags matching the exclude pattern' do
69+
# Arrange
70+
stub_current_commit_tag(nil)
71+
stub_main_command(
72+
%w[git describe --tags --abbrev=0 --match v* --exclude *beta*],
73+
stdout: 'v1.7.3'
74+
)
75+
# Act
76+
tag = run_described_fastlane_action(
77+
pattern: 'v*',
78+
exclude: ['*beta*']
79+
)
80+
# Assert
81+
expect(tag).to eq('v1.7.3')
82+
end
83+
84+
it 'excludes both the exclude pattern and the current commit tag' do
85+
# Arrange
86+
stub_current_commit_tag('v1.8.0')
87+
stub_main_command(
88+
%w[git describe --tags --abbrev=0 --match v* --exclude *beta* --exclude v1.8.0],
89+
stdout: 'v1.7.3'
90+
)
91+
# Act
92+
tag = run_described_fastlane_action(
93+
pattern: 'v*',
94+
exclude: ['*beta*']
95+
)
96+
# Assert
97+
expect(tag).to eq('v1.7.3')
98+
end
99+
100+
it 'auto-converts a single exclude string into an array' do
101+
# Arrange
102+
stub_current_commit_tag(nil)
103+
stub_main_command(
104+
%w[git describe --tags --abbrev=0 --match v* --exclude *beta*],
105+
stdout: 'v1.7.3'
106+
)
107+
# Act — Fastlane's ConfigItem auto-converts a String to Array when type is Array
108+
tag = run_described_fastlane_action(
109+
pattern: 'v*',
110+
exclude: '*beta*'
111+
)
112+
# Assert
113+
expect(tag).to eq('v1.7.3')
114+
end
115+
116+
it 'excludes tags matching multiple exclude patterns' do
117+
# Arrange
118+
stub_current_commit_tag(nil)
119+
stub_main_command(
120+
%w[git describe --tags --abbrev=0 --match v* --exclude *alpha* --exclude *beta*],
121+
stdout: 'v1.7.3'
122+
)
123+
# Act
124+
tag = run_described_fastlane_action(
125+
pattern: 'v*',
126+
exclude: %w[*alpha* *beta*]
127+
)
128+
# Assert
129+
expect(tag).to eq('v1.7.3')
130+
end
131+
68132
it 'returns nil if no previous commit could be found' do
69133
# Arrange
70134
stub_current_commit_tag(nil)

0 commit comments

Comments
 (0)