Commit 69c4cea65a7ba13469f43914fbdccf592053fec8

Authored by Jeremy Anderson
1 parent e6edaa3b

updating project api to include raw blob contents and single branch

Gemfile
1 1 source "http://rubygems.org"
2 2  
3 3 gem "rails", "3.2.5"
4   -gem "rake", "0.8.7"
5 4  
6 5 # Supported DBs
7 6 gem "sqlite3"
... ... @@ -19,7 +18,7 @@ gem 'yaml_db', :git => "https://github.com/gitlabhq/yaml_db.git"
19 18 gem 'grack', :git => "https://github.com/gitlabhq/grack.git"
20 19 gem "linguist", "~> 1.0.0", :git => "https://github.com/gitlabhq/linguist.git"
21 20  
22   -gem "grape"
  21 +gem "grape", "~> 0.2.1"
23 22 gem "stamp"
24 23 gem "kaminari"
25 24 gem "haml-rails"
... ...
Gemfile.lock
... ... @@ -170,7 +170,7 @@ GEM
170 170 gherkin (2.11.0)
171 171 json (>= 1.4.6)
172 172 git (1.2.5)
173   - grape (0.2.0)
  173 + grape (0.2.1)
174 174 hashie (~> 1.2)
175 175 multi_json
176 176 multi_xml
... ... @@ -263,7 +263,7 @@ GEM
263 263 rdoc (~> 3.4)
264 264 thor (>= 0.14.6, < 2.0)
265 265 raindrops (0.9.0)
266   - rake (0.8.7)
  266 + rake (0.9.2.2)
267 267 raphael-rails (1.5.2)
268 268 rdoc (3.12)
269 269 json (~> 1.4)
... ... @@ -392,7 +392,7 @@ DEPENDENCIES
392 392 git
393 393 gitolite!
394 394 grack!
395   - grape
  395 + grape (~> 0.2.1)
