Commit 3643df1f7cbcc3734055b0d710fd21dd4426ca3b
Exists in
master
and in
4 other branches
Merge pull request #1411 from miks/project_hooks_api
Project hooks API
Showing
4 changed files
with
122 additions
and
0 deletions
Show diff stats
doc/api/projects.md
| ... | ... | @@ -173,6 +173,50 @@ Parameters: |
| 173 | 173 | |
| 174 | 174 | Will return status `200 OK` on success, or `404 Not found` on fail. |
| 175 | 175 | |
| 176 | +## Get project hooks | |
| 177 | + | |
| 178 | +Get hooks for project | |
| 179 | + | |
| 180 | +``` | |
| 181 | +GET /projects/:id/hooks | |
| 182 | +``` | |
| 183 | + | |
| 184 | +Parameters: | |
| 185 | + | |
| 186 | ++ `id` (required) - The ID or code name of a project | |
| 187 | + | |
| 188 | +Will return hooks with status `200 OK` on success, or `404 Not found` on fail. | |
| 189 | + | |
| 190 | +## Add project hook | |
| 191 | + | |
| 192 | +Add hook to project | |
| 193 | + | |
| 194 | +``` | |
| 195 | +POST /projects/:id/hooks | |
| 196 | +``` | |
| 197 | + | |
| 198 | +Parameters: | |
| 199 | + | |
| 200 | ++ `id` (required) - The ID or code name of a project | |
| 201 | ++ `url` (required) - The hook URL | |
| 202 | + | |
| 203 | +Will return status `201 Created` on success, or `404 Not found` on fail. | |
| 204 | + | |
| 205 | +## Delete project hook | |
| 206 | + | |
| 207 | +Delete hook from project | |
| 208 | + | |
| 209 | +``` | |
| 210 | +DELETE /projects/:id/hooks | |
| 211 | +``` | |
| 212 | + | |
| 213 | +Parameters: | |
| 214 | + | |
| 215 | ++ `id` (required) - The ID or code name of a project | |
| 216 | ++ `hook_id` (required) - The ID of hook to delete | |
| 217 | + | |
| 218 | +Will return status `200 OK` on success, or `404 Not found` on fail. | |
| 219 | + | |
| 176 | 220 | ## Project repository branches |
| 177 | 221 | |
| 178 | 222 | Get a list of repository branches from a project, sorted by name alphabetically. | ... | ... |
lib/api/entities.rb
| ... | ... | @@ -9,6 +9,10 @@ module Gitlab |
| 9 | 9 | expose :id, :email, :name, :blocked, :created_at |
| 10 | 10 | end |
| 11 | 11 | |
| 12 | + class Hook < Grape::Entity | |
| 13 | + expose :id, :url | |
| 14 | + end | |
| 15 | + | |
| 12 | 16 | class Project < Grape::Entity |
| 13 | 17 | expose :id, :code, :name, :description, :path, :default_branch |
| 14 | 18 | expose :owner, using: Entities::UserBasic | ... | ... |
lib/api/projects.rb
| ... | ... | @@ -106,6 +106,49 @@ module Gitlab |
| 106 | 106 | nil |
| 107 | 107 | end |
| 108 | 108 | |
| 109 | + # Get project hooks | |
| 110 | + # | |
| 111 | + # Parameters: | |
| 112 | + # id (required) - The ID or code name of a project | |
| 113 | + # Example Request: | |
| 114 | + # GET /projects/:id/hooks | |
| 115 | + get ":id/hooks" do | |
| 116 | + authorize! :admin_project, user_project | |
| 117 | + @hooks = paginate user_project.hooks | |
| 118 | + present @hooks, with: Entities::Hook | |
| 119 | + end | |
| 120 | + | |
| 121 | + # Add hook to project | |
| 122 | + # | |
| 123 | + # Parameters: | |
| 124 | + # id (required) - The ID or code name of a project | |
| 125 | + # url (required) - The hook URL | |
| 126 | + # Example Request: | |
| 127 | + # POST /projects/:id/hooks | |
| 128 | + post ":id/hooks" do | |
| 129 | + authorize! :admin_project, user_project | |
| 130 | + @hook = user_project.hooks.new({"url" => params[:url]}) | |
| 131 | + if @hook.save | |
| 132 | + present @hook, with: Entities::Hook | |
| 133 | + else | |
| 134 | + error!({'message' => '404 Not found'}, 404) | |
| 135 | + end | |
| 136 | + end | |
| 137 | + | |
| 138 | + # Delete project hook | |
| 139 | + # | |
| 140 | + # Parameters: | |
| 141 | + # id (required) - The ID or code name of a project | |
| 142 | + # hook_id (required) - The ID of hook to delete | |
| 143 | + # Example Request: | |
| 144 | + # DELETE /projects/:id/hooks | |
| 145 | + delete ":id/hooks" do | |
| 146 | + authorize! :admin_project, user_project | |
| 147 | + @hook = user_project.hooks.find(params[:hook_id]) | |
| 148 | + @hook.destroy | |
| 149 | + nil | |
| 150 | + end | |
| 151 | + | |
| 109 | 152 | # Get a project repository branches |
| 110 | 153 | # |
| 111 | 154 | # Parameters: | ... | ... |
spec/requests/api/projects_spec.rb
| ... | ... | @@ -6,6 +6,7 @@ describe Gitlab::API do |
| 6 | 6 | let(:user) { Factory :user } |
| 7 | 7 | let(:user2) { Factory.create(:user) } |
| 8 | 8 | let(:user3) { Factory.create(:user) } |
| 9 | + let!(:hook) { Factory :project_hook, project: project, url: "http://example.com" } | |
| 9 | 10 | let!(:project) { Factory :project, owner: user } |
| 10 | 11 | let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' } |
| 11 | 12 | let!(:users_project) { Factory :users_project, user: user, project: project, project_access: UsersProject::MASTER } |
| ... | ... | @@ -149,6 +150,36 @@ describe Gitlab::API do |
| 149 | 150 | end |
| 150 | 151 | end |
| 151 | 152 | |
| 153 | + describe "GET /projects/:id/hooks" do | |
| 154 | + it "should return project hooks" do | |
| 155 | + get api("/projects/#{project.code}/hooks", user) | |
| 156 | + | |
| 157 | + response.status.should == 200 | |
| 158 | + | |
| 159 | + json_response.should be_an Array | |
| 160 | + json_response.count.should == 1 | |
| 161 | + json_response.first['url'].should == "http://example.com" | |
| 162 | + end | |
| 163 | + end | |
| 164 | + | |
| 165 | + describe "POST /projects/:id/users" do | |
| 166 | + it "should add hook to project" do | |
| 167 | + expect { | |
| 168 | + post api("/projects/#{project.code}/hooks", user), | |
| 169 | + "url" => "http://example.com" | |
| 170 | + }.to change {project.hooks.count}.by(1) | |
| 171 | + end | |
| 172 | + end | |
| 173 | + | |
| 174 | + describe "DELETE /projects/:id/hooks" do | |
| 175 | + it "should delete hook from project" do | |
| 176 | + expect { | |
| 177 | + delete api("/projects/#{project.code}/hooks", user), | |
| 178 | + hook_id: hook.id | |
| 179 | + }.to change {project.hooks.count}.by(-1) | |
| 180 | + end | |
| 181 | + end | |
| 182 | + | |
| 152 | 183 | describe "GET /projects/:id/repository/tags" do |
| 153 | 184 | it "should return an array of project tags" do |
| 154 | 185 | get api("/projects/#{project.code}/repository/tags", user) | ... | ... |