From 6c0ae72ccc75d24dcda75fdf7bce0874c4fe1a4a Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 3 Nov 2014 13:47:48 +0100 Subject: [PATCH] Add project git hooks api. --- lib/api/api.rb | 1 + lib/api/entities.rb | 5 +++ lib/api/project_git_hooks.rb | 76 ++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 lib/api/project_git_hooks.rb diff --git a/lib/api/api.rb b/lib/api/api.rb index 22eddae098..76fea17541 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -41,6 +41,7 @@ module API mount ProjectMembers mount DeployKeys mount ProjectHooks + mount ProjectGitHooks mount Ldap mount Services mount Files diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 61466a969d..ce3212eda9 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -34,6 +34,11 @@ module API expose :issues_events, :merge_requests_events, :tag_push_events end + class ProjectGitHook < Grape::Entity + expose :id, :project_id, :created_at + expose :commit_message_regex, :deny_delete_tag + end + class ForkedFromProject < Grape::Entity expose :id expose :name, :name_with_namespace diff --git a/lib/api/project_git_hooks.rb b/lib/api/project_git_hooks.rb new file mode 100644 index 0000000000..31ad2ae264 --- /dev/null +++ b/lib/api/project_git_hooks.rb @@ -0,0 +1,76 @@ +module API + # Projects git hook API + class ProjectGitHooks < Grape::API + before { authenticate! } + before { authorize_admin_project } + + resource :projects do + # Get project git hook + # + # Parameters: + # id (required) - The ID of a project + # Example Request: + # GET /projects/:id/git_hook + get ":id/git_hook" do + @git_hooks = user_project.git_hook + present @git_hooks, with: Entities::ProjectGitHook + end + + # Add git hook to project + # + # Parameters: + # id (required) - The ID of a project + # Example Request: + # POST /projects/:id/git_hook + post ":id/git_hook" do + attrs = attributes_for_keys [ + :commit_message_regex, + :deny_delete_tag + ] + + if user_project.git_hook + error!("Project git hook exists", 422) + else + @git_hook = user_project.create_git_hook(attrs) + present @git_hook, with: Entities::ProjectGitHook + end + end + + # Update an existing project git hook + # + # Parameters: + # id (required) - The ID of a project + # Example Request: + # PUT /projects/:id/git_hook + put ":id/git_hook" do + @git_hook = user_project.git_hook + + attrs = attributes_for_keys [ + :commit_message_regex, + :deny_delete_tag + ] + + if @git_hook.update_attributes attrs + present @git_hook, with: Entities::ProjectGitHook + else + not_found! + end + end + + # Deletes project git hook. This is an idempotent function. + # + # Parameters: + # id (required) - The ID of a project + # Example Request: + # DELETE /projects/:id/git_hook + delete ":id/git_hook" do + @git_hook = user_project.git_hook + if @git_hook + @git_hook.destroy + else + not_found! + end + end + end + end +end