396 396 grit!
397 397 haml-rails
398 398 httparty
... ... @@ -410,7 +410,6 @@ DEPENDENCIES
410 410 pygments.rb!
411 411 rails (= 3.2.5)
412 412 rails-footnotes
413   - rake (= 0.8.7)
414 413 raphael-rails (= 1.5.2)
415 414 redcarpet (~> 2.1.1)
416 415 resque (~> 1.20.0)
... ...
doc/api/projects.md
... ... @@ -129,6 +129,43 @@ Parameters:
129 129 ]
130 130 ```
131 131  
  132 +Get a single project repository branch.
  133 +
  134 +```
  135 +GET /projects/:id/repository/branches/:branch
  136 +```
  137 +
  138 +Parameters:
  139 +
  140 ++ `id` (required) - The ID or code name of a project
  141 ++ `branch` (required) - The name of the branch
  142 +
  143 +```json
  144 +{
  145 + "name": "master",
  146 + "commit": {
  147 + "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c",
  148 + "parents": [
  149 + {
  150 + "id": "4ad91d3c1144c406e50c7b33bae684bd6837faf8"
  151 + }
  152 + ],
  153 + "tree": "46e82de44b1061621357f24c05515327f2795a95",
  154 + "message": "add projects API",
  155 + "author": {
  156 + "name": "John Smith",
  157 + "email": "john@example.com"
  158 + },
  159 + "committer": {
  160 + "name": "John Smith",
  161 + "email": "john@example.com"
  162 + },
  163 + "authored_date": "2012-06-27T05:51:39-07:00",
  164 + "committed_date": "2012-06-28T03:44:20-07:00"
  165 + }
  166 +}
  167 +```
  168 +
132 169 ## Project repository tags
133 170  
134 171 Get a list of project repository tags sorted by name in reverse alphabetical order.
... ... @@ -268,3 +305,19 @@ Parameters:
268 305 + `snippet_id` (required) - The ID of a project's snippet
269 306  
270 307 Status code `200` will be returned on success.
  308 +
  309 +## Raw blob content
  310 +
  311 +Get the raw file contents for a file.
  312 +
  313 +```
  314 +GET /projects/:id/repository/commits/:sha/blob
  315 +```
  316 +
  317 +Parameters:
  318 +
  319 ++ `id` (required) - The ID or code name of a project
  320 ++ `sha` (required) - The commit or branch name
  321 ++ `filepath` (required) - The path th the file
  322 +
  323 +Will return the raw file contents.
... ...
lib/api/projects.rb
... ... @@ -33,6 +33,18 @@ module Gitlab
33 33 present user_project.repo.heads.sort_by(&:name), :with => Entities::RepoObject
34 34 end
35 35  
  36 + # Get a single branch
  37 + #
  38 + # Parameters:
  39 + # id (required) - The ID or code name of a project
  40 + # branch_id (required) - The name of the branch
  41 + # Example Request:
  42 + # GET /projects/:id/repository/branches/:branch_id
  43 + get ":id/repository/branches/:branch_id" do
  44 + @branch = user_project.repo.heads.find { |item| item.name == params[:branch_id] }
  45 + present @branch, :with => Entities::RepoObject
  46 + end
  47 +
36 48 # Get a project repository tags
37 49 #
38 50 # Parameters:
... ... @@ -131,6 +143,34 @@ module Gitlab
131 143 @snippet = user_project.snippets.find(params[:snippet_id])
132 144 present @snippet.content
133 145 end
  146 +
  147 + # Get a raw file contents
  148 + #
  149 + # Parameters:
  150 + # id (required) - The ID or code name of a project
  151 + # sha (required) - The commit or branch name
  152 + # filepath (required) - The path to the file to display
  153 + # Example Request:
  154 + # GET /projects/:id/repository/commits/:sha/blob
  155 + get ":id/repository/commits/:sha/blob" do
  156 + ref = params[:sha]
  157 +
  158 + commit = user_project.commit ref
  159 + error!('404 Commit Not Found', 404) unless commit
  160 +
  161 + tree = Tree.new commit.tree, user_project, ref, params[:filepath]
  162 + error!('404 File Not Found', 404) unless tree.try(:tree)
  163 +
  164 + if tree.text?
  165 + encoding = Gitlab::Encode.detect_encoding(tree.data)
  166 + content_type encoding ? "text/plain; charset=#{encoding}" : "text/plain"
  167 + else
  168 + content_type tree.mime_type
  169 + end
  170 +
  171 + present tree.data
  172 + end
  173 +
134 174 end
135 175 end
136 176 end
... ...
spec/api/projects_spec.rb
... ... @@ -53,6 +53,16 @@ describe Gitlab::API do
53 53 end
54 54 end
55 55  
  56 + describe "GET /projects/:id/repository/branches/:branch" do
  57 + it "should return the branch information for a single branch" do
  58 + get "#{api_prefix}/projects/#{project.code}/repository/branches/new_design?private_token=#{user.private_token}"
  59 + response.status.should == 200
  60 +
  61 + json_response['name'].should == 'new_design'
  62 + json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
  63 + end
  64 + end
  65 +
56 66 describe "GET /projects/:id/repository/tags" do
57 67 it "should return an array of project tags" do
58 68 get "#{api_prefix}/projects/#{project.code}/repository/tags?private_token=#{user.private_token}"
... ... @@ -93,7 +103,7 @@ describe Gitlab::API do
93 103 it "should delete existing project snippet" do
94 104 expect {
95 105 delete "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}"
96   - }.should change { Snippet.count }.by(-1)
  106 + }.to change { Snippet.count }.by(-1)
97 107 end
98 108 end
99 109  
... ... @@ -103,4 +113,24 @@ describe Gitlab::API do
103 113 response.status.should == 200
104 114 end
105 115 end
  116 +
  117 + describe "GET /projects/:id/:sha/blob" do
  118 + it "should get the raw file contents" do
  119 + get "#{api_prefix}/projects/#{project.code}/repository/commits/master/blob?filepath=README.md&private_token=#{user.private_token}"
  120 +
  121 + response.status.should == 200
  122 + end
  123 +
  124 + it "should return 404 for invalid branch_name" do
  125 + get "#{api_prefix}/projects/#{project.code}/repository/commits/invalid_branch_name/blob?filepath=README.md&private_token=#{user.private_token}"
  126 +
  127 + response.status.should == 404
  128 + end
  129 +
  130 + it "should return 404 for invalid file" do
  131 + get "#{api_prefix}/projects/#{project.code}/repository/commits/master/blob?filepath=README.invalid&private_token=#{user.private_token}"
  132 +
  133 + response.status.should == 404
  134 + end
  135 + end
106 136 end
... ...