summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2012-09-12 12:16:35 -0700
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2012-09-12 12:16:35 -0700
commit3643df1f7cbcc3734055b0d710fd21dd4426ca3b (patch)
tree2a1d223a7513813353d6dff5e7c9d8b7a0769807
parent6233fb6b5d4732729b5bd734357f47fa42d34ed3 (diff)
parent6d76e000d07300ab9e8e8c74b90db0abcdc48451 (diff)
downloadgitlab-ce-3643df1f7cbcc3734055b0d710fd21dd4426ca3b.tar.gz
Merge pull request #1411 from miks/project_hooks_api
Project hooks API
-rw-r--r--doc/api/projects.md44
-rw-r--r--lib/api/entities.rb4
-rw-r--r--lib/api/projects.rb43
-rw-r--r--spec/requests/api/projects_spec.rb31
4 files changed, 122 insertions, 0 deletions
diff --git a/doc/api/projects.md b/doc/api/projects.md
index 72874e59682..73d6adc9bc3 100644
--- a/doc/api/projects.md
+++ b/doc/api/projects.md
@@ -173,6 +173,50 @@ Parameters:
Will return status `200 OK` on success, or `404 Not found` on fail.
+## Get project hooks
+
+Get hooks for project
+
+```
+GET /projects/:id/hooks
+```
+
+Parameters:
+
++ `id` (required) - The ID or code name of a project
+
+Will return hooks with status `200 OK` on success, or `404 Not found` on fail.
+
+## Add project hook
+
+Add hook to project
+
+```
+POST /projects/:id/hooks
+```
+
+Parameters:
+
++ `id` (required) - The ID or code name of a project
++ `url` (required) - The hook URL
+
+Will return status `201 Created` on success, or `404 Not found` on fail.
+
+## Delete project hook
+
+Delete hook from project
+
+```
+DELETE /projects/:id/hooks
+```
+
+Parameters:
+
++ `id` (required) - The ID or code name of a project
++ `hook_id` (required) - The ID of hook to delete
+
+Will return status `200 OK` on success, or `404 Not found` on fail.
+
## Project repository branches
Get a list of repository branches from a project, sorted by name alphabetically.
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index fef5328d093..b50d683f940 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -9,6 +9,10 @@ module Gitlab
expose :id, :email, :name, :blocked, :created_at
end
+ class Hook < Grape::Entity
+ expose :id, :url
+ end
+
class Project < Grape::Entity
expose :id, :code, :name, :description, :path, :default_branch
expose :owner, using: Entities::UserBasic
diff --git a/lib/api/projects.rb b/lib/api/projects.rb
index 9d33323e5fb..cf23dc5f538 100644
--- a/lib/api/projects.rb
+++ b/lib/api/projects.rb
@@ -106,6 +106,49 @@ module Gitlab
nil
end
+ # Get project hooks
+ #
+ # Parameters:
+ # id (required) - The ID or code name of a project
+ # Example Request:
+ # GET /projects/:id/hooks
+ get ":id/hooks" do
+ authorize! :admin_project, user_project
+ @hooks = paginate user_project.hooks
+ present @hooks, with: Entities::Hook
+ end
+
+ # Add hook to project
+ #
+ # Parameters:
+ # id (required) - The ID or code name of a project
+ # url (required) - The hook URL
+ # Example Request:
+ # POST /projects/:id/hooks
+ post ":id/hooks" do
+ authorize! :admin_project, user_project
+ @hook = user_project.hooks.new({"url" => params[:url]})
+ if @hook.save
+ present @hook, with: Entities::Hook
+ else
+ error!({'message' => '404 Not found'}, 404)
+ end
+ end
+
+ # Delete project hook
+ #
+ # Parameters:
+ # id (required) - The ID or code name of a project
+ # hook_id (required) - The ID of hook to delete
+ # Example Request:
+ # DELETE /projects/:id/hooks
+ delete ":id/hooks" do
+ authorize! :admin_project, user_project
+ @hook = user_project.hooks.find(params[:hook_id])
+ @hook.destroy
+ nil
+ end
+
# Get a project repository branches
#
# Parameters:
diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb
index 570a4bc2165..b46380b21d9 100644
--- a/spec/requests/api/projects_spec.rb
+++ b/spec/requests/api/projects_spec.rb
@@ -6,6 +6,7 @@ describe Gitlab::API do
let(:user) { Factory :user }
let(:user2) { Factory.create(:user) }
let(:user3) { Factory.create(:user) }
+ let!(:hook) { Factory :project_hook, project: project, url: "http://example.com" }
let!(:project) { Factory :project, owner: user }
let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }
let!(:users_project) { Factory :users_project, user: user, project: project, project_access: UsersProject::MASTER }
@@ -149,6 +150,36 @@ describe Gitlab::API do
end
end
+ describe "GET /projects/:id/hooks" do
+ it "should return project hooks" do
+ get api("/projects/#{project.code}/hooks", user)
+
+ response.status.should == 200
+
+ json_response.should be_an Array
+ json_response.count.should == 1
+ json_response.first['url'].should == "http://example.com"
+ end
+ end
+
+ describe "POST /projects/:id/users" do
+ it "should add hook to project" do
+ expect {
+ post api("/projects/#{project.code}/hooks", user),
+ "url" => "http://example.com"
+ }.to change {project.hooks.count}.by(1)
+ end
+ end
+
+ describe "DELETE /projects/:id/hooks" do
+ it "should delete hook from project" do
+ expect {
+ delete api("/projects/#{project.code}/hooks", user),
+ hook_id: hook.id
+ }.to change {project.hooks.count}.by(-1)
+ end
+ end
+
describe "GET /projects/:id/repository/tags" do
it "should return an array of project tags" do
get api("/projects/#{project.code}/repository/tags", user)