2013-06-06 05:37:51 -04:00
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class ProjectHooks < Grape::API
|
|
|
|
before { authenticate! }
|
2013-09-30 09:49:55 -04:00
|
|
|
before { authorize_admin_project }
|
2013-06-06 05:37:51 -04: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 06:19:53 -05:00
|
|
|
present @hooks, with: Entities::ProjectHook
|
2013-06-06 05:37:51 -04: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 06:19:53 -05:00
|
|
|
present @hook, with: Entities::ProjectHook
|
2013-06-06 05:37:51 -04: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 04:23:18 -04:00
|
|
|
attrs = attributes_for_keys [
|
|
|
|
:url,
|
|
|
|
:push_events,
|
|
|
|
:issues_events,
|
|
|
|
:merge_requests_events,
|
2015-05-16 02:33:31 -04:00
|
|
|
:tag_push_events,
|
|
|
|
:note_events
|
2014-09-19 04:23:18 -04:00
|
|
|
]
|
2013-12-04 06:35:38 -05:00
|
|
|
@hook = user_project.hooks.new(attrs)
|
2013-06-06 05:37:51 -04:00
|
|
|
|
|
|
|
if @hook.save
|
2013-12-04 06:19:53 -05:00
|
|
|
present @hook, with: Entities::ProjectHook
|
2013-06-06 05:37:51 -04:00
|
|
|
else
|
|
|
|
if @hook.errors[:url].present?
|
|
|
|
error!("Invalid url given", 422)
|
|
|
|
end
|
2014-12-30 09:17:46 -05:00
|
|
|
not_found!("Project hook #{@hook.errors.messages}")
|
2013-06-06 05:37:51 -04: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 04:23:18 -04:00
|
|
|
attrs = attributes_for_keys [
|
|
|
|
:url,
|
|
|
|
:push_events,
|
|
|
|
:issues_events,
|
|
|
|
:merge_requests_events,
|
2015-05-16 02:33:31 -04:00
|
|
|
:tag_push_events,
|
|
|
|
:note_events
|
2014-09-19 04:23:18 -04:00
|
|
|
]
|
2013-06-06 05:37:51 -04:00
|
|
|
|
|
|
|
if @hook.update_attributes attrs
|
2013-12-04 06:19:53 -05:00
|
|
|
present @hook, with: Entities::ProjectHook
|
2013-06-06 05:37:51 -04:00
|
|
|
else
|
|
|
|
if @hook.errors[:url].present?
|
|
|
|
error!("Invalid url given", 422)
|
|
|
|
end
|
2014-12-30 09:17:46 -05:00
|
|
|
not_found!("Project hook #{@hook.errors.messages}")
|
2013-06-06 05:37:51 -04: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
|
|
|
|
@hook = ProjectHook.find(params[:hook_id])
|
|
|
|
@hook.destroy
|
|
|
|
rescue
|
|
|
|
# ProjectHook can raise Error if hook_id not found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|