Commit 9b276f0003d19599e6426eb5f58028a81ede4a30

Authored by Dmitriy Zaporozhets
1 parent 2ab2d60b

Move branches api to separate class

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
lib/api/api.rb
@@ -45,5 +45,6 @@ module API @@ -45,5 +45,6 @@ module API
45 mount Files 45 mount Files
46 mount Commits 46 mount Commits
47 mount Namespaces 47 mount Namespaces
  48 + mount Branches
48 end 49 end
49 end 50 end
lib/api/branches.rb 0 → 100644
@@ -0,0 +1,70 @@ @@ -0,0 +1,70 @@
  1 +require 'mime/types'
  2 +
  3 +module API
  4 + # Projects API
  5 + class Branches < Grape::API
  6 + before { authenticate! }
  7 + before { authorize! :download_code, user_project }
  8 +
  9 + resource :projects do
  10 + # Get a project repository branches
  11 + #
  12 + # Parameters:
  13 + # id (required) - The ID of a project
  14 + # Example Request:
  15 + # GET /projects/:id/repository/branches
  16 + get ":id/repository/branches" do
  17 + present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject, project: user_project
  18 + end
  19 +
  20 + # Get a single branch
  21 + #
  22 + # Parameters:
  23 + # id (required) - The ID of a project
  24 + # branch (required) - The name of the branch
  25 + # Example Request:
  26 + # GET /projects/:id/repository/branches/:branch
  27 + get ":id/repository/branches/:branch" do
  28 + @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
  29 + not_found!("Branch does not exist") if @branch.nil?
  30 + present @branch, with: Entities::RepoObject, project: user_project
  31 + end
  32 +
  33 + # Protect a single branch
  34 + #
  35 + # Parameters:
  36 + # id (required) - The ID of a project
  37 + # branch (required) - The name of the branch
  38 + # Example Request:
  39 + # PUT /projects/:id/repository/branches/:branch/protect
  40 + put ":id/repository/branches/:branch/protect" do
  41 + authorize_admin_project
  42 +
  43 + @branch = user_project.repository.find_branch(params[:branch])
  44 + not_found! unless @branch
  45 + protected_branch = user_project.protected_branches.find_by(name: @branch.name)
  46 + user_project.protected_branches.create(name: @branch.name) unless protected_branch
  47 +
  48 + present @branch, with: Entities::RepoObject, project: user_project
  49 + end
  50 +
  51 + # Unprotect a single branch
  52 + #
  53 + # Parameters:
  54 + # id (required) - The ID of a project
  55 + # branch (required) - The name of the branch
  56 + # Example Request:
  57 + # PUT /projects/:id/repository/branches/:branch/unprotect
  58 + put ":id/repository/branches/:branch/unprotect" do
  59 + authorize_admin_project
  60 +
  61 + @branch = user_project.repository.find_branch(params[:branch])
  62 + not_found! unless @branch
  63 + protected_branch = user_project.protected_branches.find_by(name: @branch.name)
  64 + protected_branch.destroy if protected_branch
  65 +
  66 + present @branch, with: Entities::RepoObject, project: user_project
  67 + end
  68 + end
  69 + end
  70 +end
lib/api/commits.rb
1 require 'mime/types' 1 require 'mime/types'
2 2
3 module API 3 module API
4 - # Projects API 4 + # Projects commits API
5 class Commits < Grape::API 5 class Commits < Grape::API
6 before { authenticate! } 6 before { authenticate! }
7 before { authorize! :download_code, user_project } 7 before { authorize! :download_code, user_project }
8 8
9 resource :projects do 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 10 # Get a project repository commits
20 # 11 #
21 # Parameters: 12 # Parameters:
lib/api/repositories.rb
@@ -15,66 +15,6 @@ module API @@ -15,66 +15,6 @@ module API
15 not_found! 15 not_found!
16 end 16 end
17 end 17 end
18 -  
19 - # Get a project repository branches  
20 - #  
21 - # Parameters:  
22 - # id (required) - The ID of a project  
23 - # Example Request:  
24 - # GET /projects/:id/repository/branches  
25 - get ":id/repository/branches" do  
26 - present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject, project: user_project  
27 - end  
28 -  
29 - # Get a single branch  
30 - #  
31 - # Parameters:  
32 - # id (required) - The ID of a project  
33 - # branch (required) - The name of the branch  
34 - # Example Request:  
35 - # GET /projects/:id/repository/branches/:branch  
36 - get ":id/repository/branches/:branch" do  
37 - @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }  
38 - not_found!("Branch does not exist") if @branch.nil?  
39 - present @branch, with: Entities::RepoObject, project: user_project  
40 - end  
41 -  
42 - # Protect a single branch  
43 - #  
44 - # Parameters:  
45 - # id (required) - The ID of a project  
46 - # branch (required) - The name of the branch  
47 - # Example Request:  
48 - # PUT /projects/:id/repository/branches/:branch/protect  
49 - put ":id/repository/branches/:branch/protect" do  
50 - authorize_admin_project  
51 -  
52 - @branch = user_project.repository.find_branch(params[:branch])  
53 - not_found! unless @branch  
54 - protected_branch = user_project.protected_branches.find_by(name: @branch.name)  
55 - user_project.protected_branches.create(name: @branch.name) unless protected_branch  
56 -  
57 - present @branch, with: Entities::RepoObject, project: user_project  
58 - end  
59 -  
60 - # Unprotect a single branch  
61 - #  
62 - # Parameters:  
63 - # id (required) - The ID of a project  
64 - # branch (required) - The name of the branch  
65 - # Example Request:  
66 - # PUT /projects/:id/repository/branches/:branch/unprotect  
67 - put ":id/repository/branches/:branch/unprotect" do  
68 - authorize_admin_project  
69 -  
70 - @branch = user_project.repository.find_branch(params[:branch])  
71 - not_found! unless @branch  
72 - protected_branch = user_project.protected_branches.find_by(name: @branch.name)  
73 - protected_branch.destroy if protected_branch  
74 -  
75 - present @branch, with: Entities::RepoObject, project: user_project  
76 - end  
77 -  
78 # Get a project repository tags 18 # Get a project repository tags
79 # 19 #
80 # Parameters: 20 # Parameters:
spec/requests/api/branches_spec.rb 0 → 100644
@@ -0,0 +1,95 @@ @@ -0,0 +1,95 @@
  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 + describe "GET /projects/:id/repository/branches" do
  16 + it "should return an array of project branches" do
  17 + get api("/projects/#{project.id}/repository/branches", user)
  18 + response.status.should == 200
  19 + json_response.should be_an Array
  20 + json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name
  21 + end
  22 + end
  23 +
  24 + describe "GET /projects/:id/repository/branches/:branch" do
  25 + it "should return the branch information for a single branch" do
  26 + get api("/projects/#{project.id}/repository/branches/new_design", user)
  27 + response.status.should == 200
  28 +
  29 + json_response['name'].should == 'new_design'
  30 + json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
  31 + json_response['protected'].should == false
  32 + end
  33 +
  34 + it "should return a 403 error if guest" do
  35 + get api("/projects/#{project.id}/repository/branches", user2)
  36 + response.status.should == 403
  37 + end
  38 +
  39 + it "should return a 404 error if branch is not available" do
  40 + get api("/projects/#{project.id}/repository/branches/unknown", user)
  41 + response.status.should == 404
  42 + end
  43 + end
  44 +
  45 + describe "PUT /projects/:id/repository/branches/:branch/protect" do
  46 + it "should protect a single branch" do
  47 + put api("/projects/#{project.id}/repository/branches/new_design/protect", user)
  48 + response.status.should == 200
  49 +
  50 + json_response['name'].should == 'new_design'
  51 + json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
  52 + json_response['protected'].should == true
  53 + end
  54 +
  55 + it "should return a 404 error if branch not found" do
  56 + put api("/projects/#{project.id}/repository/branches/unknown/protect", user)
  57 + response.status.should == 404
  58 + end
  59 +
  60 + it "should return a 403 error if guest" do
  61 + put api("/projects/#{project.id}/repository/branches/new_design/protect", user2)
  62 + response.status.should == 403
  63 + end
  64 +
  65 + it "should return success when protect branch again" do
  66 + put api("/projects/#{project.id}/repository/branches/new_design/protect", user)
  67 + put api("/projects/#{project.id}/repository/branches/new_design/protect", user)
  68 + response.status.should == 200
  69 + end
  70 + end
  71 +
  72 + describe "PUT /projects/:id/repository/branches/:branch/unprotect" do
  73 + it "should unprotect a single branch" do
  74 + put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)
  75 + response.status.should == 200
  76 +
  77 + json_response['name'].should == 'new_design'
  78 + json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
  79 + json_response['protected'].should == false
  80 + end
  81 +
  82 + it "should return success when unprotect branch" do
  83 + put api("/projects/#{project.id}/repository/branches/unknown/unprotect", user)
  84 + response.status.should == 404
  85 + end
  86 +
  87 + it "should return success when unprotect branch again" do
  88 + put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)
  89 + put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)
  90 + response.status.should == 200
  91 + end
  92 + end
  93 +
  94 +
  95 +end
