diff --git a/app/finders/repositories/changelog_tag_finder.rb b/app/finders/repositories/changelog_tag_finder.rb index d621550314ef0f70b8c375031a2665c8500c88a6..ee48b54d9162011cc8d9ede2c89a1eafe1920cd0 100644 --- a/app/finders/repositories/changelog_tag_finder.rb +++ b/app/finders/repositories/changelog_tag_finder.rb @@ -35,13 +35,18 @@ def execute(new_version) tags = {} # Custom regex matcher extracts versions from repository tags + # This format is defined by the user and applied to repository tags only + # https://docs.gitlab.com/ee/user/project/changelogs.html#customize-the-tag-format-when-extracting-versions custom_regex_matcher = matcher(@regex) if Feature.enabled?(:update_changelog_logic, @project) # Default regex macher extracts the user provided version + # The regex is different here, because it must match API documentation requirements + # https://gitlab.com/gitlab-org/gitlab/-/blob/44ab4e5bccdea01642b2f42bcccef706409ebfec/doc/api/repositories.md#L338 default_regex_matcher = matcher(Gitlab::Changelog::Config::DEFAULT_TAG_REGEX) - requested_version = extract_version(new_version, default_regex_matcher) + version_components = default_regex_matcher.match(new_version) + requested_version = assemble_version(version_components) unless requested_version raise Gitlab::Changelog::Error, @@ -54,7 +59,16 @@ def execute(new_version) versions = [requested_version] @project.repository.tags.each do |tag| - version = extract_version(tag.name, custom_regex_matcher) + version_components = custom_regex_matcher.match(tag.name) + + next unless version_components + + # 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 + next if version_components[:pre] + + version = assemble_version(version_components) next unless version @@ -84,15 +98,9 @@ def matcher(regex) ) end - def extract_version(string, version_matcher) - 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] + # Builds a version string based on regex matcher's output + def assemble_version(matches) + return if matches.blank? major = matches[:major] minor = matches[:minor] diff --git a/spec/finders/repositories/changelog_tag_finder_spec.rb b/spec/finders/repositories/changelog_tag_finder_spec.rb index ea2a5fdee5f7b5dd13d8daceaa550b5c55ad55d8..e7c065323108a4dfae8c59dfd7e025a331764bb7 100644 --- a/spec/finders/repositories/changelog_tag_finder_spec.rb +++ b/spec/finders/repositories/changelog_tag_finder_spec.rb @@ -39,6 +39,9 @@ expect(finder.execute('0.9.0')).to eq(tag6) expect(finder.execute('0.6.0')).to eq(tag7) + # with a pre-release version + expect(finder.execute('0.6.0-rc1')).to eq(tag7) + # with v at the beginning expect(finder.execute('v2.1.0')).to eq(tag3) expect { finder.execute('wrong_version') }.to raise_error(Gitlab::Changelog::Error) @@ -70,6 +73,9 @@ expect(finder.execute('0.9.0')).to eq(tag6) expect(finder.execute('0.6.0')).to eq(tag7) + # with a pre-release version + expect(finder.execute('0.6.0-rc1')).to eq(tag7) + # with v at the beginning expect(finder.execute('v2.1.0')).to eq(nil) expect(finder.execute('wrong_version')).to eq(nil)