diff options
author | Valery Sizov <valery@gitlab.com> | 2015-01-07 23:21:14 +0000 |
---|---|---|
committer | Valery Sizov <valery@gitlab.com> | 2015-01-07 23:21:14 +0000 |
commit | 7b92b470420bcdd522f1088d1852d3b46a926c92 (patch) | |
tree | a45549a8d30bc9cd8dd4243fdb854384351733fd | |
parent | a84c237eddf657f9ed090cd9a9e029d634defe40 (diff) | |
parent | 37b13b9f15570eecbb190429709a63775d60810f (diff) | |
download | gitlab-ci-7b92b470420bcdd522f1088d1852d3b46a926c92.tar.gz |
Merge branch 'api_for_webhooks_registering' into 'master'
API for webhooks registering
Simple API method for creating new WebHooks for specified Project.
P.S. Sorry for not squasing commits, they were already pushed.
See merge request !11
-rw-r--r-- | lib/api/entities.rb | 4 | ||||
-rw-r--r-- | lib/api/projects.rb | 24 | ||||
-rw-r--r-- | spec/requests/api/projects_spec.rb | 43 |
3 files changed, 71 insertions, 0 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 0748442..dfd419a 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -23,5 +23,9 @@ module API class RunnerProject < Grape::Entity expose :id, :project_id, :runner_id end + + class WebHook < Grape::Entity + expose :id, :project_id, :url + end end end diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 89faa04..632aa51 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -4,6 +4,30 @@ module API before { authenticate! } resource :projects do + # Register new webhook for project + # + # Parameters + # project_id (required) - The ID of a project + # web_hook (required) - WebHook URL + # Example Request + # POST /projects/:project_id/webhooks + post ":project_id/webhooks" do + required_attributes! [:web_hook] + + project = Project.find(params[:project_id]) + + if project.present? && current_user.can_access_project?(project.gitlab_id) + web_hook = project.web_hooks.new({url: params[:web_hook]}) + + if web_hook.save + present web_hook, :with => Entities::WebHook + else + errors = web_hook.errors.full_messages.join(", ") + render_api_error!(errors, 400) + end + end + end + # Retrieve all Gitlab CI projects that the user has access to # # Example Request: diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index bb2f1cb..2000162 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -52,6 +52,49 @@ describe API::API do end end + describe "POST /projects/:project_id/webhooks" do + let!(:project) { FactoryGirl.create(:project) } + + context "Valid Webhook URL" do + let!(:webhook) { {:web_hook => "http://example.com/sth/1/ala_ma_kota" } } + + before do + options.merge!(webhook) + end + + it "should create webhook for specified project" do + post api("/projects/#{project.id}/webhooks"), options + response.status.should == 201 + json_response["url"].should == webhook[:web_hook] + end + + it "fails to create webhook for non existsing project" do + post api("/projects/non-existant-id/webhooks"), options + response.status.should == 404 + end + + end + + context "Invalid Webhook URL" do + let!(:webhook) { {:web_hook => "ala_ma_kota" } } + + before do + options.merge!(webhook) + end + + it "fails to create webhook for not valid url" do + post api("/projects/#{project.id}/webhooks"), options + response.status.should == 400 + end + end + + context "Missed web_hook parameter" do + it "fails to create webhook for not provided url" do + post api("/projects/#{project.id}/webhooks"), options + response.status.should == 400 + end + end + end describe "GET /projects/:id" do let!(:project) { FactoryGirl.create(:project) } |