Commit 8686d09d3e9e53089f7d3f11d74f0368f76a2aa2

Authored by Dmitriy Zaporozhets
2 parents 8a55636f 6cf39fe1

Merge branch 'feature/api_fle_encoded' into 'master'

Complete api files CRUD

Adds ability to read file from repository with content encoded with Base64.
app/models/repository.rb
@@ -180,13 +180,13 @@ class Repository @@ -180,13 +180,13 @@ class Repository
180 end 180 end
181 181
182 def blob_at_branch(branch_name, path) 182 def blob_at_branch(branch_name, path)
183 - last_commit = commit(branch_name) 183 + last_commit = commit(branch_name)
184 184
185 - if last_commit  
186 - blob_at(last_commit.sha, path)  
187 - else  
188 - nil  
189 - end 185 + if last_commit
  186 + blob_at(last_commit.sha, path)
  187 + else
  188 + nil
  189 + end
190 end 190 end
191 191
192 # Returns url for submodule 192 # Returns url for submodule
app/views/help/_api_layout.html.haml
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 %i.icon-angle-left 5 %i.icon-angle-left
6 Back to help 6 Back to help
7 %ul.nav.nav-pills.nav-stacked 7 %ul.nav.nav-pills.nav-stacked
8 - - %w(README projects project_snippets repositories deploy_keys users groups session issues milestones merge_requests notes system_hooks).each do |file| 8 + - %w(README projects project_snippets repositories repository_files commits deploy_keys users groups session issues milestones merge_requests notes system_hooks).each do |file|
9 %li{class: file == @category ? 'active' : nil} 9 %li{class: file == @category ? 'active' : nil}
10 = link_to file.titleize, help_api_file_path(file) 10 = link_to file.titleize, help_api_file_path(file)
11 11
doc/api/README.md
@@ -127,6 +127,8 @@ But when you want to create a link to web page - use `http:://host/project/issu @@ -127,6 +127,8 @@ But when you want to create a link to web page - use `http:://host/project/issu
127 + [Projects](projects.md) 127 + [Projects](projects.md)
128 + [Project Snippets](project_snippets.md) 128 + [Project Snippets](project_snippets.md)
129 + [Repositories](repositories.md) 129 + [Repositories](repositories.md)
  130 ++ [Repository Files](repository_files.md)
  131 ++ [Commits](commits.md)
