2013-06-06 09:37:51 +00:00
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class ProjectHooks < Grape::API
|
|
|
|
before { authenticate! }
|
2013-09-30 13:49:55 +00:00
|
|
|
before { authorize_admin_project }
|
2013-06-06 09:37:51 +00:00
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
# Get project hooks
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/hooks
|
|
|
|
get ":id/hooks" do
|
|
|
|
@hooks = paginate user_project.hooks
|
2013-12-04 11:19:53 +00:00
|
|
|
present @hooks, with: Entities::ProjectHook
|
2013-06-06 09:37:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get a project hook
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# hook_id (required) - The ID of a project hook
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/hooks/:hook_id
|
|
|
|
get ":id/hooks/:hook_id" do
|
|
|
|
@hook = user_project.hooks.find(params[:hook_id])
|
2013-12-04 11:19:53 +00:00
|
|
|
present @hook, with: Entities::ProjectHook
|
2013-06-06 09:37:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add hook to project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# url (required) - The hook URL
|
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/hooks
|
|
|
|
post ":id/hooks" do
|
|
|
|
required_attributes! [:url]
|
2014-09-19 08:23:18 +00:00
|
|
|
attrs = attributes_for_keys [
|
|
|
|
:url,
|
|
|
|
:push_events,
|
|
|
|
:issues_events,
|
|
|
|
:merge_requests_events,
|
2015-05-16 06:33:31 +00:00
|
|
|
:tag_push_events,
|
2015-09-24 16:34:16 +00:00
|
|
|
:note_events,
|
2015-12-07 12:23:23 +00:00
|
|
|
:build_events,
|
2016-08-02 10:06:31 +00:00
|
|
|
:pipeline_events,
|
2016-08-23 18:54:44 +00:00
|
|
|
:wiki_page_events,
|
2015-09-24 16:34:16 +00:00
|
|
|
:enable_ssl_verification
|
2014-09-19 08:23:18 +00:00
|
|
|
]
|
2013-12-04 11:35:38 +00:00
|
|
|
@hook = user_project.hooks.new(attrs)
|
2013-06-06 09:37:51 +00:00
|
|
|
|
|
|
|
if @hook.save
|
2013-12-04 11:19:53 +00:00
|
|
|
present @hook, with: Entities::ProjectHook
|
2013-06-06 09:37:51 +00:00
|
|
|
else
|
|
|
|
if @hook.errors[:url].present?
|
|
|
|
error!("Invalid url given", 422)
|
|
|
|
end
|
2014-12-30 14:17:46 +00:00
|
|
|
not_found!("Project hook #{@hook.errors.messages}")
|
2013-06-06 09:37:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update an existing project hook
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# hook_id (required) - The ID of a project hook
|
|
|
|
# url (required) - The hook URL
|
|
|
|
# Example Request:
|
|
|
|
# PUT /projects/:id/hooks/:hook_id
|
|
|
|
put ":id/hooks/:hook_id" do
|
|
|
|
@hook = user_project.hooks.find(params[:hook_id])
|
|
|
|
required_attributes! [:url]
|
2014-09-19 08:23:18 +00:00
|
|
|
attrs = attributes_for_keys [
|
|
|
|
:url,
|
|
|
|
:push_events,
|
|
|
|
:issues_events,
|
|
|
|
:merge_requests_events,
|
2015-05-16 06:33:31 +00:00
|
|
|
:tag_push_events,
|
2015-09-24 16:34:16 +00:00
|
|
|
:note_events,
|
2015-12-07 12:23:23 +00:00
|
|
|
:build_events,
|
2016-08-02 10:06:31 +00:00
|
|
|
:pipeline_events,
|
2016-08-23 18:54:44 +00:00
|
|
|
:wiki_page_events,
|
2015-09-24 16:34:16 +00:00
|
|
|
:enable_ssl_verification
|
2014-09-19 08:23:18 +00:00
|
|
|
]
|
2013-06-06 09:37:51 +00:00
|
|
|
|
|
|
|
if @hook.update_attributes attrs
|
2013-12-04 11:19:53 +00:00
|
|
|
present @hook, with: Entities::ProjectHook
|
2013-06-06 09:37:51 +00:00
|
|
|
else
|
|
|
|
if @hook.errors[:url].present?
|
|
|
|
error!("Invalid url given", 422)
|
|
|
|
end
|
2014-12-30 14:17:46 +00:00
|
|
|
not_found!("Project hook #{@hook.errors.messages}")
|
2013-06-06 09:37:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Deletes project hook. This is an idempotent function.
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# hook_id (required) - The ID of hook to delete
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /projects/:id/hooks/:hook_id
|
|
|
|
delete ":id/hooks/:hook_id" do
|
|
|
|
required_attributes! [:hook_id]
|
|
|
|
|
|
|
|
begin
|
2016-04-25 05:39:18 +00:00
|
|
|
@hook = user_project.hooks.destroy(params[:hook_id])
|
2013-06-06 09:37:51 +00:00
|
|
|
rescue
|
|
|
|
# ProjectHook can raise Error if hook_id not found
|
2016-04-25 05:39:18 +00:00
|
|
|
not_found!("Error deleting hook #{params[:hook_id]}")
|
2013-06-06 09:37:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|