Commit 53271b48 authored by Kamil Trzcinski's avatar Kamil Trzcinski Committed by Z.J. van de Weg

Make chat authorization to work [ci skip]

parent 47621989
...@@ -28,6 +28,8 @@ class Projects::ServicesController < Projects::ApplicationController ...@@ -28,6 +28,8 @@ class Projects::ServicesController < Projects::ApplicationController
end end
def test def test
return render_404 unless @service.can_test?
data = @service.test_data(project, current_user) data = @service.test_data(project, current_user)
outcome = @service.test(data) outcome = @service.test(data)
......
...@@ -6,4 +6,8 @@ module TriggersHelper ...@@ -6,4 +6,8 @@ module TriggersHelper
"#{Settings.gitlab.url}/api/v3/projects/#{project_id}/ref/#{ref}/trigger/builds" "#{Settings.gitlab.url}/api/v3/projects/#{project_id}/ref/#{ref}/trigger/builds"
end end
end end
def service_trigger_url(service)
"#{Settings.gitlab.url}/api/v3/projects/#{service.project_id}/services/#{service.to_param}/trigger"
end
end end
...@@ -23,8 +23,8 @@ class Project < ActiveRecord::Base ...@@ -23,8 +23,8 @@ class Project < ActiveRecord::Base
cache_markdown_field :description, pipeline: :description cache_markdown_field :description, pipeline: :description
delegate :feature_available?, :builds_enabled?, :wiki_enabled?, delegate :feature_available?, :builds_enabled?, :wiki_enabled?,
:merge_requests_enabled?, :issues_enabled?, to: :project_feature, :merge_requests_enabled?, :issues_enabled?, to: :project_feature,
allow_nil: true allow_nil: true
default_value_for :archived, false default_value_for :archived, false
...@@ -91,6 +91,7 @@ class Project < ActiveRecord::Base ...@@ -91,6 +91,7 @@ class Project < ActiveRecord::Base
has_one :assembla_service, dependent: :destroy has_one :assembla_service, dependent: :destroy
has_one :asana_service, dependent: :destroy has_one :asana_service, dependent: :destroy
has_one :gemnasium_service, dependent: :destroy has_one :gemnasium_service, dependent: :destroy
has_one :mattermost_chat_service, dependent: :destroy
has_one :slack_service, dependent: :destroy has_one :slack_service, dependent: :destroy
has_one :buildkite_service, dependent: :destroy has_one :buildkite_service, dependent: :destroy
has_one :bamboo_service, dependent: :destroy has_one :bamboo_service, dependent: :destroy
......
...@@ -3,13 +3,16 @@ ...@@ -3,13 +3,16 @@
class ChatService < Service class ChatService < Service
default_value_for :category, 'chat' default_value_for :category, 'chat'
has_many :chat_users has_many :chat_names, foreign_key: :service_id
def valid_token?(token) def valid_token?(token)
self.respond_to?(:token) && self.token.present? && ActiveSupport::SecurityUtils.variable_size_secure_compare(token, self.token) self.respond_to?(:token) &&
self.token.present? &&
ActiveSupport::SecurityUtils.variable_size_secure_compare(token, self.token)
end end
def supported_events def supported_events
[]
end end
def trigger(params) def trigger(params)
......
# Base class for Chat services
class MattermostChatService < ChatService class MattermostChatService < ChatService
include TriggersHelper
prop_accessor :token
def can_test?
false
end
def title def title
'Mattermost' 'Mattermost'
end end
...@@ -13,28 +20,40 @@ class MattermostChatService < ChatService ...@@ -13,28 +20,40 @@ class MattermostChatService < ChatService
end end
def help def help
'This service allows you to use slash commands with your Mattermost installation.<br/> "This service allows you to use slash commands with your Mattermost installation.<br/>
To setup this Service you need to create a new <b>"Slash commands"</b> in your Mattermost integration panel, To setup this Service you need to create a new <b>Slash commands</b> in your Mattermost integration panel.<br/>
and enter the token below.' <br/>
Create integration with URL #{service_trigger_url(self)} and enter the token below."
end end
def fields def fields
[ [
{ type: 'text', name: 'token', placeholder: 'https://hooks.slack.com/services/...' } { type: 'text', name: 'token', placeholder: '' }
] ]
end end
def trigger(params) def trigger(params)
user = ChatNames::FindUserService.new(chat_names, params).execute return nil unless valid_token?(params[:token])
user = find_chat_user(params)
return authorize_chat_name(params) unless user return authorize_chat_name(params) unless user
Mattermost::CommandService.new(project, user, params).execute Mattermost::CommandService.new(project, user, params.slice(:command, :text)).
execute
end end
private private
def find_chat_user(params)
params = params.slice(:team_id, :user_id)
ChatNames::FindUserService.
new(chat_names, params).
execute
end
def authorize_chat_name(params) def authorize_chat_name(params)
url = ChatNames::RequestService.new(service, params).execute params = params.slice(:team_id, :team_domain, :user_id, :user_name)
url = ChatNames::AuthorizeUserService.new(self, params).execute
{ {
response_type: :ephemeral, response_type: :ephemeral,
......
...@@ -214,6 +214,7 @@ class Service < ActiveRecord::Base ...@@ -214,6 +214,7 @@ class Service < ActiveRecord::Base
hipchat hipchat
irker irker
jira jira
mattermost_chat
pivotaltracker pivotaltracker
pushover pushover
redmine redmine
......
...@@ -10,26 +10,27 @@ ...@@ -10,26 +10,27 @@
.col-sm-10 .col-sm-10
= form.check_box :active = form.check_box :active
.form-group - if @service.supported_events.present?
= form.label :url, "Trigger", class: 'control-label' .form-group
= form.label :url, "Trigger", class: 'control-label'
.col-sm-10
- @service.supported_events.each do |event| .col-sm-10
%div - @service.supported_events.each do |event|
= form.check_box service_event_field_name(event), class: 'pull-left' %div
.prepend-left-20 = form.check_box service_event_field_name(event), class: 'pull-left'
= form.label service_event_field_name(event), class: 'list-label' do .prepend-left-20
%strong = form.label service_event_field_name(event), class: 'list-label' do
= event.humanize %strong
= event.humanize
- field = @service.event_field(event)
- field = @service.event_field(event)
- if field
%p - if field
= form.text_field field[:name], class: "form-control", placeholder: field[:placeholder] %p
= form.text_field field[:name], class: "form-control", placeholder: field[:placeholder]
%p.light
= service_event_description(event) %p.light
= service_event_description(event)
- @service.global_fields.each do |field| - @service.global_fields.each do |field|
- type = field[:type] - type = field[:type]
......
...@@ -45,4 +45,7 @@ Rails.application.configure do ...@@ -45,4 +45,7 @@ Rails.application.configure do
# Do not log asset requests # Do not log asset requests
config.assets.quiet = true config.assets.quiet = true
# Make hot reloading to work with Grape API
ActiveSupport::Dependencies.explicitly_unloadable_constants << "API"
end end
module API module API
# Projects API # Projects API
class Services < Grape::API class Services < Grape::API
before { authenticate! }
before { authorize_admin_project }
resource :projects do resource :projects do
before { authenticate! }
before { authorize_admin_project }
# Set <service_slug> service for project # Set <service_slug> service for project
# #
# Example Request: # Example Request:
...@@ -59,5 +59,28 @@ module API ...@@ -59,5 +59,28 @@ module API
present project_service, with: Entities::ProjectService, include_passwords: current_user.is_admin? present project_service, with: Entities::ProjectService, include_passwords: current_user.is_admin?
end end
end end
resource :projects do
post ':id/services/:service_slug/trigger' do
project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])
underscored_service = params[:service_slug].underscore
not_found!('Service') unless Service.available_services_names.include?(underscored_service)
service_method = "#{underscored_service}_service"
service = project.public_send(service_method)
result = if service.try(:active?) && service.respond_to?(:trigger)
service.trigger(params)
end
if result
present result, status: result[:status] || 200
else
not_found!('Service')
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