Commit 6cf39fe10ddf6f90a17d52ba6b50425f58215eeb

Authored by Dmitriy Zaporozhets
1 parent ddbe9780

Extract commits API to separate file

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/views/help/_api_layout.html.haml
... ... @@ -5,7 +5,7 @@
5 5 %i.icon-angle-left
6 6 Back to help
7 7 %ul.nav.nav-pills.nav-stacked
8   - - %w(README projects project_snippets repositories repository_files 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 9 %li{class: file == @category ? 'active' : nil}
10 10 = link_to file.titleize, help_api_file_path(file)
11 11  
... ...
doc/api/README.md
... ... @@ -128,6 +128,7 @@ But when you want to create a link to web page - use `http:://host/project/issu
128 128 + [Project Snippets](project_snippets.md)
129 129 + [Repositories](repositories.md)
130 130 + [Repository Files](repository_files.md)
  131 ++ [Commits](commits.md)
131 132 + [Merge Requests](merge_requests.md)
132 133 + [Issues](issues.md)
133 134 + [Milestones](milestones.md)
... ...
doc/api/commits.md 0 → 100644
... ... @@ -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 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 207 ## List repository tree
301 208  
302 209 Get a list of repository files and directories in a project.
... ...
lib/api/api.rb
... ... @@ -40,6 +40,7 @@ module API
40 40 mount ProjectHooks
41 41 mount Services
42 42 mount Files
  43 + mount Commits
43 44 mount Namespaces
44 45 end
45 46 end
... ...
lib/api/commits.rb 0 → 100644
... ... @@ -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/repositories.rb
... ... @@ -85,50 +85,6 @@ module API
85 85 present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject, project: user_project
86 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 88 # Get a project repository tree
133 89 #
134 90 # Parameters:
... ...
spec/requests/api/commits_spec.rb 0 → 100644
... ... @@ -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/repositories_spec.rb
... ... @@ -103,77 +103,6 @@ describe API::API do
103 103 end
104 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 106 describe "GET /projects/:id/repository/tree" do
178 107 context "authorized user" do
179 108 before { project.team << [user2, :reporter] }
... ...