Commit 4123c76ec192f08be4d74f4958624c6e87041564
Exists in
master
and in
4 other branches
Merge pull request #5253 from karlhungus/feature-api-download-archive
Feature api download archive
Showing
3 changed files
with
54 additions
and
1 deletions
Show diff stats
doc/api/repositories.md
| @@ -356,3 +356,16 @@ Parameters: | @@ -356,3 +356,16 @@ Parameters: | ||
| 356 | + `id` (required) - The ID of a project | 356 | + `id` (required) - The ID of a project |
| 357 | + `sha` (required) - The commit or branch name | 357 | + `sha` (required) - The commit or branch name |
| 358 | + `filepath` (required) - The path the file | 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 sha to download defaults to the tip of the default branch | ||
| 359 | \ No newline at end of file | 372 | \ No newline at end of file |
lib/api/repositories.rb
| @@ -144,7 +144,7 @@ module API | @@ -144,7 +144,7 @@ module API | ||
| 144 | trees = [] | 144 | trees = [] |
| 145 | 145 | ||
| 146 | %w(trees blobs submodules).each do |type| | 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 | end | 148 | end |
| 149 | 149 | ||
| 150 | trees | 150 | trees |
| @@ -176,6 +176,34 @@ module API | @@ -176,6 +176,34 @@ module API | ||
| 176 | content_type blob.mime_type | 176 | content_type blob.mime_type |
| 177 | present blob.data | 177 | present blob.data |
| 178 | end | 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 the tip of the default branch | ||
| 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, storage_path) | ||
| 194 | + if file_path && File.exists?(file_path) | ||
| 195 | + data = File.open(file_path, 'rb').read | ||
| 196 | + | ||
| 197 | + header "Content-Disposition:", " infile; filename=\"#{File.basename(file_path)}\"" | ||
| 198 | + content_type 'application/x-gzip' | ||
| 199 | + | ||
| 200 | + env['api.format'] = :binary | ||
| 201 | + | ||
| 202 | + present data | ||
| 203 | + else | ||
| 204 | + not_found! | ||
| 205 | + end | ||
| 206 | + end | ||
| 179 | end | 207 | end |
| 180 | end | 208 | end |
| 181 | end | 209 | end |
spec/requests/api/repositories_spec.rb
| @@ -225,4 +225,16 @@ describe API::API do | @@ -225,4 +225,16 @@ describe API::API do | ||
| 225 | end | 225 | end |
| 226 | end | 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 | + response.content_type.should == 'application/x-gzip' | ||
| 233 | + end | ||
| 234 | + | ||
| 235 | + it "should return 404 for invalid sha" do | ||
| 236 | + get api("/projects/#{project.id}/repository/archive/?sha=xxx", user) | ||
| 237 | + response.status.should == 404 | ||
| 238 | + end | ||
| 239 | + end | ||
| 228 | end | 240 | end |