Add option to suppress archive commit sha

Repository archives are always named `<project>-<ref>-<sha>` even if
the ref is a commit. A consequence of always including the sha even
for tags is that packaging a release is more difficult because both
the ref and sha must be known by the packager.

- add append_sha option (defaults true) to provide a method for
toggling this feature.

Support added to GitLab Workhorse by gitlab-org/gitlab-workhorse!232
This commit is contained in:
James Ramsay 2018-02-19 15:41:04 -05:00
parent eaed588bf2
commit 0b1b9c409d
7 changed files with 27 additions and 14 deletions

View file

@ -24,8 +24,8 @@ module WorkhorseHelper
end end
# Archive a Git repository and send it through Workhorse # Archive a Git repository and send it through Workhorse
def send_git_archive(repository, ref:, format:) def send_git_archive(repository, **kwargs)
headers.store(*Gitlab::Workhorse.send_git_archive(repository, ref: ref, format: format)) headers.store(*Gitlab::Workhorse.send_git_archive(repository, **kwargs))
head :ok head :ok
end end

View file

@ -489,8 +489,8 @@ module API
header(*Gitlab::Workhorse.send_git_blob(repository, blob)) header(*Gitlab::Workhorse.send_git_blob(repository, blob))
end end
def send_git_archive(repository, ref:, format:) def send_git_archive(repository, **kwargs)
header(*Gitlab::Workhorse.send_git_archive(repository, ref: ref, format: format)) header(*Gitlab::Workhorse.send_git_archive(repository, **kwargs))
end end
def send_artifacts_entry(build, entry) def send_artifacts_entry(build, entry)

View file

@ -88,7 +88,7 @@ module API
end end
get ':id/repository/archive', requirements: { format: Gitlab::PathRegex.archive_formats_regex } do get ':id/repository/archive', requirements: { format: Gitlab::PathRegex.archive_formats_regex } do
begin begin
send_git_archive user_project.repository, ref: params[:sha], format: params[:format] send_git_archive user_project.repository, ref: params[:sha], format: params[:format], append_sha: true
rescue rescue
not_found!('File') not_found!('File')
end end

View file

@ -75,7 +75,7 @@ module API
end end
get ':id/repository/archive', requirements: { format: Gitlab::PathRegex.archive_formats_regex } do get ':id/repository/archive', requirements: { format: Gitlab::PathRegex.archive_formats_regex } do
begin begin
send_git_archive user_project.repository, ref: params[:sha], format: params[:format] send_git_archive user_project.repository, ref: params[:sha], format: params[:format], append_sha: true
rescue rescue
not_found!('File') not_found!('File')
end end

View file

@ -394,17 +394,24 @@ module Gitlab
nil nil
end end
def archive_prefix(ref, sha) def archive_prefix(ref, sha, append_sha:)
append_sha = (ref != sha) if append_sha.nil?
project_name = self.name.chomp('.git') project_name = self.name.chomp('.git')
"#{project_name}-#{ref.tr('/', '-')}-#{sha}" formatted_ref = ref.tr('/', '-')
prefix_segments = [project_name, formatted_ref]
prefix_segments << sha if append_sha
prefix_segments.join('-')
end end
def archive_metadata(ref, storage_path, format = "tar.gz") def archive_metadata(ref, storage_path, format = "tar.gz", append_sha: true)
ref ||= root_ref ref ||= root_ref
commit = Gitlab::Git::Commit.find(self, ref) commit = Gitlab::Git::Commit.find(self, ref)
return {} if commit.nil? return {} if commit.nil?
prefix = archive_prefix(ref, commit.id) prefix = archive_prefix(ref, commit.id, append_sha: append_sha)
{ {
'RepoPath' => path, 'RepoPath' => path,

View file

@ -63,10 +63,10 @@ module Gitlab
] ]
end end
def send_git_archive(repository, ref:, format:) def send_git_archive(repository, ref:, format:, append_sha: true)
format ||= 'tar.gz' format ||= 'tar.gz'
format.downcase! format.downcase!
params = repository.archive_metadata(ref, Gitlab.config.gitlab.repository_downloads_path, format) params = repository.archive_metadata(ref, Gitlab.config.gitlab.repository_downloads_path, format, append_sha)
raise "Repository or ref not found" if params.empty? raise "Repository or ref not found" if params.empty?
if Gitlab::GitalyClient.feature_enabled?(:workhorse_archive, status: Gitlab::GitalyClient::MigrationStatus::OPT_OUT) if Gitlab::GitalyClient.feature_enabled?(:workhorse_archive, status: Gitlab::GitalyClient::MigrationStatus::OPT_OUT)

View file

@ -247,16 +247,22 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
it 'returns parameterised string for a ref containing slashes' do it 'returns parameterised string for a ref containing slashes' do
prefix = repository.archive_prefix('test/branch', 'SHA') prefix = repository.archive_prefix('test/branch', 'SHA', append_sha: true)
expect(prefix).to eq("#{project_name}-test-branch-SHA") expect(prefix).to eq("#{project_name}-test-branch-SHA")
end end
it 'returns correct string for a ref containing dots' do it 'returns correct string for a ref containing dots' do
prefix = repository.archive_prefix('test.branch', 'SHA') prefix = repository.archive_prefix('test.branch', 'SHA', append_sha: true)
expect(prefix).to eq("#{project_name}-test.branch-SHA") expect(prefix).to eq("#{project_name}-test.branch-SHA")
end end
it 'returns string with sha when append_sha is false' do
prefix = repository.archive_prefix('test.branch', 'SHA', append_sha: false)
expect(prefix).to eq("#{project_name}-test.branch")
end
end end
describe '#archive' do describe '#archive' do