130 + [Merge Requests](merge_requests.md) 132 + [Merge Requests](merge_requests.md)
131 + [Issues](issues.md) 133 + [Issues](issues.md)
132 + [Milestones](milestones.md) 134 + [Milestones](milestones.md)
doc/api/commits.md 0 → 100644
@@ -0,0 +1,95 @@ @@ -0,0 +1,95 @@
  1 +# Commits API
  2 +
  3 +## List repository commits
  4 +
  5 +Get a list of repository commits in a project.
  6 +
  7 +```
  8 +GET /projects/:id/repository/commits
  9 +```
  10 +
  11 +Parameters:
  12 +
  13 ++ `id` (required) - The ID of a project
  14 ++ `ref_name` (optional) - The name of a repository branch or tag or if not given the default branch
  15 +
  16 +```json
  17 +[
  18 + {
  19 + "id": "ed899a2f4b50b4370feeea94676502b42383c746",
  20 + "short_id": "ed899a2f4b5",
  21 + "title": "Replace sanitize with escape once",
  22 + "author_name": "Dmitriy Zaporozhets",
  23 + "author_email": "dzaporozhets@sphereconsultinginc.com",
  24 + "created_at": "2012-09-20T11:50:22+03:00"
  25 + },
  26 + {
  27 + "id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",
  28 + "short_id": "6104942438c",
  29 + "title": "Sanitize for network graph",
  30 + "author_name": "randx",
  31 + "author_email": "dmitriy.zaporozhets@gmail.com",
  32 + "created_at": "2012-09-20T09:06:12+03:00"
  33 + }
  34 +]
  35 +```
  36 +
  37 +## Get a single commit
  38 +
  39 +Get a specific commit identified by the commit hash or name of a branch or tag.
  40 +
  41 +```
  42 +GET /projects/:id/repository/commits/:sha
  43 +```
  44 +
  45 +Parameters:
  46 +
  47 ++ `id` (required) - The ID of a project
  48 ++ `sha` (required) - The commit hash or name of a repository branch or tag
  49 +
  50 +```json
  51 +{
  52 + "id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",
  53 + "short_id": "6104942438c",
  54 + "title": "Sanitize for network graph",
  55 + "author_name": "randx",
  56 + "author_email": "dmitriy.zaporozhets@gmail.com",
  57 + "created_at": "2012-09-20T09:06:12+03:00",
  58 + "committed_date": "2012-09-20T09:06:12+03:00",
  59 + "authored_date": "2012-09-20T09:06:12+03:00",
  60 + "parent_ids" : [
  61 + "ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"
  62 + ]
  63 +}
  64 +```
  65 +
  66 +
  67 +## Get the diff of a commit
  68 +
  69 +Get the diff of a commit in a project.
  70 +
  71 +```
  72 +GET /projects/:id/repository/commits/:sha/diff
  73 +```
  74 +
  75 +Parameters:
  76 +
  77 ++ `id` (required) - The ID of a project
  78 ++ `sha` (required) - The name of a repository branch or tag or if not given the default branch
  79 +
  80 +```json
  81 +[
  82 + {
  83 + "diff": "--- a/doc/update/5.4-to-6.0.md\n+++ b/doc/update/5.4-to-6.0.md\n@@ -71,6 +71,8 @@\n sudo -u git -H bundle exec rake migrate_keys RAILS_ENV=production\n sudo -u git -H bundle exec rake migrate_inline_notes RAILS_ENV=production\n \n+sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production\n+\n ```\n \n ### 6. Update config files",
  84 + "new_path": "doc/update/5.4-to-6.0.md",
  85 + "old_path": "doc/update/5.4-to-6.0.md",
  86 + "a_mode": null,
  87 + "b_mode": "100644",
  88 + "new_file": false,
  89 + "renamed_file": false,
  90 + "deleted_file": false
  91 + }
  92 +]
  93 +```
  94 +
  95 +
doc/api/repositories.md
@@ -204,99 +204,6 @@ Parameters: @@ -204,99 +204,6 @@ Parameters:
204 ] 204 ]
205 ``` 205 ```
206 206
207 -  
208 -## List repository commits  
209 -  
210 -Get a list of repository commits in a project.  
211 -  
212 -```  
213 -GET /projects/:id/repository/commits  
214 -```  
215 -  
216 -Parameters:  
217 -  
218 -+ `id` (required) - The ID of a project  
219 -+ `ref_name` (optional) - The name of a repository branch or tag or if not given the default branch  
220 -  
221 -```json  
222 -[  
223 - {  
224 - "id": "ed899a2f4b50b4370feeea94676502b42383c746",  
225 - "short_id": "ed899a2f4b5",  
226 - "title": "Replace sanitize with escape once",  
227 - "author_name": "Dmitriy Zaporozhets",  
228 - "author_email": "dzaporozhets@sphereconsultinginc.com",  
229 - "created_at": "2012-09-20T11:50:22+03:00"  
230 - },  
231 - {  
232 - "id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",  
233 - "short_id": "6104942438c",  
234 - "title": "Sanitize for network graph",  
235 - "author_name": "randx",  
236 - "author_email": "dmitriy.zaporozhets@gmail.com",  
237 - "created_at": "2012-09-20T09:06:12+03:00"  
238 - }  
239 -]  
240 -```  
241 -  
242 -## Get a single commit  
243 -  
244 -Get a specific commit identified by the commit hash or name of a branch or tag.  
245 -  
246 -```  
247 -GET /projects/:id/repository/commits/:sha  
248 -```  
249 -  
250 -Parameters:  
251 -  
252 -+ `id` (required) - The ID of a project  
253 -+ `sha` (required) - The commit hash or name of a repository branch or tag  
254 -  
255 -```json  
256 -{  
257 - "id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",  
258 - "short_id": "6104942438c",  
259 - "title": "Sanitize for network graph",  
260 - "author_name": "randx",  
261 - "author_email": "dmitriy.zaporozhets@gmail.com",  
262 - "created_at": "2012-09-20T09:06:12+03:00",  
263 - "committed_date": "2012-09-20T09:06:12+03:00",  
264 - "authored_date": "2012-09-20T09:06:12+03:00",  
265 - "parent_ids" : [  
266 - "ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"  
267 - ]  
268 -}  
269 -```  
270 -  
271 -  
272 -## Get the diff of a commit  
273 -  
274 -Get the diff of a commit in a project.  
275 -  
276 -```  
277 -GET /projects/:id/repository/commits/:sha/diff  
278 -```  
279 -  
280 -Parameters:  
281 -  
282 -+ `id` (required) - The ID of a project  
283 -+ `sha` (required) - The name of a repository branch or tag or if not given the default branch  
284 -  
285 -```json  
286 -[  
287 - {  
288 - "diff": "--- a/doc/update/5.4-to-6.0.md\n+++ b/doc/update/5.4-to-6.0.md\n@@ -71,6 +71,8 @@\n sudo -u git -H bundle exec rake migrate_keys RAILS_ENV=production\n sudo -u git -H bundle exec rake migrate_inline_notes RAILS_ENV=production\n \n+sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production\n+\n ```\n \n ### 6. Update config files",  
289 - "new_path": "doc/update/5.4-to-6.0.md",  
290 - "old_path": "doc/update/5.4-to-6.0.md",  
291 - "a_mode": null,  
292 - "b_mode": "100644",  
293 - "new_file": false,  
294 - "renamed_file": false,  
295 - "deleted_file": false  
296 - }  
297 -]  
298 -```  
299 -  
300 ## List repository tree 207 ## List repository tree
301 208
302 Get a list of repository files and directories in a project. 209 Get a list of repository files and directories in a project.
@@ -388,44 +295,3 @@ GET /projects/:id/repository/archive @@ -388,44 +295,3 @@ GET /projects/:id/repository/archive
388 Parameters: 295 Parameters:
389 + `id` (required) - The ID of a project 296 + `id` (required) - The ID of a project
390 + `sha` (optional) - The commit sha to download defaults to the tip of the default branch 297 + `sha` (optional) - The commit sha to download defaults to the tip of the default branch
391 -  
392 -  
393 -## Create new file in repository  
394 -  
395 -```  
396 -POST /projects/:id/repository/files  
397 -```  
398 -  
399 -Parameters:  
400 -  
401 -+ `file_path` (optional) - Full path to new file. Ex. lib/class.rb  
402 -+ `branch_name` (required) - The name of branch  
403 -+ `encoding` (optional) - 'text' or 'base64'. Text is default.  
404 -+ `content` (required) - File content  
405 -+ `commit_message` (required) - Commit message  
406 -  
407 -## Update existing file in repository  
408 -  
409 -```  
410 -PUT /projects/:id/repository/files  
411 -```  
412 -  
413 -Parameters:  
414 -  
415 -+ `file_path` (required) - Full path to file. Ex. lib/class.rb  
416 -+ `branch_name` (required) - The name of branch  
417 -+ `encoding` (optional) - 'text' or 'base64'. Text is default.  
418 -+ `content` (required) - New file content  
419 -+ `commit_message` (required) - Commit message  
420 -  
421 -## Delete existing file in repository  
422 -  
423 -```  
424 -DELETE /projects/:id/repository/files  
425 -```  
426 -  
427 -Parameters:  
428 -  
429 -+ `file_path` (required) - Full path to file. Ex. lib/class.rb  
430 -+ `branch_name` (required) - The name of branch  
431 -+ `commit_message` (required) - Commit message  
doc/api/repository_files.md 0 → 100644
@@ -0,0 +1,102 @@ @@ -0,0 +1,102 @@
  1 +# CRUD for repository files
  2 +
  3 +## Create, read, update and delete repository files using this API
  4 +
  5 +- - -
  6 +
  7 +## Get file from repository
  8 +
  9 +Allows you to receive information about file in repository like name, size, content.
  10 +Note that file content is Base64 encoded.
  11 +
  12 +```
  13 +GET /projects/:id/repository/files
  14 +```
  15 +
  16 +Example response:
  17 +
  18 +```json
  19 +{
  20 + "file_name": "key.rb",
  21 + "file_path": "app/models/key.rb",
  22 + "size": 1476,
  23 + "encoding": "base64",
  24 + "content": "IyA9PSBTY2hlbWEgSW5mb3...",
  25 + "ref": "master",
  26 + "blob_id": "79f7bbd25901e8334750839545a9bd021f0e4c83",
  27 + "commit_id": "d5a3ff139356ce33e37e73add446f16869741b50"
  28 +}
  29 +```
  30 +
  31 +Parameters:
  32 +
  33 ++ `file_path` (required) - Full path to new file. Ex. lib/class.rb
  34 ++ `ref` (required) - The name of branch, tag or commit
  35 +
  36 +## Create new file in repository
  37 +
  38 +```
  39 +POST /projects/:id/repository/files
  40 +```
  41 +
  42 +Example response:
  43 +
  44 +```json
  45 +{
  46 + "file_name": "app/project.rb",
  47 + "branch_name": "master",
  48 +}
  49 +```
  50 +
  51 +Parameters:
  52 +
  53 ++ `file_path` (required) - Full path to new file. Ex. lib/class.rb
  54 ++ `branch_name` (required) - The name of branch
  55 ++ `encoding` (optional) - 'text' or 'base64'. Text is default.
  56 ++ `content` (required) - File content
  57 ++ `commit_message` (required) - Commit message
  58 +
  59 +## Update existing file in repository
  60 +
  61 +```
  62 +PUT /projects/:id/repository/files
  63 +```
  64 +
  65 +Example response:
  66 +
  67 +```json
  68 +{
  69 + "file_name": "app/project.rb",
  70 + "branch_name": "master",
  71 +}
  72 +```
  73 +
  74 +Parameters:
  75 +
  76 ++ `file_path` (required) - Full path to file. Ex. lib/class.rb
  77 ++ `branch_name` (required) - The name of branch
  78 ++ `encoding` (optional) - 'text' or 'base64'. Text is default.
  79 ++ `content` (required) - New file content
  80 ++ `commit_message` (required) - Commit message
  81 +
  82 +## Delete existing file in repository
  83 +
  84 +```
  85 +DELETE /projects/:id/repository/files
  86 +```
  87 +
  88 +Example response:
  89 +
  90 +```json
  91 +{
  92 + "file_name": "app/project.rb",
  93 + "branch_name": "master",
  94 +}
  95 +```
  96 +
  97 +Parameters:
  98 +
  99 ++ `file_path` (required) - Full path to file. Ex. lib/class.rb
  100 ++ `branch_name` (required) - The name of branch
  101 ++ `commit_message` (required) - Commit message
  102 +
lib/api/api.rb
@@ -42,6 +42,7 @@ module API @@ -42,6 +42,7 @@ module API
42 mount ProjectHooks 42 mount ProjectHooks
43 mount Services 43 mount Services
44 mount Files 44 mount Files
  45 + mount Commits
45 mount Namespaces 46 mount Namespaces
46 end 47 end
47 end 48 end
lib/api/commits.rb 0 → 100644
@@ -0,0 +1,64 @@ @@ -0,0 +1,64 @@
  1 +require 'mime/types'
  2 +
  3 +module API
  4 + # Projects API
  5 + class Commits < Grape::API
  6 + before { authenticate! }
  7 + before { authorize! :download_code, user_project }
  8 +
  9 + resource :projects do
  10 + helpers do
  11 + def handle_project_member_errors(errors)
  12 + if errors[:project_access].any?
  13 + error!(errors[:project_access], 422)
  14 + end
  15 + not_found!
  16 + end
  17 + end
  18 +
  19 + # Get a project repository commits
  20 + #
  21 + # Parameters:
  22 + # id (required) - The ID of a project
  23 + # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used
  24 + # Example Request:
  25 + # GET /projects/:id/repository/commits
  26 + get ":id/repository/commits" do
  27 + page = (params[:page] || 0).to_i
  28 + per_page = (params[:per_page] || 20).to_i
  29 + ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
  30 +
  31 + commits = user_project.repository.commits(ref, nil, per_page, page * per_page)
  32 + present commits, with: Entities::RepoCommit
  33 + end
  34 +
  35 + # Get a specific commit of a project
  36 + #
  37 + # Parameters:
  38 + # id (required) - The ID of a project
  39 + # sha (required) - The commit hash or name of a repository branch or tag
  40 + # Example Request:
  41 + # GET /projects/:id/repository/commits/:sha
  42 + get ":id/repository/commits/:sha" do
  43 + sha = params[:sha]
  44 + commit = user_project.repository.commit(sha)
  45 + not_found! "Commit" unless commit
  46 + present commit, with: Entities::RepoCommitDetail
  47 + end
  48 +
  49 + # Get the diff for a specific commit of a project
  50 + #
  51 + # Parameters:
  52 + # id (required) - The ID of a project
  53 + # sha (required) - The commit or branch name
  54 + # Example Request:
  55 + # GET /projects/:id/repository/commits/:sha/diff
  56 + get ":id/repository/commits/:sha/diff" do
  57 + sha = params[:sha]
  58 + commit = user_project.repository.commit(sha)
  59 + not_found! "Commit" unless commit
  60 + commit.diffs
  61 + end
  62 + end
  63 + end
  64 +end
lib/api/files.rb
@@ -5,10 +5,61 @@ module API @@ -5,10 +5,61 @@ module API
5 before { authorize! :push_code, user_project } 5 before { authorize! :push_code, user_project }
6 6
7 resource :projects do 7 resource :projects do
  8 + # Get file from repository
  9 + # File content is Base64 encoded
  10 + #
  11 + # Parameters:
  12 + # file_path (required) - The path to the file. Ex. lib/class.rb
  13 + # ref (required) - The name of branch, tag or commit
  14 + #
  15 + # Example Request:
  16 + # GET /projects/:id/repository/files
  17 + #
  18 + # Example response:
  19 + # {
  20 + # "file_name": "key.rb",
  21 + # "file_path": "app/models/key.rb",
  22 + # "size": 1476,
  23 + # "encoding": "base64",
  24 + # "content": "IyA9PSBTY2hlbWEgSW5mb3...",
  25 + # "ref": "master",
  26 + # "blob_id": "79f7bbd25901e8334750839545a9bd021f0e4c83",
  27 + # "commit_id": "d5a3ff139356ce33e37e73add446f16869741b50"
  28 + # }
  29 + #
  30 + get ":id/repository/files" do
  31 + required_attributes! [:file_path, :ref]
  32 + attrs = attributes_for_keys [:file_path, :ref]
  33 + ref = attrs.delete(:ref)
  34 + file_path = attrs.delete(:file_path)
  35 +
  36 + commit = user_project.repository.commit(ref)
  37 + not_found! "Commit" unless commit
  38 +
  39 + blob = user_project.repository.blob_at(commit.sha, file_path)
  40 +
  41 + if blob
  42 + status(200)
  43 +
  44 + {
  45 + file_name: blob.name,
  46 + file_path: blob.path,
  47 + size: blob.size,
  48 + encoding: "base64",
  49 + content: Base64.encode64(blob.data),
  50 + ref: ref,
  51 + blob_id: blob.id,
  52 + commit_id: commit.id,
  53 + }
  54 + else
  55 + render_api_error!('File not found', 404)
  56 + end
  57 + end
  58 +
8 # Create new file in repository 59 # Create new file in repository
9 # 60 #
10 # Parameters: 61 # Parameters:
11 - # file_path (optional) - The path to new file. Ex. lib/class.rb 62 + # file_path (required) - The path to new file. Ex. lib/class.rb
12 # branch_name (required) - The name of branch 63 # branch_name (required) - The name of branch
13 # content (required) - File content 64 # content (required) - File content
14 # commit_message (required) - Commit message 65 # commit_message (required) - Commit message
lib/api/repositories.rb
@@ -85,50 +85,6 @@ module API @@ -85,50 +85,6 @@ module API
85 present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject, project: user_project 85 present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject, project: user_project
86 end 86 end
87 87
88 - # Get a project repository commits  
89 - #  
90 - # Parameters:  
91 - # id (required) - The ID of a project  
92 - # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used  
93 - # Example Request:  
94 - # GET /projects/:id/repository/commits  
95 - get ":id/repository/commits" do  
96 - page = (params[:page] || 0).to_i  
97 - per_page = (params[:per_page] || 20).to_i  
98 - ref = params[:ref_name] || user_project.try(:default_branch) || 'master'  
99 -  
100 - commits = user_project.repository.commits(ref, nil, per_page, page * per_page)  
101 - present commits, with: Entities::RepoCommit  
102 - end  
103 -  
104 - # Get a specific commit of a project  
105 - #  
106 - # Parameters:  
107 - # id (required) - The ID of a project  
108 - # sha (required) - The commit hash or name of a repository branch or tag  
109 - # Example Request:  
110 - # GET /projects/:id/repository/commits/:sha  
111 - get ":id/repository/commits/:sha" do  
112 - sha = params[:sha]  
113 - commit = user_project.repository.commit(sha)  
114 - not_found! "Commit" unless commit  
115 - present commit, with: Entities::RepoCommitDetail  
116 - end  
117 -  
118 - # Get the diff for a specific commit of a project  
119 - #  
120 - # Parameters:  
121 - # id (required) - The ID of a project  
122 - # sha (required) - The commit or branch name  
123 - # Example Request:  
124 - # GET /projects/:id/repository/commits/:sha/diff  
125 - get ":id/repository/commits/:sha/diff" do  
126 - sha = params[:sha]  
127 - commit = user_project.repository.commit(sha)  
128 - not_found! "Commit" unless commit  
129 - commit.diffs  
130 - end  
131 -  
132 # Get a project repository tree 88 # Get a project repository tree
133 # 89 #
134 # Parameters: 90 # Parameters:
spec/requests/api/commits_spec.rb 0 → 100644
@@ -0,0 +1,87 @@ @@ -0,0 +1,87 @@
  1 +require 'spec_helper'
  2 +require 'mime/types'
  3 +
  4 +describe API::API do
  5 + include ApiHelpers
  6 + before(:each) { enable_observers }
  7 + after(:each) {disable_observers}
  8 +
  9 + let(:user) { create(:user) }
  10 + let(:user2) { create(:user) }
  11 + let!(:project) { create(:project, creator_id: user.id) }
  12 + let!(:master) { create(:users_project, user: user, project: project, project_access: UsersProject::MASTER) }
  13 + let!(:guest) { create(:users_project, user: user2, project: project, project_access: UsersProject::GUEST) }
  14 +
  15 + before { project.team << [user, :reporter] }
  16 +
  17 + describe "GET /projects/:id/repository/commits" do
  18 + context "authorized user" do
  19 + before { project.team << [user2, :reporter] }
  20 +
  21 + it "should return project commits" do
  22 + get api("/projects/#{project.id}/repository/commits", user)
  23 + response.status.should == 200
  24 +
  25 + json_response.should be_an Array
  26 + json_response.first['id'].should == project.repository.commit.id
  27 + end
  28 + end
  29 +
  30 + context "unauthorized user" do
  31 + it "should not return project commits" do
  32 + get api("/projects/#{project.id}/repository/commits")
  33 + response.status.should == 401
  34 + end
  35 + end
  36 + end
  37 +
  38 + describe "GET /projects:id/repository/commits/:sha" do
  39 + context "authorized user" do
  40 + it "should return a commit by sha" do
  41 + get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
  42 + response.status.should == 200
  43 + json_response['id'].should == project.repository.commit.id
  44 + json_response['title'].should == project.repository.commit.title
  45 + end
  46 +
  47 + it "should return a 404 error if not found" do
  48 + get api("/projects/#{project.id}/repository/commits/invalid_sha", user)
  49 + response.status.should == 404
  50 + end
  51 + end
  52 +
  53 + context "unauthorized user" do
  54 + it "should not return the selected commit" do
  55 + get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}")
  56 + response.status.should == 401
  57 + end
  58 + end
  59 + end
  60 +
  61 + describe "GET /projects:id/repository/commits/:sha/diff" do
  62 + context "authorized user" do
  63 + before { project.team << [user2, :reporter] }
  64 +
  65 + it "should return the diff of the selected commit" do
  66 + get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff", user)
  67 + response.status.should == 200
  68 +
  69 + json_response.should be_an Array
  70 + json_response.length.should >= 1
  71 + json_response.first.keys.should include "diff"
  72 + end
  73 +
  74 + it "should return a 404 error if invalid commit" do
  75 + get api("/projects/#{project.id}/repository/commits/invalid_sha/diff", user)
  76 + response.status.should == 404
  77 + end
  78 + end
  79 +
  80 + context "unauthorized user" do
  81 + it "should not return the diff of the selected commit" do
  82 + get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff")
  83 + response.status.should == 401
  84 + end
  85 + end
  86 + end
  87 +end
spec/requests/api/files_spec.rb
@@ -9,6 +9,36 @@ describe API::API do @@ -9,6 +9,36 @@ describe API::API do
9 let!(:project) { create(:project, namespace: user.namespace ) } 9 let!(:project) { create(:project, namespace: user.namespace ) }
10 before { project.team << [user, :developer] } 10 before { project.team << [user, :developer] }
11 11
  12 + describe "GET /projects/:id/repository/files" do
  13 + it "should return file info" do
  14 + params = {
  15 + file_path: 'app/models/key.rb',
  16 + ref: 'master',
  17 + }
  18 +
  19 + get api("/projects/#{project.id}/repository/files", user), params
  20 + response.status.should == 200
  21 + json_response['file_path'].should == 'app/models/key.rb'
  22 + json_response['file_name'].should == 'key.rb'
  23 + Base64.decode64(json_response['content']).lines.first.should == "class Key < ActiveRecord::Base\n"
  24 + end
  25 +
  26 + it "should return a 400 bad request if no params given" do
  27 + get api("/projects/#{project.id}/repository/files", user)
  28 + response.status.should == 400
  29 + end
  30 +
  31 + it "should return a 404 if such file does not exist" do
  32 + params = {
  33 + file_path: 'app/models/application.rb',
  34 + ref: 'master',
  35 + }
  36 +
  37 + get api("/projects/#{project.id}/repository/files", user), params
  38 + response.status.should == 404
  39 + end
  40 + end
  41 +
12 describe "POST /projects/:id/repository/files" do 42 describe "POST /projects/:id/repository/files" do
13 let(:valid_params) { 43 let(:valid_params) {
14 { 44 {
spec/requests/api/repositories_spec.rb
@@ -103,77 +103,6 @@ describe API::API do @@ -103,77 +103,6 @@ describe API::API do
103 end 103 end
104 end 104 end
105 105
106 - describe "GET /projects/:id/repository/commits" do  
107 - context "authorized user" do  
108 - before { project.team << [user2, :reporter] }  
109 -  
110 - it "should return project commits" do  
111 - get api("/projects/#{project.id}/repository/commits", user)  
112 - response.status.should == 200  
113 -  
114 - json_response.should be_an Array  
115 - json_response.first['id'].should == project.repository.commit.id  
116 - end  
117 - end  
118 -  
119 - context "unauthorized user" do  
120 - it "should not return project commits" do  
121 - get api("/projects/#{project.id}/repository/commits")  
122 - response.status.should == 401  
123 - end  
124 - end  
125 - end  
126 -  
127 - describe "GET /projects:id/repository/commits/:sha" do  
128 - context "authorized user" do  
129 - it "should return a commit by sha" do  
130 - get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)  
131 - response.status.should == 200  
132 - json_response['id'].should == project.repository.commit.id  
133 - json_response['title'].should == project.repository.commit.title  
134 - end  
135 -  
136 - it "should return a 404 error if not found" do  
137 - get api("/projects/#{project.id}/repository/commits/invalid_sha", user)  
138 - response.status.should == 404  
139 - end  
140 - end  
141 -  
142 - context "unauthorized user" do  
143 - it "should not return the selected commit" do  
144 - get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}")  
145 - response.status.should == 401  
146 - end  
147 - end  
148 - end  
149 -  
150 - describe "GET /projects:id/repository/commits/:sha/diff" do  
151 - context "authorized user" do  
152 - before { project.team << [user2, :reporter] }  
153 -  
154 - it "should return the diff of the selected commit" do  
155 - get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff", user)  
156 - response.status.should == 200  
157 -  
158 - json_response.should be_an Array  
159 - json_response.length.should >= 1  
160 - json_response.first.keys.should include "diff"  
161 - end  
162 -  
163 - it "should return a 404 error if invalid commit" do  
164 - get api("/projects/#{project.id}/repository/commits/invalid_sha/diff", user)  
165 - response.status.should == 404  
166 - end  
167 - end  
168 -  
169 - context "unauthorized user" do  
170 - it "should not return the diff of the selected commit" do  
171 - get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff")  
172 - response.status.should == 401  
173 - end  
174 - end  
175 - end  
176 -  
177 describe "GET /projects/:id/repository/tree" do 106 describe "GET /projects/:id/repository/tree" do
178 context "authorized user" do 107 context "authorized user" do
179 before { project.team << [user2, :reporter] } 108 before { project.team << [user2, :reporter] }