Commit 3b5a90bdf654f9715fd15c189d59bd56492bae8c

Authored by miks
1 parent 27e44365

Projects hooks API implemented

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
... ... @@ -103,6 +103,46 @@ module Gitlab
103 103 nil
104 104 end
105 105  
  106 + # Get project hooks
  107 + #
  108 + # Parameters:
  109 + # id (required) - The ID or code name of a project
  110 + # Example Request:
  111 + # GET /projects/:id/hooks
  112 + get ":id/hooks" do
  113 + @hooks = paginate user_project.hooks
  114 + present @hooks, with: Entities::Hook
  115 + end
  116 +
  117 + # Add hook to project
  118 + #
  119 + # Parameters:
  120 + # id (required) - The ID or code name of a project
  121 + # url (required) - The hook URL
  122 + # Example Request:
  123 + # POST /projects/:id/hooks
  124 + post ":id/hooks" do
  125 + @hook = user_project.hooks.new({"url" => params[:url]})
  126 + if @hook.save
  127 + present @hook, with: Entities::Hook
  128 + else
  129 + error!({'message' => '404 Not found'}, 404)
  130 + end
  131 + end
  132 +
  133 + # Delete project hook
  134 + #
  135 + # Parameters:
  136 + # id (required) - The ID or code name of a project
  137 + # hook_id (required) - The ID of hook to delete
  138 + # Example Request:
  139 + # DELETE /projects/:id/hooks
  140 + delete ":id/hooks" do
  141 + @hook = user_project.hooks.find(params[:hook_id])
  142 + @hook.destroy
  143 + nil
  144 + end
  145 +
106 146 # Get a project repository branches
107 147 #
108 148 # 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 }
... ... @@ -147,6 +148,36 @@ describe Gitlab::API do
147 148 end
148 149 end
149 150  
  151 + describe "GET /projects/:id/hooks" do
  152 + it "should return project hooks" do
  153 + get api("/projects/#{project.code}/hooks", user)
  154 +
  155 + response.status.should == 200
  156 +
  157 + json_response.should be_an Array
  158 + json_response.count.should == 1
  159 + json_response.first['url'].should == "http://example.com"
  160 + end
  161 + end
  162 +
  163 + describe "POST /projects/:id/users" do
  164 + it "should add hook to project" do
  165 + expect {
  166 + post api("/projects/#{project.code}/hooks", user),
  167 + "url" => "http://example.com"
  168 + }.to change {project.hooks.count}.by(1)
  169 + end
  170 + end
  171 +
  172 + describe "DELETE /projects/:id/hooks" do
  173 + it "should delete hook from project" do
  174 + expect {
  175 + delete api("/projects/#{project.code}/hooks", user),
  176 + hook_id: hook.id
  177 + }.to change {project.hooks.count}.by(-1)
  178 + end
  179 + end
  180 +
150 181 describe "GET /projects/:id/repository/tags" do
151 182 it "should return an array of project tags" do
152 183 get api("/projects/#{project.code}/repository/tags", user)
... ...