summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2016-12-20 00:22:10 +0100
committerKamil Trzcinski <ayufan@ayufan.eu>2016-12-20 00:22:10 +0100
commit841960f847f04da9c427bcdb19037e2112a90890 (patch)
treeff485924db7614a01d5f3f52f66dd28056097ff8
parent34295036e2a9ecf18ca5440a5dd6dbb0c7f05643 (diff)
downloadgitlab-ce-841960f847f04da9c427bcdb19037e2112a90890.tar.gz
Fix flow
-rw-r--r--app/controllers/projects/mattermosts_controller.rb1
-rw-r--r--app/models/project_services/mattermost_slash_commands_service.rb11
-rw-r--r--lib/mattermost/client.rb39
-rw-r--r--lib/mattermost/command.rb14
-rw-r--r--lib/mattermost/team.rb14
5 files changed, 51 insertions, 28 deletions
diff --git a/app/controllers/projects/mattermosts_controller.rb b/app/controllers/projects/mattermosts_controller.rb
index c6b39add7ad..0f939838306 100644
--- a/app/controllers/projects/mattermosts_controller.rb
+++ b/app/controllers/projects/mattermosts_controller.rb
@@ -32,6 +32,7 @@ class Projects::MattermostsController < Projects::ApplicationController
def teams
@teams ||= @service.list_teams(current_user)
rescue => e
+ @teams = []
flash[:alert] = e.message
end
diff --git a/app/models/project_services/mattermost_slash_commands_service.rb b/app/models/project_services/mattermost_slash_commands_service.rb
index 51de80f38de..accf59bea18 100644
--- a/app/models/project_services/mattermost_slash_commands_service.rb
+++ b/app/models/project_services/mattermost_slash_commands_service.rb
@@ -25,18 +25,15 @@ class MattermostSlashCommandsService < ChatService
]
end
- def configure!(current_user, params)
- token = Mattermost::Session.new(current_user).with_session do |session|
- Mattermost::Command.create(session, command(params))
- end
+ def configure!(user, params)
+ token = Mattermost::Command.new(user).
+ create(command(params))
update!(active: true, token: token)
end
def list_teams(user)
- Mattermost::Session.new(user).with_session do |session|
- Mattermost::Team.all(session)
- end
+ Mattermost::Team.new(user).all
end
def trigger(params)
diff --git a/lib/mattermost/client.rb b/lib/mattermost/client.rb
new file mode 100644
index 00000000000..2a0e4b400d3
--- /dev/null
+++ b/lib/mattermost/client.rb
@@ -0,0 +1,39 @@
+module Mattermost
+ class Client
+ attr_reader :user
+
+ def initialize(user)
+ @user = user
+ end
+
+ private
+
+ def with_session(&blk)
+ Session.new(user).with_session(&blk)
+ end
+
+ def json_get(path, options = {})
+ with_session do |session|
+ json_response session.get(path, options)
+ end
+ end
+
+ def json_post(path, options = {})
+ with_session do |session|
+ json_response session.post(path, options)
+ end
+ end
+
+ def json_response(response)
+ json_response = JSON.parse(response.body)
+
+ if response.success?
+ json_response
+ elsif json_response['message']
+ raise json_response['message']
+ else
+ raise 'Undefined error'
+ end
+ end
+ end
+end
diff --git a/lib/mattermost/command.rb b/lib/mattermost/command.rb
index 5c6f5861a7f..d1e4bb0eccf 100644
--- a/lib/mattermost/command.rb
+++ b/lib/mattermost/command.rb
@@ -1,16 +1,10 @@
module Mattermost
- class Command
- def self.create(session, params)
- response = session.post("/api/v3/teams/#{params[:team_id]}/commands/create",
+ class Command < Client
+ def create(params)
+ response = json_post("/api/v3/teams/#{params[:team_id]}/commands/create",
body: params.to_json)
- if response.success?
- response.parsed_response['token']
- elsif response.parsed_response.try(:has_key?, 'message')
- raise response.parsed_response['message']
- else
- raise 'Failed to create a new command'
- end
+ response['token']
end
end
end
diff --git a/lib/mattermost/team.rb b/lib/mattermost/team.rb
index 2de01eced0b..784eca6ab5a 100644
--- a/lib/mattermost/team.rb
+++ b/lib/mattermost/team.rb
@@ -1,15 +1,7 @@
module Mattermost
- class Team
- def self.all(session)
- 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
+ class Team < Client
+ def all
+ json_get('/api/v3/teams/all')
end
end
end