-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #87 Add some SlackNotificationService tests and gral Rubocop warnings cleanup #89
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to avoid wrong initializations |
||
|
||
@extra_params = params || {} | ||
@slack_bot = SlackBot.new(channel: channel(params)) | ||
@pr = PullRequest.new(params) | ||
end | ||
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rubocop fix |
||
channel = "##{build_channel(lang, repository_info)}" | ||
|
||
if search_channel(channel) | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rubocop fix |
||
Rails.logger.error("Error #{e.inspect} for channel #{channel}") | ||
nil | ||
end | ||
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rubocop fixes |
||
|
||
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 | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||||
require 'rails_helper' | ||||||||
|
||||||||
describe SlackNotificationService do | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe. My idea was to explore the class code by creating scenarios. First, started with initialization scenarios, quickly finding issues with nil errors when Hash structure is not the expected one. Added a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I like this way |
||||||||
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 } | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about this. Isn't it better if we only create the object in the "subject", and after call the method? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure what style is better. The subject under test in this scenario is the method |
||||||||
|
||||||||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trying to avoid nil errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Params never gonna be nil so why this can raise an error? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know anything about the Slack connection/gem/client/etc usages (I still don't).
I just started to test the class in isolation and noticed that it was raising a lot errors because it assumes a complex parameter tree structure. I usually try to validate initialization/params just a bit, to avoid ugly bugs later in the code (most of the time,
undefined xxxx for nil
errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sometimes a complex input structure is a smell that could be improve/cleaned, sometimes with separated params or a Struct/PORO