diff options
author | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-02-20 13:41:50 +0100 |
---|---|---|
committer | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-02-20 13:41:50 +0100 |
commit | 444d71e043eb19979ec1b08504b2760910cb2a47 (patch) | |
tree | eae73ba8a12de1a84faee800fae9749b70ed1b78 /app | |
parent | 549fc3469790da388035de713294f335fbfb4fb5 (diff) | |
download | gitlab-ce-444d71e043eb19979ec1b08504b2760910cb2a47.tar.gz |
Transactional mattermost team creation
Before this commit, but still on this feature branch, the creation of
mattermost teams where a background job. However, it was decided it was
better that these happened as transaction so feedback could be displayed
to the user.
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/groups_controller.rb | 1 | ||||
-rw-r--r-- | app/models/group.rb | 1 | ||||
-rw-r--r-- | app/models/namespace.rb | 1 | ||||
-rw-r--r-- | app/services/groups/create_service.rb | 21 | ||||
-rw-r--r-- | app/services/mattermost/create_team_service.rb | 15 | ||||
-rw-r--r-- | app/workers/mattermost/create_team_worker.rb | 24 |
6 files changed, 29 insertions, 34 deletions
diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index b2a18f0e65d..0e421b915cf 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -29,7 +29,6 @@ class GroupsController < Groups::ApplicationController end def create - byebug @group = Groups::CreateService.new(current_user, group_params).execute if @group.persisted? diff --git a/app/models/group.rb b/app/models/group.rb index 27dc4e8f319..240a17f1dc1 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -21,7 +21,6 @@ class Group < Namespace has_many :shared_projects, through: :project_group_links, source: :project has_many :notification_settings, dependent: :destroy, as: :source has_many :labels, class_name: 'GroupLabel' - has_one :chat_team, dependent: :destroy validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? } validate :visibility_level_allowed_by_projects diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 6de4d08fc28..461a38d3e8e 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -20,6 +20,7 @@ class Namespace < ActiveRecord::Base belongs_to :parent, class_name: "Namespace" has_many :children, class_name: "Namespace", foreign_key: :parent_id + has_one :chat_team, dependent: :destroy validates :owner, presence: true, unless: ->(n) { n.type == "Group" } validates :name, diff --git a/app/services/groups/create_service.rb b/app/services/groups/create_service.rb index 13d1b545498..3028025fc6e 100644 --- a/app/services/groups/create_service.rb +++ b/app/services/groups/create_service.rb @@ -2,12 +2,10 @@ module Groups class CreateService < Groups::BaseService def initialize(user, params = {}) @current_user, @params = user, params.dup + @chat_team = @params.delete(:create_chat_team) end def execute - create_chat_team = params.delete(:create_chat_team) - team_name = params.delete(:chat_team_name) - @group = Group.new(params) unless Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level]) @@ -23,15 +21,22 @@ module Groups end @group.name ||= @group.path.dup - @group.save - @group.add_owner(current_user) - if create_chat_team && Gitlab.config.mattermost.enabled - options = team_name ? { name: team_name } : {} - Mattermost::CreateTeamWorker.perform_async(@group.id, current_user.id, options) + if create_chat_team? + Mattermost::CreateTeamService.new(@group, current_user).execute + + return @group if @group.errors.any? end + @group.save + @group.add_owner(current_user) @group end + + private + + def create_chat_team? + @chat_team && Gitlab.config.mattermost.enabled + end end end diff --git a/app/services/mattermost/create_team_service.rb b/app/services/mattermost/create_team_service.rb new file mode 100644 index 00000000000..199d15aee92 --- /dev/null +++ b/app/services/mattermost/create_team_service.rb @@ -0,0 +1,15 @@ +module Mattermost + class CreateTeamService < ::BaseService + def initialize(group, current_user) + @group, @current_user = group, current_user + end + + def execute + # The user that creates the team will be Team Admin + response = Mattermost::Team.new(current_user).create(@group) + @group.build_chat_team(name: response['name'], team_id: response['id']) + rescue Mattermost::ClientError => e + @group.errors.add(:chat_team, e.message) + end + end +end diff --git a/app/workers/mattermost/create_team_worker.rb b/app/workers/mattermost/create_team_worker.rb deleted file mode 100644 index 95b63dac8ab..00000000000 --- a/app/workers/mattermost/create_team_worker.rb +++ /dev/null @@ -1,24 +0,0 @@ -module Mattermost - class CreateTeamWorker - include Sidekiq::Worker - include DedicatedSidekiqQueue - - sidekiq_options retry: 5 - - # Add 5 seconds so the first retry isn't 1 second later - sidekiq_retry_in do |count| - 5 + 5**n - end - - def perform(group_id, current_user_id, options = {}) - group = Group.find_by(id: group_id) - current_user = User.find_by(id: current_user_id) - return unless group && current_user - - # The user that creates the team will be Team Admin - response = Mattermost::Team.new(current_user).create(group, options) - - group.create_chat_team(name: response['name'], team_id: response['id']) - end - end -end |