Skip to content
代码片段 群组 项目
未验证 提交 dc862ac1 编辑于 作者: Vasilii Iakliushin's avatar Vasilii Iakliushin 提交者: GitLab
浏览文件

Merge branch '437616_fix_changelog_tag_detection' into 'master'

No related branches found
No related tags found
无相关合并请求
...@@ -33,40 +33,16 @@ def initialize(project, regex: Gitlab::Changelog::Config::DEFAULT_TAG_REGEX) ...@@ -33,40 +33,16 @@ def initialize(project, regex: Gitlab::Changelog::Config::DEFAULT_TAG_REGEX)
def execute(new_version) def execute(new_version)
tags = {} tags = {}
versions = [new_version]
begin
regex = Gitlab::UntrustedRegexp.new(@regex)
rescue RegexpError => e
# The error messages produced by default are not very helpful, so we
# raise a better one here. We raise the specific error here so its
# message is displayed in the API (where we catch this specific
# error).
raise(
Gitlab::Changelog::Error,
"The regular expression to use for finding the previous tag for a version is invalid: #{e.message}"
)
end
@project.repository.tags.each do |tag|
matches = regex.match(tag.name)
next unless matches
# When using this class for generating changelog data for a range of requested_version = extract_version(new_version)
# commits, we want to compare against the tag of the last _stable_ return unless requested_version
# release; not some random RC that came after that.
next if matches[:pre]
major = matches[:major] versions = [requested_version]
minor = matches[:minor]
patch = matches[:patch]
build = matches[:meta]
next unless major && minor && patch @project.repository.tags.each do |tag|
version = extract_version(tag.name)
version = "#{major}.#{minor}.#{patch}" next unless version
version += "+#{build}" if build
tags[version] = tag tags[version] = tag
versions << version versions << version
...@@ -74,9 +50,47 @@ def execute(new_version) ...@@ -74,9 +50,47 @@ def execute(new_version)
VersionSorter.sort!(versions) VersionSorter.sort!(versions)
index = versions.index(new_version) index = versions.index(requested_version)
tags[versions[index - 1]] if index&.positive? tags[versions[index - 1]] if index&.positive?
end end
private
def version_matcher
@version_matcher ||= Gitlab::UntrustedRegexp.new(@regex)
rescue RegexpError => e
# The error messages produced by default are not very helpful, so we
# raise a better one here. We raise the specific error here so its
# message is displayed in the API (where we catch this specific
# error).
raise(
Gitlab::Changelog::Error,
"The regular expression to use for finding the previous tag for a version is invalid: #{e.message}"
)
end
def extract_version(string)
matches = version_matcher.match(string)
return unless matches
# When using this class for generating changelog data for a range of
# commits, we want to compare against the tag of the last _stable_
# release; not some random RC that came after that.
return if matches[:pre]
major = matches[:major]
minor = matches[:minor]
patch = matches[:patch]
build = matches[:meta]
return unless major && minor && patch
version = "#{major}.#{minor}.#{patch}"
version += "+#{build}" if build
version
end
end end
end end
...@@ -366,6 +366,9 @@ If the `version` attribute is `2.1.0`, GitLab uses tag `v2.0.0`. And when the ...@@ -366,6 +366,9 @@ If the `version` attribute is `2.1.0`, GitLab uses tag `v2.0.0`. And when the
version is `1.1.1`, or `1.2.0`, GitLab uses tag v1.1.0. The tag `v1.0.0-pre1` is version is `1.1.1`, or `1.2.0`, GitLab uses tag v1.1.0. The tag `v1.0.0-pre1` is
never used, because pre-release tags are ignored. never used, because pre-release tags are ignored.
The `version` attribute can start with `v`. For example: `v1.0.0`.
The response is the same as for `version` value `1.0.0`. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/437616) in GitLab 16.9.
If `from` is unspecified and no tag to use is found, the API produces an error. If `from` is unspecified and no tag to use is found, the API produces an error.
To solve such an error, you must explicitly specify a value for the `from` To solve such an error, you must explicitly specify a value for the `from`
attribute. attribute.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Repositories::ChangelogTagFinder do RSpec.describe Repositories::ChangelogTagFinder, feature_category: :source_code_management do
let(:project) { build_stubbed(:project) } let(:project) { build_stubbed(:project) }
let(:finder) { described_class.new(project) } let(:finder) { described_class.new(project) }
...@@ -35,6 +35,10 @@ ...@@ -35,6 +35,10 @@
expect(finder.execute('1.0.0')).to eq(tag4) expect(finder.execute('1.0.0')).to eq(tag4)
expect(finder.execute('0.9.0')).to eq(tag6) expect(finder.execute('0.9.0')).to eq(tag6)
expect(finder.execute('0.6.0')).to eq(tag7) expect(finder.execute('0.6.0')).to eq(tag7)
# with v at the beginning
expect(finder.execute('v2.1.0')).to eq(tag3)
expect(finder.execute('wrong_version')).to eq(nil)
end end
end end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册