Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/projects/mattermost_controller.rb19
-rw-r--r--app/models/project_services/mattermost_slash_commands_service.rb25
-rw-r--r--config/routes/project.rb6
-rw-r--r--lib/mattermost/command.rb17
-rw-r--r--lib/mattermost/session.rb4
-rw-r--r--lib/mattermost/team.rb20
-rw-r--r--spec/fixtures/mattermost_new_command.json1
-rw-r--r--spec/lib/mattermost/command_spec.rb20
-rw-r--r--spec/lib/mattermost/session_spec.rb2
-rw-r--r--spec/lib/mattermost/team_spec.rb18
-rw-r--r--spec/models/project_services/mattermost_slash_commands_service_spec.rb35
11 files changed, 91 insertions, 76 deletions
diff --git a/app/controllers/projects/mattermost_controller.rb b/app/controllers/projects/mattermost_controller.rb
index 1759d21e84f..a0eaec262ee 100644
--- a/app/controllers/projects/mattermost_controller.rb
+++ b/app/controllers/projects/mattermost_controller.rb
@@ -7,12 +7,13 @@ class Projects::MattermostController < Projects::ApplicationController
def new
end
- def configure
- @service.configure(host, current_user, configure_params)
+ def create
+ message = @service.configure(current_user, configure_params)
+ notice = message.is_a?(String) ? message : 'This service is now configured'
redirect_to(
new_namespace_project_mattermost_path(@project.namespace, @project),
- notice: 'This service is now configured.'
+ notice: notice
)
rescue NoSessionError
redirect_to(
@@ -24,7 +25,8 @@ class Projects::MattermostController < Projects::ApplicationController
private
def configure_params
- params.permit(:trigger, :team_id).merge(url: service_trigger_url(@service), icon_url: asset_url('gitlab_logo.png'))
+ params.permit(:trigger, :team_id).
+ merge(url: service_trigger_url(@service), icon_url: asset_url('gitlab_logo.png'))
end
def service
@@ -32,13 +34,6 @@ class Projects::MattermostController < Projects::ApplicationController
end
def teams
- @teams =
- begin
- Mattermost::Mattermost.new(Gitlab.config.mattermost.host, current_user).with_session do
- Mattermost::Team.team_admin
- end
- rescue
- []
- end
+ @teams = @service.list_teams(current_user)
end
end
diff --git a/app/models/project_services/mattermost_slash_commands_service.rb b/app/models/project_services/mattermost_slash_commands_service.rb
index 5dfc4cc2744..6fdcb770593 100644
--- a/app/models/project_services/mattermost_slash_commands_service.rb
+++ b/app/models/project_services/mattermost_slash_commands_service.rb
@@ -25,12 +25,29 @@ class MattermostSlashCommandsService < ChatService
]
end
- def configure(host, current_user, params)
- new_token = Mattermost::Session.new(host, current_user).with_session do
- Mattermost::Command.create(params[:team_id], command)
+ def configure(current_user, params)
+ result = Mattermost::Session.new(current_user).with_session do |session|
+ Mattermost::Command.create(session, params[:team_id], command)
end
- update!(token: new_token, active: true)
+ if result.has_key?('message')
+ result['message']
+ else
+ update!(token: result['token'], active: true)
+ end
+ end
+
+ def list_teams
+ begin
+ response = Mattermost::Mattermost.new(current_user).with_session do |session|
+ Mattermost::Team.teams(session)
+ end
+
+ # We ignore the error message as we can't display it
+ response.has_key?('message') ? [] : response
+ rescue Mattermost::NoSessionError
+ []
+ end
end
def trigger(params)
diff --git a/config/routes/project.rb b/config/routes/project.rb
index 23d85368f1b..b42c5e5211c 100644
--- a/config/routes/project.rb
+++ b/config/routes/project.rb
@@ -76,11 +76,7 @@ constraints(ProjectUrlConstrainer.new) do
end
end
- resources :mattermost, only: [:new] do
- collection do
- post :configure
- end
- end
+ resources :mattermost, only: [:new, :create]
resources :deploy_keys, constraints: { id: /\d+/ }, only: [:index, :new, :create] do
member do
diff --git a/lib/mattermost/command.rb b/lib/mattermost/command.rb
index 9c37d0b0d79..afbf2ce3349 100644
--- a/lib/mattermost/command.rb
+++ b/lib/mattermost/command.rb
@@ -1,14 +1,13 @@
module Mattermost
- class Command < Session
- def self.create(team_id, trigger: 'gitlab', url:, icon_url:)
+ class Command
+ def self.create(session, team_id, command)
+ response = session.post("/api/v3/teams/#{team_id}/commands/create", body: command.to_json).parsed_response
- post_command(command)['token']
- end
-
- private
-
- def post_command(command)
- post( "/teams/#{team_id}/commands/create", body: command.to_json).parsed_response
+ if response.has_key?('message')
+ response
+ else
+ response['token']
+ end
end
end
end
diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb
index dcdff94814c..670f83bb6bc 100644
--- a/lib/mattermost/session.rb
+++ b/lib/mattermost/session.rb
@@ -30,6 +30,8 @@ module Mattermost
begin
yield self
+ rescue Errno::ECONNREFUSED
+ raise NoSessionError
ensure
destroy
end
@@ -112,4 +114,4 @@ module Mattermost
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/mattermost/team.rb b/lib/mattermost/team.rb
index 714748aea3c..c1b867629b6 100644
--- a/lib/mattermost/team.rb
+++ b/lib/mattermost/team.rb
@@ -1,21 +1,13 @@
module Mattermost
- class Team < Session
- def self.team_admin
- return [] unless initial_load['team_members']
+ class Team
+ def self.all(session)
+ response_body = retreive_teams(session)
- team_ids = initial_load['team_members'].map do |team|
- team['team_id'] if team['roles'].split.include?('team_admin')
- end.compact
-
- initial_load['teams'].select do |team|
- team_ids.include?(team['id'])
- end
+ response_body.has_key?('message') ? response_body : response_body.values
end
- private
-
- def initial_load
- @initial_load ||= get('/users/initial_load').parsed_response
+ def self.retreive_teams(session)
+ session.get('/api/v3/teams/all').parsed_response
end
end
end
diff --git a/spec/fixtures/mattermost_new_command.json b/spec/fixtures/mattermost_new_command.json
deleted file mode 100644
index 4b827f19926..00000000000
--- a/spec/fixtures/mattermost_new_command.json
+++ /dev/null
@@ -1 +0,0 @@
-{"id":"y8j1nexrdirj5nubq5uzdwwidr","token":"pzajm5hfbtni3r49ujpt8betpc","create_at":1481897117122,"update_at":1481897117122,"delete_at":0,"creator_id":"78nm4euoc7dypergdc13ekxgpo","team_id":"w59qt5a817f69jkxdz6xe7y4ir","trigger":"display","method":"P","username":"GitLab","icon_url":"","auto_complete":false,"auto_complete_desc":"","auto_complete_hint":"","display_name":"Display name","description":"the description","url":"http://trigger.url/trigger"}
diff --git a/spec/lib/mattermost/command_spec.rb b/spec/lib/mattermost/command_spec.rb
index 7c6457f639d..bc2e47ebbc9 100644
--- a/spec/lib/mattermost/command_spec.rb
+++ b/spec/lib/mattermost/command_spec.rb
@@ -1,17 +1,25 @@
require 'spec_helper'
describe Mattermost::Command do
+ let(:session) { double("session") }
+ let(:hash) { { 'token' => 'token' } }
+
describe '.create' do
- let(:new_command) do
- JSON.parse(File.read(Rails.root.join('spec/fixtures/', 'mattermost_new_command.json')))
+ before do
+ allow(session).to receive(:post).and_return(hash)
+ allow(hash).to receive(:parsed_response).and_return(hash)
end
- it 'gets the teams' do
- allow(described_class).to receive(:post_command).and_return(new_command)
+ context 'with access' do
+ it 'gets the teams' do
+ expect(session).to receive(:post)
+
+ described_class.create(session, 'abc', url: 'http://trigger.com')
+ end
+ end
- token = described_class.create('abc', url: 'http://trigger.url/trigger', icon_url: 'http://myicon.com/icon.png')
+ context 'on an error' do
- expect(token).to eq('pzajm5hfbtni3r49ujpt8betpc')
end
end
end
diff --git a/spec/lib/mattermost/session_spec.rb b/spec/lib/mattermost/session_spec.rb
index 752ac796b1c..3c2eddbd221 100644
--- a/spec/lib/mattermost/session_spec.rb
+++ b/spec/lib/mattermost/session_spec.rb
@@ -96,4 +96,4 @@ describe Mattermost::Session, type: :request do
end
end
end
-end \ No newline at end of file
+end
diff --git a/spec/lib/mattermost/team_spec.rb b/spec/lib/mattermost/team_spec.rb
index 0fe6163900d..32a0dbf42ec 100644
--- a/spec/lib/mattermost/team_spec.rb
+++ b/spec/lib/mattermost/team_spec.rb
@@ -2,20 +2,22 @@ require 'spec_helper'
describe Mattermost::Team do
describe '.team_admin' do
- let(:init_load) do
- JSON.parse(File.read(Rails.root.join('spec/fixtures/', 'mattermost_initial_load.json')))
- end
+ let(:session) { double("session") }
+ # TODO fix fixture
+ let(:json) { File.read(Rails.root.join('spec/fixtures/', 'mattermost_initial_load.json')) }
+ let(:parsed_response) { JSON.parse(json) }
before do
- allow(described_class).to receive(:initial_load).and_return(init_load)
+ allow(session).to receive(:get).with('/api/v3/teams/all').
+ and_return(json)
+ allow(json).to receive(:parsed_response).and_return(parsed_response)
end
- it 'gets the teams' do
- expect(described_class.team_admin.count).to be(2)
+ xit 'gets the teams' do
+ expect(described_class.all(session).count).to be(2)
end
- it 'filters on being team admin' do
- ids = described_class.team_admin.map { |team| team['id'] }
+ xit 'filters on being team admin' do
expect(ids).to include("w59qt5a817f69jkxdz6xe7y4ir", "my9oujxf5jy1zqdgu9rihd66do")
end
end
diff --git a/spec/models/project_services/mattermost_slash_commands_service_spec.rb b/spec/models/project_services/mattermost_slash_commands_service_spec.rb
index 43b2c2c1302..00018624d96 100644
--- a/spec/models/project_services/mattermost_slash_commands_service_spec.rb
+++ b/spec/models/project_services/mattermost_slash_commands_service_spec.rb
@@ -102,29 +102,34 @@ describe MattermostSlashCommandsService, models: true do
let(:service) { project.build_mattermost_slash_commands_service }
subject do
- service.configure('http://localhost:8065', nil, team_id: 'abc', trigger: 'gitlab', url: 'http://trigger.url', icon_url: 'http://icon.url/icon.png')
+ service.configure('http://localhost:8065', team_id: 'abc', trigger: 'gitlab', url: 'http://trigger.url', icon_url: 'http://icon.url/icon.png')
end
- it 'creates a new Mattermost session' do
- expect_any_instance_of(Mattermost::Session).to receive(:with_session)
+ context 'the requests succeeds' do
+ before do
+ allow_any_instance_of(Mattermost::Session).to receive(:with_session).
+ and_return('token' => 'mynewtoken')
+ end
- subject
- end
+ it 'saves the service' do
+ expect_any_instance_of(Mattermost::Session).to receive(:with_session)
+ expect { subject }.to change { project.services.count }.by(1)
+ end
- it 'saves the service' do
- allow_any_instance_of(Mattermost::Session).to receive(:with_session).
- and_return('mynewtoken')
+ it 'saves the token' do
+ subject
- expect { subject }.to change { project.services.count }.by(1)
+ expect(service.reload.token).to eq('mynewtoken')
+ end
end
- it 'saves the token' do
- allow_any_instance_of(Mattermost::Session).to receive(:with_session).
- and_return('mynewtoken')
-
- subject
+ context 'an error is received' do
+ it 'shows error messages' do
+ allow_any_instance_of(Mattermost::Session).to receive(:with_session).
+ and_return('token' => 'mynewtoken', 'message' => "Error")
- expect(service.reload.token).to eq('mynewtoken')
+ expect(subject).to eq("Error")
+ end
end
end
end