BigW Consortium Gitlab

Commit 475a4dc3 by Z.J. van de Weg

Merge branch 'zj-mattermost-session' into zj-mattermost-slash-config

parents 00459967 166ae89d
...@@ -8,15 +8,15 @@ class Projects::MattermostController < Projects::ApplicationController ...@@ -8,15 +8,15 @@ class Projects::MattermostController < Projects::ApplicationController
end end
def configure def configure
@service.configure(host, current_user, params) @service.configure(host, current_user, configure_params)
redirect_to( redirect_to(
new_namespace_project_service_path(@project.namespace, @project, @service.to_param), new_namespace_project_mattermost_path(@project.namespace, @project),
notice: 'This service is now configured.' notice: 'This service is now configured.'
) )
rescue Mattermost::NoSessionError rescue NoSessionError
redirect_to( redirect_to(
edit_namespace_project_service_path(@project.namespace, @project, @service.to_param), new_namespace_project_mattermost_path(@project.namespace, @project),
alert: 'No session could be set up, is Mattermost configured with Single Sign on?' alert: 'No session could be set up, is Mattermost configured with Single Sign on?'
) )
end end
...@@ -24,11 +24,11 @@ class Projects::MattermostController < Projects::ApplicationController ...@@ -24,11 +24,11 @@ class Projects::MattermostController < Projects::ApplicationController
private private
def configure_params def configure_params
params.require(:configure_params).permit(:trigger, :team_id) params.permit(:trigger, :team_id).merge(url: service_trigger_url(@service), icon_url: asset_url('gitlab_logo.png'))
end end
def service def service
@service ||= @project.services.find_by(type: 'MattermostSlashCommandsService') @service ||= @project.find_or_initialize_service('mattermost_slash_commands')
end end
def teams def teams
...@@ -37,7 +37,7 @@ class Projects::MattermostController < Projects::ApplicationController ...@@ -37,7 +37,7 @@ class Projects::MattermostController < Projects::ApplicationController
Mattermost::Mattermost.new(Gitlab.config.mattermost.host, current_user).with_session do Mattermost::Mattermost.new(Gitlab.config.mattermost.host, current_user).with_session do
Mattermost::Team.team_admin Mattermost::Team.team_admin
end end
rescue Mattermost::NoSessionError rescue
[] []
end end
end end
......
...@@ -25,15 +25,15 @@ class MattermostSlashCommandsService < ChatService ...@@ -25,15 +25,15 @@ class MattermostSlashCommandsService < ChatService
] ]
end end
def configure(host, current_user, params) def configure(host, current_user, team_id:, trigger:, url:, icon_url:)
token = Mattermost::Mattermost.new(host, current_user).with_session do new_token = Mattermost::Session.new(host, current_user).with_session do
Mattermost::Commands.create(params[:team_id], Mattermost::Command.create(team_id,
trigger: params[:trigger] || @service.project.path, trigger: trigger || @service.project.path,
url: service_trigger_url(@service), url: url,
icon_url: asset_url('gitlab_logo.png')) icon_url: icon_url)
end end
update_attributes(token: token) update!(token: new_token)
end end
def trigger(params) def trigger(params)
......
= "hello world" = "hello world"
= form_for(:create, method: :post, url: configure_namespace_project_mattermost_path(@project.namespace, @project, @service.to_param)) do |f| = @teams
= form_for(:create, method: :post, url: configure_namespace_project_mattermost_path(@project.namespace, @project, )) do |f|
= "Team ID" = "Team ID"
= f.text_field(:team_id) = f.text_field(:team_id)
= f.submit 'Configure', class: 'btn btn-save' = f.submit 'Configure', class: 'btn btn-save'
......
module Mattermost module Mattermost
class Command < Session class Command < Session
def self.all(team_id) def self.create(team_id, trigger: 'gitlab', url:, icon_url:)
get("/teams/#{team_id}/commands/list_team_commands").parsed_response
end
# params should be a hash, which supplies _at least_:
# - trigger => The slash command, no spaces, cannot start with a /
# - url => What is the URL to trigger here?
# - icon_url => Supply a link to the icon
def self.create(team_id, params)
command = { command = {
auto_complete: true, auto_complete: true,
auto_complete_desc: 'List all available commands', auto_complete_desc: 'List all available commands',
auto_complete_hint: '[help]', auto_complete_hint: '[help]',
description: 'Perform common operations on GitLab', description: 'Perform common operations on GitLab',
display_name: 'GitLab', display_name: 'GitLab Slash Commands',
method: 'P', method: 'P',
user_name: 'GitLab', user_name: 'GitLab',
trigger: 'gitlab', trigger: trigger,
}.merge(params) url: url,
icon_url: icon_url
}
response = post( "/teams/#{team_id}/commands/create", body: command.to_json) response = post( "/teams/#{team_id}/commands/create", body: command.to_json)
......
...@@ -21,6 +21,8 @@ module Mattermost ...@@ -21,6 +21,8 @@ module Mattermost
def initialize(uri, current_user) def initialize(uri, current_user)
uri = normalize_uri(uri) uri = normalize_uri(uri)
# Sets the base uri for HTTParty, so we can use paths
self.class.base_uri(uri) self.class.base_uri(uri)
@current_resource_owner = current_user @current_resource_owner = current_user
...@@ -28,12 +30,14 @@ module Mattermost ...@@ -28,12 +30,14 @@ module Mattermost
def with_session def with_session
raise NoSessionError unless create raise NoSessionError unless create
result = yield
destroy
result begin
yield
rescue Errno::ECONNREFUSED rescue Errno::ECONNREFUSED
raise NoSessionError raise NoSessionError
ensure
destroy
end
end end
# Next methods are needed for Doorkeeper # Next methods are needed for Doorkeeper
......
...@@ -5,7 +5,7 @@ describe Mattermost::Session do ...@@ -5,7 +5,7 @@ describe Mattermost::Session do
subject { described_class.new('http://localhost:8065', user) } subject { described_class.new('http://localhost:8065', user) }
# Needed for doorman to function # Needed for doorkeeper to function
it { is_expected.to respond_to(:current_resource_owner) } it { is_expected.to respond_to(:current_resource_owner) }
it { is_expected.to respond_to(:request) } it { is_expected.to respond_to(:request) }
it { is_expected.to respond_to(:authorization) } it { is_expected.to respond_to(:authorization) }
......
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