spec/requests/api/repositories_spec.rb
@@ -14,86 +14,6 @@ describe API::API do @@ -14,86 +14,6 @@ describe API::API do
14 14
15 before { project.team << [user, :reporter] } 15 before { project.team << [user, :reporter] }
16 16
17 -  
18 - describe "GET /projects/:id/repository/branches" do  
19 - it "should return an array of project branches" do  
20 - get api("/projects/#{project.id}/repository/branches", user)  
21 - response.status.should == 200  
22 - json_response.should be_an Array  
23 - json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name  
24 - end  
25 - end  
26 -  
27 - describe "GET /projects/:id/repository/branches/:branch" do  
28 - it "should return the branch information for a single branch" do  
29 - get api("/projects/#{project.id}/repository/branches/new_design", user)  
30 - response.status.should == 200  
31 -  
32 - json_response['name'].should == 'new_design'  
33 - json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'  
34 - json_response['protected'].should == false  
35 - end  
36 -  
37 - it "should return a 403 error if guest" do  
38 - get api("/projects/#{project.id}/repository/branches", user2)  
39 - response.status.should == 403  
40 - end  
41 -  
42 - it "should return a 404 error if branch is not available" do  
43 - get api("/projects/#{project.id}/repository/branches/unknown", user)  
44 - response.status.should == 404  
45 - end  
46 - end  
47 -  
48 - describe "PUT /projects/:id/repository/branches/:branch/protect" do  
49 - it "should protect a single branch" do  
50 - put api("/projects/#{project.id}/repository/branches/new_design/protect", user)  
51 - response.status.should == 200  
52 -  
53 - json_response['name'].should == 'new_design'  
54 - json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'  
55 - json_response['protected'].should == true  
56 - end  
57 -  
58 - it "should return a 404 error if branch not found" do  
59 - put api("/projects/#{project.id}/repository/branches/unknown/protect", user)  
60 - response.status.should == 404  
61 - end  
62 -  
63 - it "should return a 403 error if guest" do  
64 - put api("/projects/#{project.id}/repository/branches/new_design/protect", user2)  
65 - response.status.should == 403  
66 - end  
67 -  
68 - it "should return success when protect branch again" do  
69 - put api("/projects/#{project.id}/repository/branches/new_design/protect", user)  
70 - put api("/projects/#{project.id}/repository/branches/new_design/protect", user)  
71 - response.status.should == 200  
72 - end  
73 - end  
74 -  
75 - describe "PUT /projects/:id/repository/branches/:branch/unprotect" do  
76 - it "should unprotect a single branch" do  
77 - put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)  
78 - response.status.should == 200  
79 -  
80 - json_response['name'].should == 'new_design'  
81 - json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'  
82 - json_response['protected'].should == false  
83 - end  
84 -  
85 - it "should return success when unprotect branch" do  
86 - put api("/projects/#{project.id}/repository/branches/unknown/unprotect", user)  
87 - response.status.should == 404  
88 - end  
89 -  
90 - it "should return success when unprotect branch again" do  
91 - put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)  
92 - put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)  
93 - response.status.should == 200  
94 - end  
95 - end  
96 -  
97 describe "GET /projects/:id/repository/tags" do 17 describe "GET /projects/:id/repository/tags" do
98 it "should return an array of project tags" do 18 it "should return an array of project tags" do
99 get api("/projects/#{project.id}/repository/tags", user) 19 get api("/projects/#{project.id}/repository/tags", user)