From 87d160634dfdaacd0dc382c26932786382d1be34 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Tue, 13 Dec 2016 19:52:41 +0100 Subject: Base work for auto config MM slash commands --- lib/mattermost/command.rb | 26 ++++++++++++++++++++++++++ lib/mattermost/session.rb | 13 +++++++++++-- lib/mattermost/team.rb | 10 ++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 lib/mattermost/command.rb create mode 100644 lib/mattermost/team.rb (limited to 'lib') diff --git a/lib/mattermost/command.rb b/lib/mattermost/command.rb new file mode 100644 index 00000000000..e159458a788 --- /dev/null +++ b/lib/mattermost/command.rb @@ -0,0 +1,26 @@ +module Mattermost + class Command + def self.all(team_id) + Mattermost::Mattermost.get("/teams/#{team_id}/commands/list_team_commands") + 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) + params = { + auto_complete: true, + auto_complete_desc: 'List all available commands', + auto_complete_hint: '[help]', + description: 'Perform common operations on GitLab', + display_name: 'GitLab', + method: 'P', + user_name: 'GitLab' + }..merge(params) + + Mattermost::Mattermost.post( "/teams/#{team_id}/commands/create", params.to_json). + parsed_response['token'] + end + end +end diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb index 81964666757..cc4cb1f4f12 100644 --- a/lib/mattermost/session.rb +++ b/lib/mattermost/session.rb @@ -20,6 +20,7 @@ module Mattermost attr_accessor :current_resource_owner def initialize(uri, current_user) + uri = normalize_uri(uri) self.class.base_uri(uri) @current_resource_owner = current_user @@ -31,6 +32,8 @@ module Mattermost destroy result + rescue Errno::ECONNREFUSED + raise NoSessionError end # Next methods are needed for Doorkeeper @@ -67,11 +70,11 @@ module Mattermost end def destroy - post('/api/v3/users/logout') + post('/users/logout') end def oauth_uri - response = get("/api/v3/oauth/gitlab/login", follow_redirects: false) + response = get("/oauth/gitlab/login", follow_redirects: false) return unless 300 <= response.code && response.code < 400 redirect_uri = response.headers['location'] @@ -100,5 +103,11 @@ module Mattermost def post(path, options = {}) self.class.post(path, options) end + + def normalize_uri(uri) + uri << '/' unless uri.end_with?('/') + + uri << 'api/v3' + end end end diff --git a/lib/mattermost/team.rb b/lib/mattermost/team.rb new file mode 100644 index 00000000000..76e238a866e --- /dev/null +++ b/lib/mattermost/team.rb @@ -0,0 +1,10 @@ +module Mattermost + class Team < Mattermost + # After normalization this returns an array of hashes + # + # [{"id"=>"paf573pj9t81urupw3fanozeda", "display_name"=>"my team", }] + def self.all + @all_teams ||= get('/teams/all').parsed_response.values + end + end +end -- cgit v1.2.3