Arun Arun - 22 days ago 6
Ruby Question

GitLab archive over API creating endless zips inside zips

I am using the GitLab API to create repository archives and when I do it creates a zip file that has a zip inside, that has a zip inside, etc.

Here is the code that is making the request:

def self.get_archive(project_id, branch)
request = Typhoeus::Request.new(
BASE_URL + "/projects/#{project_id}/repository/archive?sha=#{branch}",
method: :get,
params: nil,
headers: {
"PRIVATE-TOKEN": ADMIN_TOKEN
}
)

request.run

response = request.response

if response.success?
file_name = "archive-#{SecureRandom.hex(5)}.zip"

File.open(Rails.root + "public/archives/" + file_name, "wb") do |file|
file.write(response.body)
end

return {
success: true,
file_path: "/archives/" + file_name
}
else
return {
success: false,
response_raw: response
}
end
end


Any thoughts as to what's gone wrong? My thinking here was to take the binary that I get back and write it to a zip file.

Answer

So it turns out that GitLab sends back by default a tar.gz file. To receive the Zip file instead you have to explicitly specify the .zip extension to the end of the request. Like so:

"/projects/#{project_id}/repository/archive.zip?sha=#{branch}"

The .zip at the end of "archive" was the important factor.