Commit 34295036 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Improve sources

- Add proper error handling,
- Use flash[:alert] and flash[:notice],
- Use `resource` instead of `resources`,
parent 921f411a
class Projects::MattermostController < Projects::ApplicationController class Projects::MattermostsController < Projects::ApplicationController
include TriggersHelper
include ActionView::Helpers::AssetUrlHelper
layout 'project_settings' layout 'project_settings'
before_action :authorize_admin_project! before_action :authorize_admin_project!
before_action :service before_action :service
before_action :teams, only: [:new] before_action :teams, only: [:new]
...@@ -8,32 +12,30 @@ class Projects::MattermostController < Projects::ApplicationController ...@@ -8,32 +12,30 @@ class Projects::MattermostController < Projects::ApplicationController
end end
def create def create
message = @service.configure(current_user, configure_params) @service.configure!(current_user, configure_params)
notice = message.is_a?(String) ? message : 'This service is now configured'
flash[:notice] = 'This service is now configured'
redirect_to( redirect_to edit_namespace_project_service_path(@project.namespace, @project, service)
new_namespace_project_mattermost_path(@project.namespace, @project), rescue => e
notice: notice flash[:alert] = e.message
) redirect_to new_namespace_project_mattermost_path(@project.namespace, @project)
rescue NoSessionError
redirect_to(
new_namespace_project_mattermost_path(@project.namespace, @project),
alert: 'No session could be set up, is Mattermost configured with Single Sign on?'
)
end end
private private
def configure_params def configure_params
params.permit(:trigger, :team_id). params.require(:mattermost).permit(:trigger, :team_id).merge(
merge(url: service_trigger_url(@service), icon_url: asset_url('gitlab_logo.png')) url: service_trigger_url(@service),
icon_url: asset_url('gitlab_logo.png'))
end end
def service def teams
@service ||= @project.find_or_initialize_service('mattermost_slash_commands') @teams ||= @service.list_teams(current_user)
rescue => e
flash[:alert] = e.message
end end
def teams def service
@teams = @service.list_teams(current_user) @service ||= @project.find_or_initialize_service('mattermost_slash_commands')
end end
end end
...@@ -149,12 +149,11 @@ module ProjectsHelper ...@@ -149,12 +149,11 @@ module ProjectsHelper
end end
def mattermost_teams_options(teams) def mattermost_teams_options(teams)
teams_options = teams.map do |team| teams_options = teams.map do |id, options|
return nil unless team['display_name'] && team['id'] return nil unless id && options['display_name']
[team['display_name'], team['id']] [options['display_name'], id]
end.compact end.compact
teams_options.unshift(['Select team...', '0']) unless teams_options.one? teams_options.unshift(['Select team...', '0'])
teams_options
end end
private private
......
...@@ -25,29 +25,18 @@ class MattermostSlashCommandsService < ChatService ...@@ -25,29 +25,18 @@ class MattermostSlashCommandsService < ChatService
] ]
end end
def configure(current_user, params) def configure!(current_user, params)
result = Mattermost::Session.new(current_user).with_session do |session| token = Mattermost::Session.new(current_user).with_session do |session|
Mattermost::Command.create(session, params[:team_id], command) Mattermost::Command.create(session, command(params))
end end
if result.has_key?('message') update!(active: true, token: token)
result['message']
else
update!(token: result['token'], active: true)
end
end end
def list_teams(current_user) def list_teams(user)
begin Mattermost::Session.new(user).with_session do |session|
response = Mattermost::Session.new(current_user).with_session do |session|
Mattermost::Team.all(session) Mattermost::Team.all(session)
end end
# We ignore the error message as we can't display it
response.has_key?('message') ? [] : response
rescue Mattermost::NoSessionError
[]
end
end end
def trigger(params) def trigger(params)
...@@ -64,21 +53,17 @@ class MattermostSlashCommandsService < ChatService ...@@ -64,21 +53,17 @@ class MattermostSlashCommandsService < ChatService
private private
def command(trigger:, url:, icon_url:) def command(params)
pretty_project_name = project.name_with_namespace pretty_project_name = project.name_with_namespace
{ params.merge(
auto_complete: true, auto_complete: true,
auto_complete_desc: "Perform common operations on: #{pretty_project_name}", auto_complete_desc: "Perform common operations on: #{pretty_project_name}",
auto_complete_hint: '[help]', auto_complete_hint: '[help]',
description: "Perform common operations on: #{pretty_project_name}", description: "Perform common operations on: #{pretty_project_name}",
display_name: "GitLab / #{pretty_project_name}", display_name: "GitLab / #{pretty_project_name}",
method: 'P', method: 'P',
user_name: 'GitLab', user_name: 'GitLab')
trigger: trigger,
url: url,
icon_url: icon_url
}
end end
def find_chat_user(params) def find_chat_user(params)
......
%p %p
This service will be installed on the Mattermost instance at This service will be installed on the Mattermost instance at
%strong= Gitlab.config.mattermost.host %strong= link_to Gitlab.config.mattermost.host, Gitlab.config.mattermost.host
%hr %hr
= form_for(:create, method: :post, url: configure_namespace_project_mattermost_index_path(@project.namespace, @project)) do |f| = form_for(:mattermost, method: :post, url: namespace_project_mattermost_path(@project.namespace, @project)) do |f|
%h4 Team %h4 Team
%p Select or create the team where the slash commands will be used in %p Select or create the team where the slash commands will be used in
- options = mattermost_teams_options(@teams) - selected_id = @teams.keys.first if @teams.one?
= f.select(:team_id, options, {}, { class: 'form-control', selected: "#{options.first[1] if options.count.one?}", disabled: options.count.one? }) = f.select(:team_id, mattermost_teams_options(@teams), {}, { class: 'form-control', selected: "#{selected_id}", disabled: @teams.one? })
.help-block .help-block
- if options.count.one? - if @teams.one?
This is the only team where you are an administrator. This is the only team where you are an administrator.
- else - else
The list shows teams where you are administrator The list shows teams where you are administrator
......
...@@ -5,13 +5,10 @@ ...@@ -5,13 +5,10 @@
.row .row
%strong.col-sm-3.text-right Mattermost %strong.col-sm-3.text-right Mattermost
= link_to pretty_url(Gitlab.config.mattermost.host), Gitlab.config.mattermost.host, class: 'col-sm-9', target: '__blank' = link_to pretty_url(Gitlab.config.mattermost.host), Gitlab.config.mattermost.host, class: 'col-sm-9', target: '__blank'
- unless @service.activated?
.row .row
%strong.col-sm-3.text-right Installation %strong.col-sm-3.text-right Installation
.col-sm-9 .col-sm-9
- if @service.activated?
To edit or uninstall this service, press
%strong Edit in Mattermost
- else
To install this service, press To install this service, press
%strong Add to Mattermost %strong Add to Mattermost
and follow the instructions and follow the instructions
...@@ -19,4 +16,4 @@ ...@@ -19,4 +16,4 @@
.col-sm-9.col-sm-offset-3 .col-sm-9.col-sm-offset-3
= link_to new_namespace_project_mattermost_path(@project.namespace, @project), class: 'btn btn-lg' do = link_to new_namespace_project_mattermost_path(@project.namespace, @project), class: 'btn btn-lg' do
= custom_icon('mattermost_logo', size: 15) = custom_icon('mattermost_logo', size: 15)
= @service.activated? ? 'Edit in Mattermost' : 'Add to Mattermost' = 'Add to Mattermost'
...@@ -76,7 +76,7 @@ constraints(ProjectUrlConstrainer.new) do ...@@ -76,7 +76,7 @@ constraints(ProjectUrlConstrainer.new) do
end end
end end
resources :mattermost, only: [:new, :create] resource :mattermost, only: [:new, :create]
resources :deploy_keys, constraints: { id: /\d+/ }, only: [:index, :new, :create] do resources :deploy_keys, constraints: { id: /\d+/ }, only: [:index, :new, :create] do
member do member do
......
module Mattermost module Mattermost
class Command class Command
def self.create(session, team_id, command) def self.create(session, params)
response = session.post("/api/v3/teams/#{team_id}/commands/create", body: command.to_json).parsed_response response = session.post("/api/v3/teams/#{params[:team_id]}/commands/create",
body: params.to_json)
if response.has_key?('message') if response.success?
response response.parsed_response['token']
elsif response.parsed_response.try(:has_key?, 'message')
raise response.parsed_response['message']
else else
response['token'] raise 'Failed to create a new command'
end end
end end
end end
......
module Mattermost module Mattermost
class NoSessionError < StandardError; end class NoSessionError < StandardError
def message
'No session could be set up, is Mattermost configured with Single Sign on?'
end
end
# This class' prime objective is to obtain a session token on a Mattermost # This class' prime objective is to obtain a session token on a Mattermost
# instance with SSO configured where this GitLab instance is the provider. # instance with SSO configured where this GitLab instance is the provider.
# #
......
module Mattermost module Mattermost
class Team class Team
def self.all(session) def self.all(session)
session.get('/api/v3/teams/all').parsed_response response = session.get('/api/v3/teams/all')
if response.success?
response.parsed_response
elsif response.parsed_response.try(:has_key?, 'message')
raise response.parsed_response['message']
else
raise 'Failed to list teams'
end
end end
end end
end end
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