-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#13) generate slack channel for project
- Loading branch information
Showing
7 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Projects | ||
# Runs create channel slack for project in asyncronous manner. | ||
class CreateSlackChannelJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(project_id) | ||
project = Project.find(project_id) | ||
Ops::Projects::GenerateProjectSlackChannel.call(project: project) | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
app/operations/ops/projects/generate_project_slack_channel.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ops | ||
module Projects | ||
# This is the operation of creating a project channel to slack | ||
class GenerateProjectSlackChannel < BaseOperation | ||
step :create_slack_channel! | ||
step :invite_members! | ||
|
||
private | ||
|
||
def create_slack_channel!(_ctx, project:, **) | ||
SlackService.new(ENV['SLACK_TOKEN']).create_channel(project.slug) | ||
end | ||
|
||
def invite_members!(_ctx, project:, **) | ||
members_slack_channel(project).each do |user| | ||
SlackService.new(ENV['SLACK_TOKEN']).invite_to_channel(project.slug, user.email) | ||
end | ||
end | ||
|
||
def members_slack_channel(project) | ||
project.users + [project.author, User.joins(:roles).where(roles: { type: 'Roles::Mentor' }).sample] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe Projects::CreateSlackChannelJob do | ||
describe '#perform' do | ||
let(:project) { create(:project) } | ||
|
||
it 'calls Projects::Kickoff operation' do | ||
expect(Ops::Projects::GenerateProjectSlackChannel).to receive(:call).with(project: project) | ||
described_class.perform_now(project.id) | ||
end | ||
end | ||
end |
63 changes: 63 additions & 0 deletions
63
spec/operations/ops/projects/generate_project_slack_channel_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe Ops::Projects::GenerateProjectSlackChannel do | ||
subject { described_class } | ||
let!(:idea) { create(:idea, :with_project) } | ||
let!(:service) { double } | ||
let!(:user) { create(:user, :developer, :active) } | ||
let!(:channel) { idea.project.slug } | ||
before { create(:project_user, project: idea.project, user: user) } | ||
|
||
describe '#call' do | ||
before do | ||
allow(SlackService).to receive(:new).with(any_args).and_return(service) | ||
end | ||
let!(:params) { { project: idea.project } } | ||
|
||
context 'create Slack channel for project' do | ||
it 'create channel' do | ||
expect(service).to receive(:create_channel).with(idea.project.slug) | ||
subject.call(params) | ||
end | ||
end | ||
|
||
context 'user was not invited before' do | ||
it 'invites member to Slack' do | ||
allow(service).to receive(:create_channel).with(any_args) | ||
expect(service).to receive(:invite_to_channel).with(channel, user.email) | ||
subject.call(params) | ||
end | ||
end | ||
|
||
context 'user was invited before' do | ||
it 'handles exception properly' do | ||
allow(service).to receive(:create_channel).with(any_args) | ||
expect(service) | ||
.to receive(:invite_to_channel) | ||
.with(channel, user.email) | ||
.and_raise( | ||
Slack::Web::Api::Errors::SlackError, | ||
'cant_invite_self' | ||
) | ||
subject.call(params) | ||
end | ||
end | ||
|
||
context 'some other exception occured' do | ||
it 'handles exception properly' do | ||
allow(service).to receive(:create_channel).with(any_args) | ||
allow(service) | ||
.to receive(:invite_to_channel) | ||
.with(channel, user.email) | ||
.and_raise( | ||
SlackIntegration::FailedApiCallException, | ||
'some other message' | ||
) | ||
expect { subject.call(params) } | ||
.to raise_error SlackIntegration::FailedApiCallException, 'some other message' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters