Create MM team for GitLab group
This commit is contained in:
		
							parent
							
								
									e90ec73f65
								
							
						
					
					
						commit
						297dc70158
					
				|  | @ -21,6 +21,7 @@ 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 | ||||
|  |  | |||
|  | @ -22,6 +22,11 @@ module Groups | |||
|       @group.name ||= @group.path.dup | ||||
|       @group.save | ||||
|       @group.add_owner(current_user) | ||||
| 
 | ||||
|       if params[:create_chat_team] && Gitlab.config.mattermost.enabled | ||||
|         Mattermost::CreateTeamWorker.perform_async(@group.id, current_user.id) | ||||
|       end | ||||
| 
 | ||||
|       @group | ||||
|     end | ||||
|   end | ||||
|  |  | |||
|  | @ -16,6 +16,11 @@ | |||
| 
 | ||||
|   = render 'shared/visibility_level', f: f, visibility_level: default_group_visibility, can_change_visibility_level: true, form_model: @group | ||||
| 
 | ||||
|   .form-group | ||||
|     = f.label :create_chat_team, "Create Mattermost Team", class: 'control-label' | ||||
|     .col-sm-10 | ||||
|       = f.check_box :chat_team | ||||
| 
 | ||||
|   .form-group | ||||
|     .col-sm-offset-2.col-sm-10 | ||||
|       = render 'shared/group_tips' | ||||
|  |  | |||
|  | @ -0,0 +1,28 @@ | |||
| module Mattermost | ||||
|   class CreateTeamWorker | ||||
|     include Sidekiq::Worker | ||||
|     include DedicatedSidekiqQueue | ||||
| 
 | ||||
|     def perform(group_id, current_user_id, options = {}) | ||||
|       @group = Group.find(group_id) | ||||
|       current_user = User.find(current_user_id) | ||||
| 
 | ||||
|       options = team_params.merge(options) | ||||
| 
 | ||||
|       # The user that creates the team will be Team Admin | ||||
|       response = Mattermost::Team.new(current_user).create(options) | ||||
| 
 | ||||
|       ChatTeam.create!(namespace: @group, name: response['name'], team_id: response['id']) | ||||
|     end | ||||
| 
 | ||||
|     private | ||||
| 
 | ||||
|     def team_params | ||||
|       { | ||||
|         name: @group.path[0..59], | ||||
|         display_name: @group.name[0..59], | ||||
|         type: @group.public? ? 'O' : 'I' # Open vs Invite-only | ||||
|       } | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | @ -0,0 +1,17 @@ | |||
| class CreateChatTeams < ActiveRecord::Migration | ||||
|   include Gitlab::Database::MigrationHelpers | ||||
| 
 | ||||
|   DOWNTIME = false | ||||
| 
 | ||||
|   def change | ||||
|     create_table :chat_teams do |t| | ||||
|       t.integer :namespace_id, index: true | ||||
|       t.string :team_id | ||||
|       t.string :name | ||||
| 
 | ||||
|       t.timestamps null: false | ||||
|     end | ||||
| 
 | ||||
|     add_foreign_key :chat_teams, :namespaces, on_delete: :cascade | ||||
|   end | ||||
| end | ||||
							
								
								
									
										11
									
								
								db/schema.rb
								
								
								
								
							
							
						
						
									
										11
									
								
								db/schema.rb
								
								
								
								
							|  | @ -171,6 +171,16 @@ ActiveRecord::Schema.define(version: 20170214111112) do | |||
|   add_index "chat_names", ["service_id", "team_id", "chat_id"], name: "index_chat_names_on_service_id_and_team_id_and_chat_id", unique: true, using: :btree | ||||
|   add_index "chat_names", ["user_id", "service_id"], name: "index_chat_names_on_user_id_and_service_id", unique: true, using: :btree | ||||
| 
 | ||||
|   create_table "chat_teams", force: :cascade do |t| | ||||
|     t.integer "namespace_id" | ||||
|     t.string "team_id" | ||||
|     t.string "name" | ||||
|     t.datetime "created_at", null: false | ||||
|     t.datetime "updated_at", null: false | ||||
|   end | ||||
| 
 | ||||
|   add_index "chat_teams", ["namespace_id"], name: "index_chat_teams_on_namespace_id", using: :btree | ||||
| 
 | ||||
|   create_table "ci_application_settings", force: :cascade do |t| | ||||
|     t.boolean "all_broken_builds" | ||||
|     t.boolean "add_pusher" | ||||
|  | @ -1330,6 +1340,7 @@ ActiveRecord::Schema.define(version: 20170214111112) do | |||
|   add_index "web_hooks", ["project_id"], name: "index_web_hooks_on_project_id", using: :btree | ||||
| 
 | ||||
|   add_foreign_key "boards", "projects" | ||||
|   add_foreign_key "chat_teams", "namespaces", on_delete: :cascade | ||||
|   add_foreign_key "issue_metrics", "issues", on_delete: :cascade | ||||
|   add_foreign_key "label_priorities", "labels", on_delete: :cascade | ||||
|   add_foreign_key "label_priorities", "projects", on_delete: :cascade | ||||
|  |  | |||
|  | @ -153,7 +153,7 @@ module Mattermost | |||
|       yield | ||||
|     rescue HTTParty::Error => e | ||||
|       raise Mattermost::ConnectionError.new(e.message) | ||||
|     rescue Errno::ECONNREFUSED | ||||
|     rescue Errno::ECONNREFUSED => e | ||||
|       raise Mattermost::ConnectionError.new(e.message) | ||||
|     end | ||||
|   end | ||||
|  |  | |||
|  | @ -1,7 +1,12 @@ | |||
| module Mattermost | ||||
|   class Team < Client | ||||
|     # Returns **all** teams for an admin | ||||
|     def all | ||||
|       session_get('/api/v3/teams/all') | ||||
|     end | ||||
| 
 | ||||
|     def create(params) | ||||
|       session_post('/api/v3/teams/create', body: params.to_json) | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  |  | |||
|  | @ -0,0 +1,10 @@ | |||
| require 'spec_helper' | ||||
| 
 | ||||
| describe ChatTeam, type: :model do | ||||
|   # Associations | ||||
|   it { is_expected.to belong_to(:group) } | ||||
| 
 | ||||
|   # Fields | ||||
|   it { is_expected.to respond_to(:name) } | ||||
|   it { is_expected.to respond_to(:team_id) } | ||||
| end | ||||
|  | @ -13,6 +13,7 @@ describe Group, models: true do | |||
|     it { is_expected.to have_many(:shared_projects).through(:project_group_links) } | ||||
|     it { is_expected.to have_many(:notification_settings).dependent(:destroy) } | ||||
|     it { is_expected.to have_many(:labels).class_name('GroupLabel') } | ||||
|     it { is_expected.to have_one(:chat_team) } | ||||
| 
 | ||||
|     describe '#members & #requesters' do | ||||
|       let(:requester) { create(:user) } | ||||
|  |  | |||
|  | @ -0,0 +1,21 @@ | |||
| require 'spec_helper' | ||||
| 
 | ||||
| describe Mattermost::CreateTeamWorker do | ||||
|   let(:group) { create(:group, path: 'path', name: 'name') } | ||||
|   let(:admin) { create(:admin) } | ||||
| 
 | ||||
|   describe '.perform' do | ||||
|     subject { described_class.new.perform(group.id, admin.id) } | ||||
| 
 | ||||
|     before do | ||||
|       allow_any_instance_of(Mattermost::Team). | ||||
|         to receive(:create). | ||||
|         with(name: "path", display_name: "name", type: "O"). | ||||
|         and_return('name' => 'my team', 'id' => 'sjfkdlwkdjfwlkfjwf') | ||||
|     end | ||||
| 
 | ||||
|     it 'creates a new chat team' do | ||||
|       expect { subject }.to change { ChatTeam.count }.from(0).to(1) | ||||
|     end | ||||
|   end | ||||
| end | ||||
		Loading…
	
		Reference in New Issue