Commit ce21d8688d091312f47c547c2900a83fdcaebc75

Authored by Izaak Alpert
1 parent e8d1e827

feature API call to download repo archive

defaults to HEAD

Conflicts:
	spec/requests/api/repositories_spec.rb

Change-Id: Id114aca6c59d75f18e49bf9f33809a04e010bfb6
doc/api/repositories.md
... ... @@ -356,3 +356,16 @@ Parameters:
356 356 + `id` (required) - The ID of a project
357 357 + `sha` (required) - The commit or branch name
358 358 + `filepath` (required) - The path the file
  359 +
  360 +
  361 +## Get file archive
  362 +
  363 +Get a an archive of the repository
  364 +
  365 +```
  366 +GET /projects/:id/repository/archive
  367 +```
  368 +
  369 +Parameters:
  370 ++ `id` (required) - The ID of a project
  371 ++ `sha` (optional) - The commit or branch name
... ...
lib/api/repositories.rb
... ... @@ -144,7 +144,7 @@ module API
144 144 trees = []
145 145  
146 146 %w(trees blobs submodules).each do |type|
147   - trees += tree.send(type).map { |t| { name: t.name, type: type.singularize, mode: t.mode, id: t.id } }
  147 + trees += tree.send(type).map { |t| {name: t.name, type: type.singularize, mode: t.mode, id: t.id} }
148 148 end
149 149  
150 150 trees
... ... @@ -176,6 +176,30 @@ module API
176 176 content_type blob.mime_type
177 177 present blob.data
178 178 end
  179 +
  180 + # Get a an archive of the repository
  181 + #
  182 + # Parameters:
  183 + # id (required) - The ID of a project
  184 + # sha (optional) - the commit sha to download defaults to head
  185 + # Example Request:
  186 + # GET /projects/:id/repository/archive
  187 + get ":id/repository/archive" do
  188 + authorize! :download_code, user_project
  189 + repo = user_project.repository
  190 + ref = params[:sha]
  191 + storage_path = Rails.root.join("tmp", "repositories")
  192 +
  193 + file_path = repo.archive_repo(ref || 'HEAD', storage_path)
  194 + if file_path
  195 + data = File.open(file_path).read
  196 + content_type 'application/x-gzip'
  197 + header "Content-Disposition:"," infile; filename=\"#{File.basename(file_path)}\""
  198 + present data
  199 + else
  200 + not_found!
  201 + end
  202 + end
179 203 end
180 204 end
181 205 end
... ...
spec/requests/api/repositories_spec.rb
... ... @@ -225,4 +225,15 @@ describe API::API do
225 225 end
226 226 end
227 227  
  228 + describe "GET /projects/:id/repository/archive/:sha" do
  229 + it "should get the archive" do
  230 + get api("/projects/#{project.id}/repository/archive", user)
  231 + response.status.should == 200
  232 + end
  233 +
  234 + it "should return 404 for invalid sha" do
  235 + get api("/projects/#{project.id}/repository/archive/?sha=xxx", user)
  236 + response.status.should == 404
  237 + end
  238 + end
228 239 end
... ...