Commit 5bc407b1 authored by Léopold Jacquot's avatar Léopold Jacquot Committed by Léopold Jacquot

Fix composer package version regex

Fix composer package name when published from a branch created from an issue.

Changelog: fixed
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/70251
parent 2fc42c6b
...@@ -12,18 +12,19 @@ module Packages ...@@ -12,18 +12,19 @@ module Packages
if @tag_name.present? if @tag_name.present?
@tag_name.delete_prefix('v') @tag_name.delete_prefix('v')
elsif @branch_name.present? elsif @branch_name.present?
branch_sufix_or_prefix(@branch_name.match(Gitlab::Regex.composer_package_version_regex)) branch_suffix_or_prefix(@branch_name.match(Gitlab::Regex.composer_package_version_regex))
end end
end end
private private
def branch_sufix_or_prefix(match) def branch_suffix_or_prefix(match)
if match if match
if match.captures[1] == '.x' captures = match.captures.reject(&:blank?)
match.captures[0] + '-dev' if captures[-1] == '.x'
captures[0] + '-dev'
else else
match.captures[0] + '.x-dev' captures[0] + '.x-dev'
end end
else else
"dev-#{@branch_name}" "dev-#{@branch_name}"
......
...@@ -21,7 +21,8 @@ module Gitlab ...@@ -21,7 +21,8 @@ module Gitlab
end end
def composer_package_version_regex def composer_package_version_regex
@composer_package_version_regex ||= %r{^v?(\d+(\.(\d+|x))*(-.+)?)}.freeze # see https://github.com/composer/semver/blob/31f3ea725711245195f62e54ffa402d8ef2fdba9/src/VersionParser.php#L215
@composer_package_version_regex ||= %r{\Av?((\d++)(\.(?:\d++|[xX*]))?(\.(?:\d++|[xX*]))?(\.(?:\d++|[xX*]))?)?\z}.freeze
end end
def composer_dev_version_regex def composer_dev_version_regex
......
...@@ -924,4 +924,25 @@ RSpec.describe Gitlab::Regex do ...@@ -924,4 +924,25 @@ RSpec.describe Gitlab::Regex do
it { is_expected.not_to match('/api/v4/groups/1234/packages/debian/dists/stable/Release.gpg') } it { is_expected.not_to match('/api/v4/groups/1234/packages/debian/dists/stable/Release.gpg') }
it { is_expected.not_to match('/api/v4/groups/1234/packages/debian/pool/compon/a/pkg/file.name') } it { is_expected.not_to match('/api/v4/groups/1234/packages/debian/pool/compon/a/pkg/file.name') }
end end
describe '.composer_package_version_regex' do
subject { described_class.composer_package_version_regex }
it { is_expected.to match('v1.2.3') }
it { is_expected.to match('v1.2.x') }
it { is_expected.to match('v1.2.X') }
it { is_expected.to match('1.2.3') }
it { is_expected.to match('1') }
it { is_expected.to match('v1') }
it { is_expected.to match('1.2') }
it { is_expected.to match('v1.2') }
it { is_expected.not_to match('1.2.3-beta') }
it { is_expected.not_to match('1.2.x-beta') }
it { is_expected.not_to match('1.2.X-beta') }
it { is_expected.not_to match('1.2.3-alpha.3') }
it { is_expected.not_to match('1./2.3') }
it { is_expected.not_to match('v1./2.3') }
it { is_expected.not_to match('../../../../../1.2.3') }
it { is_expected.not_to match('%2e%2e%2f1.2.3') }
end
end end
...@@ -12,6 +12,7 @@ RSpec.describe Packages::Composer::VersionParserService do ...@@ -12,6 +12,7 @@ RSpec.describe Packages::Composer::VersionParserService do
where(:tagname, :branchname, :expected_version) do where(:tagname, :branchname, :expected_version) do
nil | 'master' | 'dev-master' nil | 'master' | 'dev-master'
nil | 'my-feature' | 'dev-my-feature' nil | 'my-feature' | 'dev-my-feature'
nil | '12-feature' | 'dev-12-feature'
nil | 'v1' | '1.x-dev' nil | 'v1' | '1.x-dev'
nil | 'v1.x' | '1.x-dev' nil | 'v1.x' | '1.x-dev'
nil | 'v1.7.x' | '1.7.x-dev' nil | 'v1.7.x' | '1.7.x-dev'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment