From e663725961de66ac838d0a5a85978656938e74f4 Mon Sep 17 00:00:00 2001
From: Kamil Trzcinski <ayufan@ayufan.eu>
Date: Fri, 16 Dec 2016 12:20:42 +0100
Subject: [PATCH] Store mattermost_url in settings

---
 config/gitlab.yml.example           |  6 ++++++
 config/initializers/1_settings.rb   |  7 +++++++
 lib/mattermost/session.rb           | 17 +++++++++--------
 spec/lib/mattermost/session_spec.rb | 18 +++++++++++++-----
 4 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example
index 327e4a7937c0..b8b41a0d86c4 100644
--- a/config/gitlab.yml.example
+++ b/config/gitlab.yml.example
@@ -153,6 +153,12 @@ production: &base
     # The location where LFS objects are stored (default: shared/lfs-objects).
     # storage_path: shared/lfs-objects
 
+  ## Mattermost
+  ## For enabling Add to Mattermost button
+  mattermost:
+    enabled: false
+    host: 'https://mattermost.example.com'
+
   ## Gravatar
   ## For Libravatar see: http://doc.gitlab.com/ce/customization/libravatar.html
   gravatar:
diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb
index 0ee1b1ec6344..45404e579ae1 100644
--- a/config/initializers/1_settings.rb
+++ b/config/initializers/1_settings.rb
@@ -261,6 +261,13 @@ def host(url)
 Settings.lfs['enabled']      = true if Settings.lfs['enabled'].nil?
 Settings.lfs['storage_path'] = File.expand_path(Settings.lfs['storage_path'] || File.join(Settings.shared['path'], "lfs-objects"), Rails.root)
 
+#
+# Mattermost
+#
+Settings['mattermost'] ||= Settingslogic.new({})
+Settings.mattermost['enabled']      = false if Settings.mattermost['enabled'].nil?
+Settings.mattermost['host']         = nil unless Settings.mattermost.enabled
+
 #
 # Gravatar
 #
diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb
index 7d0290be5a17..a3715bed4827 100644
--- a/lib/mattermost/session.rb
+++ b/lib/mattermost/session.rb
@@ -17,12 +17,11 @@ class Session
     include Doorkeeper::Helpers::Controller
     include HTTParty
 
-    attr_accessor :current_resource_owner, :token
+    base_uri Settings.mattermost.host
 
-    def initialize(uri, current_user)
-      # Sets the base uri for HTTParty, so we can use paths
-      self.class.base_uri(uri)
+    attr_accessor :current_resource_owner, :token
 
+    def initialize(current_user)
       @current_resource_owner = current_user
     end
 
@@ -30,7 +29,7 @@ def with_session
       raise NoSessionError unless create
 
       begin
-        yield
+        yield self
       ensure
         destroy
       end
@@ -65,7 +64,9 @@ def create
       return unless token_uri
 
       self.token = request_token
-      self.class.headers("Cookie" => "MMAUTHTOKEN=#{self.token}")
+      @headers = {
+        "Authorization": "Bearer #{self.token}"
+      }
       self.token
     end
 
@@ -98,11 +99,11 @@ def request_token
     end
 
     def get(path, options = {})
-      self.class.get(path, options)
+      self.class.get(path, options.merge(headers: @headers))
     end
 
     def post(path, options = {})
-      self.class.post(path, options)
+      self.class.post(path, options.merge(headers: @headers))
     end
   end
 end
diff --git a/spec/lib/mattermost/session_spec.rb b/spec/lib/mattermost/session_spec.rb
index 69d677930bc8..3c2eddbd2210 100644
--- a/spec/lib/mattermost/session_spec.rb
+++ b/spec/lib/mattermost/session_spec.rb
@@ -6,7 +6,7 @@
   let(:gitlab_url) { "http://gitlab.com" }
   let(:mattermost_url) { "http://mattermost.com" }
 
-  subject { described_class.new(mattermost_url, user) }
+  subject { described_class.new(user) }
 
   # Needed for doorkeeper to function
   it { is_expected.to respond_to(:current_resource_owner) }
@@ -14,6 +14,10 @@
   it { is_expected.to respond_to(:authorization) }
   it { is_expected.to respond_to(:strategy) }
 
+  before do
+    described_class.base_uri(mattermost_url)
+  end
+
   describe '#with session' do
     let(:location) { 'http://location.tld' }
     let!(:stub) do
@@ -72,18 +76,22 @@
             end
 
           WebMock.stub_request(:post, "#{mattermost_url}/api/v3/users/logout").
-            to_return(headers: { Cookie: 'MMAUTHTOKEN=thisworksnow' }, status: 200)
+            to_return(headers: { Authorization: 'token thisworksnow' }, status: 200)
         end
 
         it 'can setup a session' do
-          subject.with_session { 1 + 1 }
+          subject.with_session do |session|
+          end
+
           expect(subject.token).not_to be_nil
         end
 
         it 'returns the value of the block' do
-          value = subject.with_session { 1 + 1 }
+          result = subject.with_session do |session|
+            "value"
+          end
 
-          expect(value).to be(2)
+          expect(result).to eq("value")
         end
       end
     end
-- 
GitLab