Skip to content

Commit

Permalink
Merge pull request #89 from rootstrap/issue_87
Browse files Browse the repository at this point in the history
Issue #87 Add some SlackNotificationService tests and gral Rubocop warnings cleanup
  • Loading branch information
Cristian Molina authored Jan 24, 2023
2 parents 9855e0c + f31a165 commit d09633f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/services/pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PullRequest
:url, :body, :language, :repo_name

def initialize(params)
@info = params.dig(:pull_request)
@info = params.fetch(:pull_request, {})
@username = @info.dig(:user, :login)
@avatar_url = @info.dig(:user, :avatar_url)
@webhook_label = params.dig(:github_webhook, :label, :name)
Expand Down
18 changes: 10 additions & 8 deletions app/services/slack_notification_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class SlackNotificationService
attr_reader :action, :extra_params, :slack_bot, :pr

def initialize(params)
@action = params[:github_webhook][:action]
@extra_params = params
@action = params.dig(:github_webhook, :action)
raise(ArgumentError, 'No action param set') unless @action

@extra_params = params || {}
@slack_bot = SlackBot.new(channel: channel(params))
@pr = PullRequest.new(params)
end
Expand Down Expand Up @@ -56,7 +58,7 @@ def filter_on_hold_labeled_action

def channel(params)
lang = LANGUAGES[params.dig(:repository, :language)&.to_sym]
repository_info = params.dig(:repository)
repository_info = params[:repository]
channel = "##{build_channel(lang, repository_info)}"

if search_channel(channel)
Expand All @@ -68,7 +70,7 @@ def channel(params)

def search_channel(channel)
Slack::Web::Client.new.conversations_info(channel: channel)
rescue Exception => e
rescue StandardError => e
Rails.logger.error("Error #{e.inspect} for channel #{channel}")
nil
end
Expand Down Expand Up @@ -110,11 +112,11 @@ def filter_merged_action
slack_bot.add_merge_emoji matches
end

def js_channels(pr, lang)
return unless pr[:name]
def js_channels(pull_request, _lang)
return unless pull_request[:name]

repo_name = pr[:name].downcase
topics = pr[:topics].present? ? pr[:topics] : []
repo_name = pull_request[:name].downcase
topics = pull_request[:topics].present? ? pull_request[:topics] : []

js_repo_name(repo_name, topics)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/api/v1/github_webhook/filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def expect_not_notification
end

def mock_channel_response(return_value)
allow_any_instance_of(SlackNotificationService).to receive(:search_channel)
.and_return(return_value)
allow_any_instance_of(SlackNotificationService)
.to receive(:search_channel)
.and_return(return_value)
end

74 changes: 74 additions & 0 deletions spec/services/slack_notification_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require 'rails_helper'

describe SlackNotificationService do
describe 'initialization' do
subject { described_class.new({}) }

it 'raise an error if no action is set in params' do
expect { subject }.to raise_error(ArgumentError)
end
end

describe '#send_notification' do
subject { described_class.new(params).send_notification }

let(:username) { Faker::Internet.username }
let!(:user) do
User.create(github_name: username, blacklisted: false)
end

context 'with valid params' do
let(:random_action) { described_class::ACTION_METHODS.keys.sample }
let(:channel_name) { described_class::DEFAULT_CHANNEL }
let(:some_language) { 'Node' }
let(:params) do
{
github_webhook: {
action: random_action,
label: { name: Faker::Internet.slug(words: '_') }
},
channel: channel_name,
pull_request: {
user: { login: username },
body: Faker::Lorem.sentence
},
repository: {
name: Faker::Internet.slug,
language: some_language
}
}
end

before do
stub_request(:post, 'https://slack.com/api/conversations.list')
.to_return(status: 200, body: '', headers: {})

stub_request(:post, 'https://slack.com/api/chat.postMessage')
.with(
body: {
'channel': channel_name,
'text': ' :nodejs:',
'username': username
}
).to_return(status: 200, body: '', headers: {})
end

it 'sends the message' do
expect_any_instance_of(described_class)
.to receive(:send)
.with(described_class::ACTION_METHODS[random_action])
.and_call_original
subject
end

context 'with a PR from a blacklisted user' do
let!(:user) { User.create(github_name: username, blacklisted: true) }

it 'does NOT send the message' do
expect_any_instance_of(described_class).not_to receive(:send)
subject
end
end
end
end
end

0 comments on commit d09633f

Please sign in to comment.