Commit d617182a authored by Douwe Maan's avatar Douwe Maan

Merge branch 'master' into 'rs-carrierwave-db'

# Conflicts:
#   spec/models/group_spec.rb
parents f96d5112 b70d151d
class BindInOut {
constructor(bindIn, bindOut) {
this.in = bindIn;
this.out = bindOut;
this.eventWrapper = {};
this.eventType = /(INPUT|TEXTAREA)/.test(bindIn.tagName) ? 'keyup' : 'change';
}
addEvents() {
this.eventWrapper.updateOut = this.updateOut.bind(this);
this.in.addEventListener(this.eventType, this.eventWrapper.updateOut);
return this;
}
updateOut() {
this.out.textContent = this.in.value;
return this;
}
removeEvents() {
this.in.removeEventListener(this.eventType, this.eventWrapper.updateOut);
return this;
}
static initAll() {
const ins = document.querySelectorAll('*[data-bind-in]');
return [].map.call(ins, anIn => BindInOut.init(anIn));
}
static init(anIn, anOut) {
const out = anOut || document.querySelector(`*[data-bind-out="${anIn.dataset.bindIn}"]`);
if (!out) return null;
const bindInOut = new BindInOut(anIn, out);
return bindInOut.addEvents().updateOut();
}
}
export default BindInOut;
......@@ -21,8 +21,7 @@
// %a.js-toggle-button
// %div.js-toggle-content
//
$('body').on('click', '.js-toggle-button', function(e) {
e.preventDefault();
$('body').on('click', '.js-toggle-button', function() {
toggleContainer($(this).closest('.js-toggle-container'));
});
......
......@@ -35,6 +35,7 @@
/* global Labels */
/* global Shortcuts */
import BindInOut from './behaviors/bind_in_out';
import GroupsList from './groups_list';
import ProjectsList from './projects_list';
......@@ -229,9 +230,14 @@ const UserCallout = require('./user_callout');
new UsersSelect();
break;
case 'groups:new':
case 'admin:groups:new':
case 'groups:create':
case 'admin:groups:create':
BindInOut.initAll();
case 'groups:new':
case 'admin:groups:new':
case 'groups:edit':
case 'admin:groups:edit':
case 'admin:groups:new':
new GroupAvatar();
break;
case 'projects:tree:show':
......
......@@ -41,6 +41,7 @@ require('./behaviors/details_behavior');
require('./behaviors/quick_submit');
require('./behaviors/requires_input');
require('./behaviors/toggler_behavior');
require('./behaviors/bind_in_out');
// blob
require('./blob/blob_ci_yaml');
......
......@@ -73,3 +73,19 @@
}
}
}
.mattermost-icon svg {
width: 16px;
height: 16px;
vertical-align: text-bottom;
}
.mattermost-team-name {
color: $gl-text-color-secondary;
}
.mattermost-info {
display: block;
color: $gl-text-color-secondary;
margin-top: 10px;
}
......@@ -32,7 +32,13 @@ class GroupsController < Groups::ApplicationController
@group = Groups::CreateService.new(current_user, group_params).execute
if @group.persisted?
redirect_to @group, notice: "Group '#{@group.name}' was successfully created."
notice = if @group.chat_team.present?
"Group '#{@group.name}' and its Mattermost team were successfully created."
else
"Group '#{@group.name}' was successfully created."
end
redirect_to @group, notice: notice
else
render action: "new"
end
......@@ -142,7 +148,9 @@ class GroupsController < Groups::ApplicationController
:request_access_enabled,
:share_with_group_lock,
:visibility_level,
:parent_id
:parent_id,
:create_chat_team,
:chat_team_name
]
end
......
class ChatTeam < ActiveRecord::Base
validates :team_id, presence: true
belongs_to :namespace
end
......@@ -213,4 +213,14 @@ class Group < Namespace
def users_with_parents
User.where(id: members_with_parents.select(:user_id))
end
def mattermost_team_params
max_length = 59
{
name: path[0..max_length],
display_name: name[0..max_length],
type: public? ? 'O' : 'I' # Open vs Invite-only
}
end
end
......@@ -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,
......
......@@ -2,6 +2,7 @@ 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
......@@ -20,9 +21,23 @@ module Groups
end
@group.name ||= @group.path.dup
if create_chat_team?
response = Mattermost::CreateTeamService.new(@group, current_user).execute
return @group if @group.errors.any?
@group.build_chat_team(name: response['name'], team_id: response['id'])
end
@group.save
@group.add_owner(current_user)
@group
end
private
def create_chat_team?
Gitlab.config.mattermost.enabled && @chat_team && group.chat_team.nil?
end
end
end
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
Mattermost::Team.new(current_user).create(@group.mattermost_team_params)
rescue Mattermost::ClientError => e
@group.errors.add(:mattermost_team, e.message)
end
end
end
.form-group
= f.label :create_chat_team, class: 'control-label' do
%span.mattermost-icon
= custom_icon('icon_mattermost')
Mattermost
.col-sm-10
.checkbox.js-toggle-container
= f.label :create_chat_team do
.js-toggle-button= f.check_box(:create_chat_team, { checked: true }, true, false)
Create a Mattermost team for this group
%br
%small.light.js-toggle-content
Mattermost URL:
= Settings.mattermost.host
%span> /
%span{ "data-bind-out" => "create_chat_team" }
......@@ -16,6 +16,8 @@
= render 'shared/visibility_level', f: f, visibility_level: default_group_visibility, can_change_visibility_level: true, form_model: @group
= render 'create_chat_team', f: f if Gitlab.config.mattermost.enabled
.form-group
.col-sm-offset-2.col-sm-10
= render 'shared/group_tips'
......
......@@ -18,7 +18,8 @@
= f.text_field :path, placeholder: 'open-source', class: 'form-control',
autofocus: local_assigns[:autofocus] || false, required: true,
pattern: Gitlab::Regex::NAMESPACE_REGEX_STR_JS,
title: 'Please choose a group name with no special characters.'
title: 'Please choose a group name with no special characters.',
"data-bind-in" => "#{'create_chat_team' if Gitlab.config.mattermost.enabled}"
- if parent
= f.hidden_field :parent_id, value: parent.id
......
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500"><path d="M250.05 34c1.9.04 3.8.11 5.6.2l-29.79 35.51c-.07.01-.15.03-.23.04C149.26 84.1 98.22 146.5 98.22 222.97c0 41.56 23.07 90.5 59.75 119.1 28.61 22.32 64.29 36.9 101.21 36.9 93.4 0 160.15-68.61 160.15-156 0-34.91-15.99-72.77-41.76-100.76l-1.63-47.39c54.45 39.15 89.95 103.02 90.06 175.17v.01c0 119.29-96.7 216-216 216-119.29 0-216-96.71-216-216S130.71 34 250 34h.05zm64.1 20.29c.66-.04 1.32.03 1.96.25 3.01 1 3.85 3.57 3.93 6.45l3.84 146.88c.76 28.66-17.16 68.44-60.39 68.56-30.97.08-63.68-20.83-63.68-60.13.01-14.73 5.61-31.26 19.25-48.11l90.03-111.18c1.15-1.42 3.08-2.58 5.06-2.72z"/></svg>
class CreateChatTeams < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = true
DOWNTIME_REASON = "Adding a foreign key"
disable_ddl_transaction!
def change
create_table :chat_teams do |t|
t.references :namespace, null: false, index: { unique: true }, foreign_key: { on_delete: :cascade }
t.string :team_id
t.string :name
t.timestamps null: false
end
end
end
......@@ -175,6 +175,16 @@ ActiveRecord::Schema.define(version: 20170306170512) 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", null: false
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", unique: true, using: :btree
create_table "ci_application_settings", force: :cascade do |t|
t.boolean "all_broken_builds"
t.boolean "add_pusher"
......@@ -1352,6 +1362,7 @@ ActiveRecord::Schema.define(version: 20170306170512) 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 "ci_triggers", "users", column: "owner_id", name: "fk_e8e10d1964", on_delete: :cascade
add_foreign_key "issue_metrics", "issues", on_delete: :cascade
add_foreign_key "label_priorities", "labels", on_delete: :cascade
......
......@@ -26,7 +26,7 @@ module Mattermost
def session_get(path, options = {})
with_session do |session|
get(session, path, options)
get(session, path, options)
end
end
......
......@@ -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
......
module Mattermost
class Team < Client
# Returns **all** teams for an admin
def all
session_get('/api/v3/teams/all').values
end
# Creates a team on the linked Mattermost instance, the team admin will be the
# `current_user` passed to the Mattermost::Client instance
def create(name:, display_name:, type:)
session_post('/api/v3/teams/create', body: {
name: name,
display_name: display_name,
type: type
}.to_json)
end
end
end
......@@ -43,6 +43,44 @@ feature 'Group', feature: true do
expect(page).to have_namespace_error_message
end
end
describe 'Mattermost team creation' do
before do
allow(Settings.mattermost).to receive_messages(enabled: mattermost_enabled)
visit new_group_path
end
context 'Mattermost enabled' do
let(:mattermost_enabled) { true }
it 'displays a team creation checkbox' do
expect(page).to have_selector('#group_create_chat_team')
end
it 'checks the checkbox by default' do
expect(find('#group_create_chat_team')['checked']).to eq(true)
end
it 'updates the team URL on graph path update', :js do
out_span = find('span[data-bind-out="create_chat_team"]')
expect(out_span.text).to be_empty
fill_in('group_path', with: 'test-group')
expect(out_span.text).to eq('test-group')
end
end
context 'Mattermost disabled' do
let(:mattermost_enabled) { false }
it 'doesnt show a team creation checkbox if Mattermost not enabled' do
expect(page).not_to have_selector('#group_create_chat_team')
end
end
end
end
describe 'create a nested group' do
......
import BindInOut from '~/behaviors/bind_in_out';
import ClassSpecHelper from '../helpers/class_spec_helper';
describe('BindInOut', function () {
describe('.constructor', function () {
beforeEach(function () {
this.in = {};
this.out = {};
this.bindInOut = new BindInOut(this.in, this.out);
});
it('should set .in', function () {
expect(this.bindInOut.in).toBe(this.in);
});
it('should set .out', function () {
expect(this.bindInOut.out).toBe(this.out);
});
it('should set .eventWrapper', function () {
expect(this.bindInOut.eventWrapper).toEqual({});
});
describe('if .in is an input', function () {
beforeEach(function () {
this.bindInOut = new BindInOut({ tagName: 'INPUT' });
});
it('should set .eventType to keyup ', function () {
expect(this.bindInOut.eventType).toEqual('keyup');
});
});
describe('if .in is a textarea', function () {
beforeEach(function () {
this.bindInOut = new BindInOut({ tagName: 'TEXTAREA' });
});
it('should set .eventType to keyup ', function () {
expect(this.bindInOut.eventType).toEqual('keyup');
});
});
describe('if .in is not an input or textarea', function () {
beforeEach(function () {
this.bindInOut = new BindInOut({ tagName: 'SELECT' });
});
it('should set .eventType to change ', function () {
expect(this.bindInOut.eventType).toEqual('change');
});
});
});
describe('.addEvents', function () {
beforeEach(function () {
this.in = jasmine.createSpyObj('in', ['addEventListener']);
this.bindInOut = new BindInOut(this.in);
this.addEvents = this.bindInOut.addEvents();
});
it('should set .eventWrapper.updateOut', function () {
expect(this.bindInOut.eventWrapper.updateOut).toEqual(jasmine.any(Function));
});
it('should call .addEventListener', function () {
expect(this.in.addEventListener)
.toHaveBeenCalledWith(
this.bindInOut.eventType,
this.bindInOut.eventWrapper.updateOut,
);
});
it('should return the instance', function () {
expect(this.addEvents).toBe(this.bindInOut);
});
});
describe('.updateOut', function () {
beforeEach(function () {
this.in = { value: 'the-value' };
this.out = { textContent: 'not-the-value' };
this.bindInOut = new BindInOut(this.in, this.out);
this.updateOut = this.bindInOut.updateOut();
});
it('should set .out.textContent to .in.value', function () {
expect(this.out.textContent).toBe(this.in.value);
});
it('should return the instance', function () {
expect(this.updateOut).toBe(this.bindInOut);
});
});
describe('.removeEvents', function () {
beforeEach(function () {
this.in = jasmine.createSpyObj('in', ['removeEventListener']);
this.updateOut = () => {};
this.bindInOut = new BindInOut(this.in);
this.bindInOut.eventWrapper.updateOut = this.updateOut;
this.removeEvents = this.bindInOut.removeEvents();
});
it('should call .removeEventListener', function () {
expect(this.in.removeEventListener)
.toHaveBeenCalledWith(
this.bindInOut.eventType,
this.updateOut,
);
});
it('should return the instance', function () {
expect(this.removeEvents).toBe(this.bindInOut);
});
});
describe('.initAll', function () {
beforeEach(function () {
this.ins = [0, 1, 2];
this.instances = [];
spyOn(document, 'querySelectorAll').and.returnValue(this.ins);
spyOn(Array.prototype, 'map').and.callThrough();
spyOn(BindInOut, 'init');
this.initAll = BindInOut.initAll();
});
ClassSpecHelper.itShouldBeAStaticMethod(BindInOut, 'initAll');
it('should call .querySelectorAll', function () {
expect(document.querySelectorAll).toHaveBeenCalledWith('*[data-bind-in]');
});
it('should call .map', function () {
expect(Array.prototype.map).toHaveBeenCalledWith(jasmine.any(Function));
});
it('should call .init for each element', function () {
expect(BindInOut.init.calls.count()).toEqual(3);
});
it('should return an array of instances', function () {
expect(this.initAll).toEqual(jasmine.any(Array));
});
});
describe('.init', function () {
beforeEach(function () {
spyOn(BindInOut.prototype, 'addEvents').and.callFake(function () { return this; });
spyOn(BindInOut.prototype, 'updateOut').and.callFake(function () { return this; });
this.init = BindInOut.init({}, {});
});
ClassSpecHelper.itShouldBeAStaticMethod(BindInOut, 'init');
it('should call .addEvents', function () {
expect(BindInOut.prototype.addEvents).toHaveBeenCalled();
});
it('should call .updateOut', function () {
expect(BindInOut.prototype.updateOut).toHaveBeenCalled();
});
describe('if no anOut is provided', function () {
beforeEach(function () {
this.anIn = { dataset: { bindIn: 'the-data-bind-in' } };
spyOn(document, 'querySelector');
BindInOut.init(this.anIn);
});
it('should call .querySelector', function () {
expect(document.querySelector)
.toHaveBeenCalledWith(`*[data-bind-out="${this.anIn.dataset.bindIn}"]`);
});
});
});
});
require 'spec_helper'
describe ChatTeam, type: :model do
# Associations
it { is_expected.to belong_to(:namespace) }
# Fields
it { is_expected.to respond_to(:name) }
it { is_expected.to respond_to(:team_id) }
end
......@@ -14,6 +14,7 @@ describe Group, models: true do
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_many(:uploads).dependent(:destroy) }
it { is_expected.to have_one(:chat_team) }
describe '#members & #requesters' do
let(:requester) { create(:user) }
......
......@@ -4,11 +4,11 @@ describe Groups::CreateService, '#execute', services: true do
let!(:user) { create(:user) }
let!(:group_params) { { path: "group_path", visibility_level: Gitlab::VisibilityLevel::PUBLIC } }
subject { service.execute }
describe 'visibility level restrictions' do
let!(:service) { described_class.new(user, group_params) }
subject { service.execute }
context "create groups without restricted visibility level" do
it { is_expected.to be_persisted }
end
......@@ -24,8 +24,6 @@ describe Groups::CreateService, '#execute', services: true do
let!(:group) { create(:group) }
let!(:service) { described_class.new(user, group_params.merge(parent_id: group.id)) }
subject { service.execute }
context 'as group owner' do
before { group.add_owner(user) }
......@@ -40,4 +38,20 @@ describe Groups::CreateService, '#execute', services: true do
end
end
end
describe 'creating a mattermost team' do
let!(:params) { group_params.merge(create_chat_team: "true") }
let!(:service) { described_class.new(user, params) }
before do
Settings.mattermost['enabled'] = true
end
it 'create the chat team with the group' do
allow_any_instance_of(Mattermost::Team).to receive(:create)
.and_return({ 'name' => 'tanuki', 'id' => 'lskdjfwlekfjsdifjj' })
expect { subject }.to change { ChatTeam.count }.from(0).to(1)
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