Commit 5857a7a9ce6f9bd37b633d48074778bcbde880a4
Exists in
master
and in
4 other branches
Merge pull request #2839 from m4tthumphrey/protected-branches-api
Added methods to protect and unprotect branches in from the API
Showing
4 changed files
with
152 additions
and
5 deletions
Show diff stats
doc/api/repositories.md
| ... | ... | @@ -33,7 +33,8 @@ Parameters: |
| 33 | 33 | }, |
| 34 | 34 | "authored_date": "2012-06-27T05:51:39-07:00", |
| 35 | 35 | "committed_date": "2012-06-28T03:44:20-07:00" |
| 36 | - } | |
| 36 | + }, | |
| 37 | + "protected": true | |
| 37 | 38 | } |
| 38 | 39 | ] |
| 39 | 40 | ``` |
| ... | ... | @@ -73,7 +74,88 @@ Parameters: |
| 73 | 74 | }, |
| 74 | 75 | "authored_date": "2012-06-27T05:51:39-07:00", |
| 75 | 76 | "committed_date": "2012-06-28T03:44:20-07:00" |
| 76 | - } | |
| 77 | + }, | |
| 78 | + "protected": true | |
| 79 | +} | |
| 80 | +``` | |
| 81 | + | |
| 82 | +## Protect a project repository branch | |
| 83 | + | |
| 84 | +Protect a single project repository branch. | |
| 85 | + | |
| 86 | +``` | |
| 87 | +PUT /projects/:id/repository/branches/:branch/protect | |
| 88 | +``` | |
| 89 | + | |
| 90 | +Parameters: | |
| 91 | + | |
| 92 | ++ `id` (required) - The ID of a project | |
| 93 | ++ `branch` (required) - The name of the branch | |
| 94 | + | |
| 95 | +```json | |
| 96 | +{ | |
| 97 | + "name": "master", | |
| 98 | + "commit": { | |
| 99 | + "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c", | |
| 100 | + "parents": [ | |
| 101 | + { | |
| 102 | + "id": "4ad91d3c1144c406e50c7b33bae684bd6837faf8" | |
| 103 | + } | |
| 104 | + ], | |
| 105 | + "tree": "46e82de44b1061621357f24c05515327f2795a95", | |
| 106 | + "message": "add projects API", | |
| 107 | + "author": { | |
| 108 | + "name": "John Smith", | |
| 109 | + "email": "john@example.com" | |
| 110 | + }, | |
| 111 | + "committer": { | |
| 112 | + "name": "John Smith", | |
| 113 | + "email": "john@example.com" | |
| 114 | + }, | |
| 115 | + "authored_date": "2012-06-27T05:51:39-07:00", | |
| 116 | + "committed_date": "2012-06-28T03:44:20-07:00" | |
| 117 | + }, | |
| 118 | + "protected": true | |
| 119 | +} | |
| 120 | +``` | |
| 121 | + | |
| 122 | +## Unprotect a project repository branch | |
| 123 | + | |
| 124 | +Unprotect a single project repository branch. | |
| 125 | + | |
| 126 | +``` | |
| 127 | +PUT /projects/:id/repository/branches/:branch/unprotect | |
| 128 | +``` | |
| 129 | + | |
| 130 | +Parameters: | |
| 131 | + | |
| 132 | ++ `id` (required) - The ID of a project | |
| 133 | ++ `branch` (required) - The name of the branch | |
| 134 | + | |
| 135 | +```json | |
| 136 | +{ | |
| 137 | + "name": "master", | |
| 138 | + "commit": { | |
| 139 | + "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c", | |
| 140 | + "parents": [ | |
| 141 | + { | |
| 142 | + "id": "4ad91d3c1144c406e50c7b33bae684bd6837faf8" | |
| 143 | + } | |
| 144 | + ], | |
| 145 | + "tree": "46e82de44b1061621357f24c05515327f2795a95", | |
| 146 | + "message": "add projects API", | |
| 147 | + "author": { | |
| 148 | + "name": "John Smith", | |
| 149 | + "email": "john@example.com" | |
| 150 | + }, | |
| 151 | + "committer": { | |
| 152 | + "name": "John Smith", | |
| 153 | + "email": "john@example.com" | |
| 154 | + }, | |
| 155 | + "authored_date": "2012-06-27T05:51:39-07:00", | |
| 156 | + "committed_date": "2012-06-28T03:44:20-07:00" | |
| 157 | + }, | |
| 158 | + "protected": false | |
| 77 | 159 | } |
| 78 | 160 | ``` |
| 79 | 161 | |
| ... | ... | @@ -110,7 +192,8 @@ Parameters: |
| 110 | 192 | }, |
| 111 | 193 | "authored_date": "2012-05-28T04:42:42-07:00", |
| 112 | 194 | "committed_date": "2012-05-28T04:42:42-07:00" |
| 113 | - } | |
| 195 | + }, | |
| 196 | + "protected": null | |
| 114 | 197 | } |
| 115 | 198 | ] |
| 116 | 199 | ``` | ... | ... |
lib/api/entities.rb
| ... | ... | @@ -34,6 +34,11 @@ module Gitlab |
| 34 | 34 | |
| 35 | 35 | class RepoObject < Grape::Entity |
| 36 | 36 | expose :name, :commit |
| 37 | + expose :protected do |repo, options| | |
| 38 | + if options[:project] | |
| 39 | + options[:project].protected_branch? repo.name | |
| 40 | + end | |
| 41 | + end | |
| 37 | 42 | end |
| 38 | 43 | |
| 39 | 44 | class RepoCommit < Grape::Entity | ... | ... |
lib/api/projects.rb
| ... | ... | @@ -218,7 +218,7 @@ module Gitlab |
| 218 | 218 | # Example Request: |
| 219 | 219 | # GET /projects/:id/repository/branches |
| 220 | 220 | get ":id/repository/branches" do |
| 221 | - present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject | |
| 221 | + present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject, project: user_project | |
| 222 | 222 | end |
| 223 | 223 | |
| 224 | 224 | # Get a single branch |
| ... | ... | @@ -230,7 +230,43 @@ module Gitlab |
| 230 | 230 | # GET /projects/:id/repository/branches/:branch |
| 231 | 231 | get ":id/repository/branches/:branch" do |
| 232 | 232 | @branch = user_project.repo.heads.find { |item| item.name == params[:branch] } |
| 233 | - present @branch, with: Entities::RepoObject | |
| 233 | + present @branch, with: Entities::RepoObject, project: user_project | |
| 234 | + end | |
| 235 | + | |
| 236 | + # Protect a single branch | |
| 237 | + # | |
| 238 | + # Parameters: | |
| 239 | + # id (required) - The ID of a project | |
| 240 | + # branch (required) - The name of the branch | |
| 241 | + # Example Request: | |
| 242 | + # PUT /projects/:id/repository/branches/:branch/protect | |
| 243 | + put ":id/repository/branches/:branch/protect" do | |
| 244 | + @branch = user_project.repo.heads.find { |item| item.name == params[:branch] } | |
| 245 | + protected = user_project.protected_branches.find_by_name(@branch.name) | |
| 246 | + | |
| 247 | + unless protected | |
| 248 | + user_project.protected_branches.create(:name => @branch.name) | |
| 249 | + end | |
| 250 | + | |
| 251 | + present @branch, with: Entities::RepoObject, project: user_project | |
| 252 | + end | |
| 253 | + | |
| 254 | + # Unprotect a single branch | |
| 255 | + # | |
| 256 | + # Parameters: | |
| 257 | + # id (required) - The ID of a project | |
| 258 | + # branch (required) - The name of the branch | |
| 259 | + # Example Request: | |
| 260 | + # PUT /projects/:id/repository/branches/:branch/unprotect | |
| 261 | + put ":id/repository/branches/:branch/unprotect" do | |
| 262 | + @branch = user_project.repo.heads.find { |item| item.name == params[:branch] } | |
| 263 | + protected = user_project.protected_branches.find_by_name(@branch.name) | |
| 264 | + | |
| 265 | + if protected | |
| 266 | + protected.destroy | |
| 267 | + end | |
| 268 | + | |
| 269 | + present @branch, with: Entities::RepoObject, project: user_project | |
| 234 | 270 | end |
| 235 | 271 | |
| 236 | 272 | # Get a project repository tags | ... | ... |
spec/requests/api/projects_spec.rb
| ... | ... | @@ -107,6 +107,29 @@ describe Gitlab::API do |
| 107 | 107 | |
| 108 | 108 | json_response['name'].should == 'new_design' |
| 109 | 109 | json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1' |
| 110 | + json_response['protected'].should == false | |
| 111 | + end | |
| 112 | + end | |
| 113 | + | |
| 114 | + describe "PUT /projects/:id/repository/branches/:branch/protect" do | |
| 115 | + it "should protect a single branch" do | |
| 116 | + put api("/projects/#{project.id}/repository/branches/new_design/protect", user) | |
| 117 | + response.status.should == 200 | |
| 118 | + | |
| 119 | + json_response['name'].should == 'new_design' | |
| 120 | + json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1' | |
| 121 | + json_response['protected'].should == true | |
| 122 | + end | |
| 123 | + end | |
| 124 | + | |
| 125 | + describe "PUT /projects/:id/repository/branches/:branch/unprotect" do | |
| 126 | + it "should unprotect a single branch" do | |
| 127 | + put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user) | |
| 128 | + response.status.should == 200 | |
| 129 | + | |
| 130 | + json_response['name'].should == 'new_design' | |
| 131 | + json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1' | |
| 132 | + json_response['protected'].should == false | |
| 110 | 133 | end |
| 111 | 134 | end |
| 112 | 135 | ... | ... |