Commit 61d09a7b authored by Kamil Trzcinski's avatar Kamil Trzcinski

WIP

parent 0cf23fde
......@@ -12,13 +12,17 @@ class Projects::MattermostsController < Projects::ApplicationController
end
def create
@service.configure!(current_user, configure_params)
result, message = @service.configure(current_user, configure_params)
if result
flash[:notice] = 'This service is now configured'
redirect_to edit_namespace_project_service_path(@project.namespace, @project, service)
rescue => e
flash[:alert] = e.message
redirect_to new_namespace_project_mattermost_path(@project.namespace, @project)
redirect_to edit_namespace_project_service_path(
@project.namespace, @project, service)
else
flash[:alert] = message || 'Failed to configure service'
redirect_to new_namespace_project_mattermost_path(
@project.namespace, @project)
end
end
private
......@@ -31,9 +35,6 @@ class Projects::MattermostsController < Projects::ApplicationController
def teams
@teams ||= @service.list_teams(current_user)
rescue => e
@teams = []
flash[:alert] = e.message
end
def service
......
......@@ -294,8 +294,4 @@ module ApplicationHelper
def page_class
"issue-boards-page" if current_controller?(:boards)
end
def pretty_url(url)
url.gsub(/\A.*?:\/\//, '')
end
end
......@@ -148,14 +148,6 @@ module ProjectsHelper
).html_safe
end
def mattermost_teams_options(teams)
teams_options = teams.map do |id, options|
[options['display_name'] || options['name'], id]
end
teams_options.compact.unshift(['Select team...', '0'])
end
private
def repo_children_classes(field)
......
......@@ -19,15 +19,19 @@ class MattermostSlashCommandsService < ChatSlashCommandsService
'mattermost_slash_commands'
end
def configure!(user, params)
def configure(user, params)
token = Mattermost::Command.new(user).
create(command(params))
update!(active: true, token: token)
update(active: true, token: token) if token
rescue => Mattermost::Error => e
false, e.message
end
def list_teams(user)
Mattermost::Team.new(user).all
rescue => Mattermost::Error => e
[]
end
private
......
......@@ -29,13 +29,13 @@ module Mattermost
def json_response(response)
json_response = JSON.parse(response.body)
if response.success?
json_response
elsif json_response['message']
raise ClientError(json_response['message'])
else
raise ClientError('Undefined error')
unless response.success?
raise ClientError(json_response['message'] || 'Undefined error')
end
json_response
rescue JSON::JSONError => e
raise ClientError('Cannot parse response')
end
end
end
......@@ -3,15 +3,11 @@ module Mattermost
class NoSessionError < Error
def message
'No session could be set up, is Mattermost configured with Single Sign on?'
'No session could be set up, is Mattermost configured with Single Sign On?'
end
end
class ConnectionError < Error
def message
'Could not connect. Is Mattermost up?'
end
end
class ConnectionError < Error; end
# This class' prime objective is to obtain a session token on a Mattermost
# instance with SSO configured where this GitLab instance is the provider.
......@@ -74,12 +70,16 @@ module Mattermost
def get(path, options = {})
self.class.get(path, options.merge(headers: @headers))
rescue Errno::ECONNREFUSED
raise ConnectionError
rescue HTTParty::Error => e
raise ConnectionError(e.message)
rescue Errno::ECONNREFUSED => e
raise ConnectionError(e.message)
end
def post(path, options = {})
self.class.post(path, options.merge(headers: @headers))
rescue HTTParty::Error => e
raise ConnectionError(e.message)
rescue Errno::ECONNREFUSED
raise ConnectionError
end
......
require 'spec_helper'
describe Mattermost::Command do
let(:session) { double("session") }
let(:hash) { { 'token' => 'token' } }
let(:user) { create(:user) }
describe '.create' do
before do
allow(session).to receive(:post).and_return(hash)
allow(hash).to receive(:parsed_response).and_return(hash)
Mattermost::Session.base_uri("http://mattermost.example.com")
end
it 'gets the teams' do
expect(session).to receive(:post)
described_class.create(session, 'abc', url: 'http://trigger.com')
describe '#create' do
it 'creates a command' do
described_class.new(user).
create(team_id: 'abc', url: 'http://trigger.com')
end
end
end
require 'spec_helper'
describe Mattermost::Team do
describe '.team_admin' do
describe '#all' do
let(:session) { double("session") }
let(:response) do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment