Commit d63706d7 authored by Nihad Abbasov's avatar Nihad Abbasov

Merge pull request #1157 from CodeAdept/api_blob_contents

API blob contents
parents 5926bbac ce837f3d
...@@ -20,3 +20,6 @@ config/database.yml ...@@ -20,3 +20,6 @@ config/database.yml
config/initializers/omniauth.rb config/initializers/omniauth.rb
config/unicorn.rb config/unicorn.rb
db/data.yml db/data.yml
.idea
.DS_Store
...@@ -18,7 +18,7 @@ gem 'yaml_db', :git => "https://github.com/gitlabhq/yaml_db.git" ...@@ -18,7 +18,7 @@ gem 'yaml_db', :git => "https://github.com/gitlabhq/yaml_db.git"
gem 'grack', :git => "https://github.com/gitlabhq/grack.git" gem 'grack', :git => "https://github.com/gitlabhq/grack.git"
gem "linguist", "~> 1.0.0", :git => "https://github.com/gitlabhq/linguist.git" gem "linguist", "~> 1.0.0", :git => "https://github.com/gitlabhq/linguist.git"
gem "grape" gem "grape", "~> 0.2.1"
gem "stamp" gem "stamp"
gem "kaminari" gem "kaminari"
gem "haml-rails" gem "haml-rails"
......
...@@ -170,7 +170,7 @@ GEM ...@@ -170,7 +170,7 @@ GEM
gherkin (2.11.0) gherkin (2.11.0)
json (>= 1.4.6) json (>= 1.4.6)
git (1.2.5) git (1.2.5)
grape (0.2.0) grape (0.2.1)
hashie (~> 1.2) hashie (~> 1.2)
multi_json multi_json
multi_xml multi_xml
...@@ -392,7 +392,7 @@ DEPENDENCIES ...@@ -392,7 +392,7 @@ DEPENDENCIES
git git
gitolite! gitolite!
grack! grack!
grape grape (~> 0.2.1)
grit! grit!
haml-rails haml-rails
httparty httparty
......
...@@ -129,6 +129,43 @@ Parameters: ...@@ -129,6 +129,43 @@ Parameters:
] ]
``` ```
Get a single project repository branch.
```
GET /projects/:id/repository/branches/:branch
```
Parameters:
+ `id` (required) - The ID or code name of a project
+ `branch` (required) - The name of the branch
```json
{
"name": "master",
"commit": {
"id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c",
"parents": [
{
"id": "4ad91d3c1144c406e50c7b33bae684bd6837faf8"
}
],
"tree": "46e82de44b1061621357f24c05515327f2795a95",
"message": "add projects API",
"author": {
"name": "John Smith",
"email": "john@example.com"
},
"committer": {
"name": "John Smith",
"email": "john@example.com"
},
"authored_date": "2012-06-27T05:51:39-07:00",
"committed_date": "2012-06-28T03:44:20-07:00"
}
}
```
## Project repository tags ## Project repository tags
Get a list of project repository tags sorted by name in reverse alphabetical order. Get a list of project repository tags sorted by name in reverse alphabetical order.
...@@ -268,3 +305,19 @@ Parameters: ...@@ -268,3 +305,19 @@ Parameters:
+ `snippet_id` (required) - The ID of a project's snippet + `snippet_id` (required) - The ID of a project's snippet
Status code `200` will be returned on success. Status code `200` will be returned on success.
## Raw blob content
Get the raw file contents for a file.
```
GET /projects/:id/repository/commits/:sha/blob
```
Parameters:
+ `id` (required) - The ID or code name of a project
+ `sha` (required) - The commit or branch name
+ `filepath` (required) - The path the file
Will return the raw file contents.
...@@ -33,6 +33,18 @@ module Gitlab ...@@ -33,6 +33,18 @@ module Gitlab
present user_project.repo.heads.sort_by(&:name), :with => Entities::RepoObject present user_project.repo.heads.sort_by(&:name), :with => Entities::RepoObject
end end
# Get a single branch
#
# Parameters:
# id (required) - The ID or code name of a project
# branch_id (required) - The name of the branch
# Example Request:
# GET /projects/:id/repository/branches/:branch_id
get ":id/repository/branches/:branch_id" do
@branch = user_project.repo.heads.find { |item| item.name == params[:branch_id] }
present @branch, :with => Entities::RepoObject
end
# Get a project repository tags # Get a project repository tags
# #
# Parameters: # Parameters:
...@@ -131,6 +143,34 @@ module Gitlab ...@@ -131,6 +143,34 @@ module Gitlab
@snippet = user_project.snippets.find(params[:snippet_id]) @snippet = user_project.snippets.find(params[:snippet_id])
present @snippet.content present @snippet.content
end end
# Get a raw file contents
#
# Parameters:
# id (required) - The ID or code name of a project
# sha (required) - The commit or branch name
# filepath (required) - The path to the file to display
# Example Request:
# GET /projects/:id/repository/commits/:sha/blob
get ":id/repository/commits/:sha/blob" do
ref = params[:sha]
commit = user_project.commit ref
error!('404 Commit Not Found', 404) unless commit
tree = Tree.new commit.tree, user_project, ref, params[:filepath]
error!('404 File Not Found', 404) unless tree.try(:tree)
if tree.text?
encoding = Gitlab::Encode.detect_encoding(tree.data)
content_type encoding ? "text/plain; charset=#{encoding}" : "text/plain"
else
content_type tree.mime_type
end
present tree.data
end
end end
end end
end end
...@@ -53,6 +53,16 @@ describe Gitlab::API do ...@@ -53,6 +53,16 @@ describe Gitlab::API do
end end
end end
describe "GET /projects/:id/repository/branches/:branch" do
it "should return the branch information for a single branch" do
get "#{api_prefix}/projects/#{project.code}/repository/branches/new_design?private_token=#{user.private_token}"
response.status.should == 200
json_response['name'].should == 'new_design'
json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
end
end
describe "GET /projects/:id/repository/tags" do describe "GET /projects/:id/repository/tags" do
it "should return an array of project tags" do it "should return an array of project tags" do
get "#{api_prefix}/projects/#{project.code}/repository/tags?private_token=#{user.private_token}" get "#{api_prefix}/projects/#{project.code}/repository/tags?private_token=#{user.private_token}"
...@@ -93,7 +103,7 @@ describe Gitlab::API do ...@@ -93,7 +103,7 @@ describe Gitlab::API do
it "should delete existing project snippet" do it "should delete existing project snippet" do
expect { expect {
delete "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" delete "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}"
}.should change { Snippet.count }.by(-1) }.to change { Snippet.count }.by(-1)
end end
end end
...@@ -103,4 +113,24 @@ describe Gitlab::API do ...@@ -103,4 +113,24 @@ describe Gitlab::API do
response.status.should == 200 response.status.should == 200
end end
end end
describe "GET /projects/:id/:sha/blob" do
it "should get the raw file contents" do
get "#{api_prefix}/projects/#{project.code}/repository/commits/master/blob?filepath=README.md&private_token=#{user.private_token}"
response.status.should == 200
end
it "should return 404 for invalid branch_name" do
get "#{api_prefix}/projects/#{project.code}/repository/commits/invalid_branch_name/blob?filepath=README.md&private_token=#{user.private_token}"
response.status.should == 404
end
it "should return 404 for invalid file" do
get "#{api_prefix}/projects/#{project.code}/repository/commits/master/blob?filepath=README.invalid&private_token=#{user.private_token}"
response.status.should == 404
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment