2014-09-02 18:07:02 +05:30
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class ProjectHooks < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
before { authorize_admin_project }
|
|
|
|
|
|
|
|
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
|
|
|
|
present @hooks, with: Entities::ProjectHook
|
|
|
|
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])
|
|
|
|
present @hook, with: Entities::ProjectHook
|
|
|
|
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]
|
2015-04-26 12:48:37 +05:30
|
|
|
attrs = attributes_for_keys [
|
|
|
|
:url,
|
|
|
|
:push_events,
|
|
|
|
:issues_events,
|
|
|
|
:merge_requests_events,
|
2015-09-11 14:41:01 +05:30
|
|
|
:tag_push_events,
|
2015-10-24 18:46:33 +05:30
|
|
|
:note_events,
|
2015-12-23 02:04:40 +05:30
|
|
|
:build_events,
|
2015-10-24 18:46:33 +05:30
|
|
|
:enable_ssl_verification
|
2015-04-26 12:48:37 +05:30
|
|
|
]
|
2014-09-02 18:07:02 +05:30
|
|
|
@hook = user_project.hooks.new(attrs)
|
|
|
|
|
|
|
|
if @hook.save
|
|
|
|
present @hook, with: Entities::ProjectHook
|
|
|
|
else
|
|
|
|
if @hook.errors[:url].present?
|
|
|
|
error!("Invalid url given", 422)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
not_found!("Project hook #{@hook.errors.messages}")
|
2014-09-02 18:07:02 +05:30
|
|
|
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]
|
2015-04-26 12:48:37 +05:30
|
|
|
attrs = attributes_for_keys [
|
|
|
|
:url,
|
|
|
|
:push_events,
|
|
|
|
:issues_events,
|
|
|
|
:merge_requests_events,
|
2015-09-11 14:41:01 +05:30
|
|
|
:tag_push_events,
|
2015-10-24 18:46:33 +05:30
|
|
|
:note_events,
|
2015-12-23 02:04:40 +05:30
|
|
|
:build_events,
|
2015-10-24 18:46:33 +05:30
|
|
|
:enable_ssl_verification
|
2015-04-26 12:48:37 +05:30
|
|
|
]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
if @hook.update_attributes attrs
|
|
|
|
present @hook, with: Entities::ProjectHook
|
|
|
|
else
|
|
|
|
if @hook.errors[:url].present?
|
|
|
|
error!("Invalid url given", 422)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
not_found!("Project hook #{@hook.errors.messages}")
|
2014-09-02 18:07:02 +05:30
|
|
|
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-06-02 11:05:42 +05:30
|
|
|
@hook = user_project.hooks.destroy(params[:hook_id])
|
2014-09-02 18:07:02 +05:30
|
|
|
rescue
|
|
|
|
# ProjectHook can raise Error if hook_id not found
|
2016-06-02 11:05:42 +05:30
|
|
|
not_found!("Error deleting hook #{params[:hook_id]}")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|