Commit 27912f9c21c3dfb20f8d1d037f8c085281de5b0e

Authored by Thom Gerdes
1 parent b27bd2b1

Add api support for raw blob search

See issue
http://feedback.gitlab.com/forums/176466-general/suggestions/4566001-expose-raw-object-search-via-the-gitlab-api
doc/api/repositories.md
... ... @@ -343,9 +343,9 @@ Parameters:
343 343 ```
344 344  
345 345  
346   -## Raw blob content
  346 +## Raw file content
347 347  
348   -Get the raw file contents for a file.
  348 +Get the raw file contents for a file by commit sha and path.
349 349  
350 350 ```
351 351 GET /projects/:id/repository/blobs/:sha
... ... @@ -358,6 +358,20 @@ Parameters:
358 358 + `filepath` (required) - The path the file
359 359  
360 360  
  361 +## Raw blob content
  362 +
  363 +Get the raw file contents for a blob by blob sha.
  364 +
  365 +```
  366 +GET /projects/:id/repository/raw_blobs/:sha
  367 +```
  368 +
  369 +Parameters:
  370 +
  371 ++ `id` (required) - The ID of a project
  372 ++ `sha` (required) - The blob sha
  373 +
  374 +
361 375 ## Get file archive
362 376  
363 377 Get a an archive of the repository
... ...
lib/api/repositories.rb
... ... @@ -177,6 +177,28 @@ module API
177 177 present blob.data
178 178 end
179 179  
  180 + # Get a raw blob contents by blob sha
  181 + #
  182 + # Parameters:
  183 + # id (required) - The ID of a project
  184 + # sha (required) - The blob's sha
  185 + # Example Request:
  186 + # GET /projects/:id/repository/raw_blobs/:sha
  187 + get ":id/repository/raw_blobs/:sha" do
  188 + ref = params[:sha]
  189 +
  190 + repo = user_project.repository
  191 +
  192 + blob = Gitlab::Git::Blob.raw(repo, ref)
  193 +
  194 + not_found! "Blob" unless blob
  195 +
  196 + env['api.format'] = :txt
  197 +
  198 + content_type blob.mime_type
  199 + present blob.data
  200 + end
  201 +
180 202 # Get a an archive of the repository
181 203 #
182 204 # Parameters:
... ...
spec/requests/api/repositories_spec.rb
... ... @@ -225,6 +225,13 @@ describe API::API do
225 225 end
226 226 end
227 227  
  228 + describe "GET /projects/:id/repository/raw_blobs/:sha" do
  229 + it "should get the raw file contents" do
  230 + get api("/projects/#{project.id}/repository/raw_blobs/d1aff2896d99d7acc4d9780fbb716b113c45ecf7", user)
  231 + response.status.should == 200
  232 + end
  233 + end
  234 +
228 235 describe "GET /projects/:id/repository/archive/:sha" do
229 236 it "should get the archive" do
230 237 get api("/projects/#{project.id}/repository/archive", user)
... ...