From d636ad49bfba59499e45b445ca7e137e83613d8b Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Tue, 29 Oct 2013 16:39:46 +0200
Subject: [PATCH] API: set gitlab-ci service for project

---
 CHANGELOG                          |  1 +
 app/models/hipchat_service.rb      |  1 -
 lib/api/api.rb                     |  1 +
 lib/api/deploy_keys.rb             | 10 --------
 lib/api/services.rb                | 40 ++++++++++++++++++++++++++++++
 spec/requests/api/services_spec.rb | 33 ++++++++++++++++++++++++
 6 files changed, 75 insertions(+), 11 deletions(-)
 create mode 100644 lib/api/services.rb
 create mode 100644 spec/requests/api/services_spec.rb

diff --git a/CHANGELOG b/CHANGELOG
index 02e1627c44d..c60bbc3c88c 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,4 +1,5 @@
 v 6.3.0
+  - API for adding gitlab-ci service
   - Init script now waits for pids to appear after (re)starting before reporting status (Rovanion Luckey)
   - Restyle project home page
   - Grammar fixes
diff --git a/app/models/hipchat_service.rb b/app/models/hipchat_service.rb
index c3fb4826334..7fec5c4fbe8 100644
--- a/app/models/hipchat_service.rb
+++ b/app/models/hipchat_service.rb
@@ -71,5 +71,4 @@ class HipchatService < Service
 
     message
   end
-
 end
diff --git a/lib/api/api.rb b/lib/api/api.rb
index c4c9f166db1..4db81f42b4c 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -38,5 +38,6 @@ module API
     mount ProjectSnippets
     mount DeployKeys
     mount ProjectHooks
+    mount Services
   end
 end
diff --git a/lib/api/deploy_keys.rb b/lib/api/deploy_keys.rb
index 218b3d8eee2..b5997608997 100644
--- a/lib/api/deploy_keys.rb
+++ b/lib/api/deploy_keys.rb
@@ -5,16 +5,6 @@ module API
     before { authorize_admin_project }
 
     resource :projects do
-      helpers do
-        def handle_project_member_errors(errors)
-          if errors[:project_access].any?
-            error!(errors[:project_access], 422)
-          end
-          not_found!
-        end
-      end
-
-
       # Get a specific project's keys
       #
       # Example Request:
diff --git a/lib/api/services.rb b/lib/api/services.rb
new file mode 100644
index 00000000000..d562b9484c5
--- /dev/null
+++ b/lib/api/services.rb
@@ -0,0 +1,40 @@
+module API
+  # Projects API
+  class Services < Grape::API
+    before { authenticate! }
+    before { authorize_admin_project }
+
+    resource :projects do
+      # Set GitLab CI service for project
+      #
+      # Parameters:
+      #   token (required) - CI project token
+      #   project_url (required) - CI project url
+      #
+      # Example Request:
+      #   PUT /projects/:id/services/gitlab-ci
+      put ":id/services/gitlab-ci" do
+        required_attributes! [:token, :project_url]
+        attrs = attributes_for_keys [:token, :project_url]
+        user_project.build_missing_services
+
+        if user_project.gitlab_ci_service.update_attributes(attrs.merge(active: true))
+          true
+        else
+          not_found!
+        end
+      end
+
+      # Delete GitLab CI service settings
+      #
+      # Example Request:
+      #   DELETE /projects/:id/keys/:id
+      delete ":id/services/gitlab-ci" do
+        if user_project.gitlab_ci_service
+          user_project.gitlab_ci_service.destroy
+        end
+      end
+    end
+  end
+end
+
diff --git a/spec/requests/api/services_spec.rb b/spec/requests/api/services_spec.rb
new file mode 100644
index 00000000000..e2fd945bad3
--- /dev/null
+++ b/spec/requests/api/services_spec.rb
@@ -0,0 +1,33 @@
+require "spec_helper"
+
+describe API::API do
+  include ApiHelpers
+  before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
+  after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
+
+  let(:user) { create(:user) }
+  let(:project) {create(:project_with_code, creator_id: user.id, namespace: user.namespace) }
+
+  describe "POST /projects/:id/services/gitlab-ci" do
+    it "should update gitlab-ci settings" do
+      put api("/projects/#{project.id}/services/gitlab-ci", user), token: 'secret-token', project_url: "http://ci.example.com/projects/1"
+
+      response.status.should == 200
+    end
+
+    it "should return if required fields missing" do
+      put api("/projects/#{project.id}/services/gitlab-ci", user), project_url: "http://ci.example.com/projects/1", active: true
+
+      response.status.should == 400
+    end
+  end
+
+  describe "DELETE /projects/:id/services/gitlab-ci" do
+    it "should update gitlab-ci settings" do
+      delete api("/projects/#{project.id}/services/gitlab-ci", user)
+
+      response.status.should == 200
+      project.gitlab_ci_service.should be_nil
+    end
+  end
+end
-- 
GitLab