From 49a42614082f057512eb4edc440b5b06e24472ef Mon Sep 17 00:00:00 2001 From: Murilo Dal Ri Date: Tue, 9 Apr 2024 14:01:19 +0100 Subject: [PATCH] Add Rubocop to default Rake task and apply fixes --- Rakefile | 9 +++++++ lib/bank_holidays.rb | 8 +++--- lib/github_fetcher.rb | 2 +- lib/message_builder.rb | 26 ++++++++++---------- lib/seal.rb | 6 ++--- lib/slack_poster.rb | 47 ++++++++++++++++++------------------ lib/team_builder.rb | 2 +- spec/bank_holidays_spec.rb | 2 +- spec/message_builder_spec.rb | 2 +- spec/seal_spec.rb | 10 ++++---- spec/spec_helper.rb | 4 +-- spec/team_builder_spec.rb | 22 ++++++++--------- 12 files changed, 75 insertions(+), 65 deletions(-) diff --git a/Rakefile b/Rakefile index c8889725..ebad37e0 100644 --- a/Rakefile +++ b/Rakefile @@ -17,7 +17,16 @@ rescue LoadError # no jsonlint available end +begin + require "rubocop/rake_task" + + RuboCop::RakeTask.new +rescue LoadError + # no rubocop available +end + task default: %i[ jsonlint + rubocop spec ] diff --git a/lib/bank_holidays.rb b/lib/bank_holidays.rb index 1cc8c3b6..fbb12651 100644 --- a/lib/bank_holidays.rb +++ b/lib/bank_holidays.rb @@ -8,12 +8,12 @@ def self.bank_holidays .then { JSON.parse _1 } .dig("england-and-wales", "events") .map { |e| Date.iso8601(e["date"]) } - rescue StandardError => e - puts "Error fetching bank holidays JSON: #{e.message}" - [] + rescue StandardError => e + puts "Error fetching bank holidays JSON: #{e.message}" + [] end def bank_holiday? Date.bank_holidays.include? self end -end \ No newline at end of file +end diff --git a/lib/github_fetcher.rb b/lib/github_fetcher.rb index 8c746459..547f5157 100644 --- a/lib/github_fetcher.rb +++ b/lib/github_fetcher.rb @@ -160,7 +160,7 @@ def has_sca_sast_scans?(repo) snyk_string = "uses: alphagov/govuk-infrastructure/.github/workflows/snyk-security.yml@main" ci_file.include?(sca_string) && ci_file.include?(sast_string) && ci_file.include?(snyk_string) - rescue Octokit::NotFound => e + rescue Octokit::NotFound true # if a CI file is not present assume no scans are needed end end diff --git a/lib/message_builder.rb b/lib/message_builder.rb index 59b00bbd..90ff8da0 100644 --- a/lib/message_builder.rb +++ b/lib/message_builder.rb @@ -3,9 +3,9 @@ require_relative "message" class MessageBuilder - TEMPLATE_DIR = Pathname.new(File.join(File.dirname(__FILE__), *%w[.. templates])) + TEMPLATE_DIR = File.join(File.dirname(__FILE__), *%w[.. templates]) - attr_accessor :pull_requests, :report, :mood, :poster_mood + attr_accessor :report, :mood, :poster_mood def initialize(team, mode) @team = team @@ -58,15 +58,15 @@ def build_ci_message end def ci_message - @repos = check_team_repos_ci.reject { |_,v| v }.keys + @repos = check_team_repos_ci.reject { |_, v| v }.keys return nil if @repos.empty? - template_file = TEMPLATE_DIR + "list_ci_issues.text.erb" - ERB.new(template_file.read, trim_mode: '-').result(binding).strip + template_file = Pathname.new("#{TEMPLATE_DIR}/list_ci_issues.text.erb") + ERB.new(template_file.read, trim_mode: "-").result(binding).strip end - def pr_date(pr) - pr[:marked_ready_for_review_at] || pr[:created] + def pr_date(pull_request) + pull_request[:marked_ready_for_review_at] || pull_request[:created] end def github_fetcher @@ -128,8 +128,8 @@ def dependapanda_message .map { |pr| pr.merge(pr_link: pr[:link], pr_title: pr[:title]) }.reverse end - template_file = TEMPLATE_DIR + "dependapanda.text.erb" - ERB.new(template_file.read, trim_mode: '-').result(binding).strip + template_file = Pathname.new("#{TEMPLATE_DIR}/dependapanda.text.erb") + ERB.new(template_file.read, trim_mode: "-").result(binding).strip end def comments(pull_request) @@ -154,10 +154,10 @@ def pr_plural(pr_count) end end - def present(pr, index) + def present(pull_request, index) @index = index - @pr = pr - @title = pr[:title] + @pr = pull_request + @title = pull_request[:title] @days = age_in_days(pr_date(@pr)) @thumbs_up = if (@pr[:thumbs_up]).positive? @@ -209,7 +209,7 @@ def apply_style(template_name) end def render(template_name) - template_file = TEMPLATE_DIR + "#{apply_style(template_name)}.text.erb" + template_file = Pathname.new("#{TEMPLATE_DIR}/#{apply_style(template_name)}.text.erb") ERB.new(template_file.read).result(binding).strip end diff --git a/lib/seal.rb b/lib/seal.rb index 3d238ea0..9b6742a0 100755 --- a/lib/seal.rb +++ b/lib/seal.rb @@ -47,11 +47,11 @@ def bark_at(team, mode: nil) rescue StandardError => e puts "Error barking at team '#{team.name}': #{e.message}" end - + def send_quotes_message(team, mode) today_is_quote_day = team.quotes_days.map(&:downcase).include?(Date.today.strftime("%A").downcase) - team_has_enabled_this_mode = team.public_send("#{mode}") - + team_has_enabled_this_mode = team.public_send(mode.to_s) + Message.new(team.quotes.sample) if today_is_quote_day && team_has_enabled_this_mode end end diff --git a/lib/slack_poster.rb b/lib/slack_poster.rb index 9368498a..d10c2dc9 100644 --- a/lib/slack_poster.rb +++ b/lib/slack_poster.rb @@ -1,5 +1,5 @@ class SlackPoster - attr_accessor :webhook_url, :poster, :mood, :mood_hash, :channel, :season_name + attr_accessor :webhook_url, :poster, :mood, :season_name def initialize(team_channel, mood) @webhook_url = ENV["SLACK_WEBHOOK"] @@ -48,28 +48,29 @@ def mood_hash end def assign_poster_settings - @mood_hash[:icon_emoji], @mood_hash[:username] = case @mood - when "panda" - [":panda_face:", "Dependapanda"] - when "informative" - [":#{@season_symbol}informative_seal:", "#{@season_name}Informative Seal"] - when "approval" - [":#{@season_symbol}seal_of_approval:", "#{@season_name}Seal of Approval"] - when "angry" - [":#{@season_symbol}angrier_seal:", "#{@season_name}Angry Seal"] - when "robot_face" - [":robot_face:", "Angry CI Robot"] - when "tea" - [":manatea:", "Tea Seal"] - when "charter" - [":happyseal:", "Team Charter Seal"] - when "fun-workstream" - [":wholesome-seal:", "It's ok Seal"] - when "govuk-green-team" - [":happybabyseal:", "Elephant Seal"] - else - raise "Bad mood: #{mood}." - end + @mood_hash[:icon_emoji], @mood_hash[:username] = + case @mood + when "panda" + [":panda_face:", "Dependapanda"] + when "informative" + [":#{@season_symbol}informative_seal:", "#{@season_name}Informative Seal"] + when "approval" + [":#{@season_symbol}seal_of_approval:", "#{@season_name}Seal of Approval"] + when "angry" + [":#{@season_symbol}angrier_seal:", "#{@season_name}Angry Seal"] + when "robot_face" + [":robot_face:", "Angry CI Robot"] + when "tea" + [":manatea:", "Tea Seal"] + when "charter" + [":happyseal:", "Team Charter Seal"] + when "fun-workstream" + [":wholesome-seal:", "It's ok Seal"] + when "govuk-green-team" + [":happybabyseal:", "Elephant Seal"] + else + raise "Bad mood: #{mood}." + end end def check_season diff --git a/lib/team_builder.rb b/lib/team_builder.rb index 16be7d90..2779ee16 100644 --- a/lib/team_builder.rb +++ b/lib/team_builder.rb @@ -101,6 +101,6 @@ def govuk_team_repos(team_channel) end def is_govuk_team?(team) - @govuk_data.any? { | repo | repo["team"] == "##{team}"} + @govuk_data.any? { |repo| repo["team"] == "##{team}" } end end diff --git a/spec/bank_holidays_spec.rb b/spec/bank_holidays_spec.rb index c775493e..8348e7fc 100644 --- a/spec/bank_holidays_spec.rb +++ b/spec/bank_holidays_spec.rb @@ -41,4 +41,4 @@ expect(Date.new(2018, 3, 28).bank_holiday?).to eq(false) end end -end \ No newline at end of file +end diff --git a/spec/message_builder_spec.rb b/spec/message_builder_spec.rb index 47fb731e..5fd836f9 100644 --- a/spec/message_builder_spec.rb +++ b/spec/message_builder_spec.rb @@ -320,4 +320,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/seal_spec.rb b/spec/seal_spec.rb index 86a03202..bbe73937 100644 --- a/spec/seal_spec.rb +++ b/spec/seal_spec.rb @@ -9,7 +9,7 @@ Team.new( slack_channel: "#lions", quotes: ["go lions!"], - quotes_days: ["Monday", "tuesday", "Wednesday"], + quotes_days: %w[Monday tuesday Wednesday], morning_seal_quotes: true, seal_prs: true, ) @@ -24,7 +24,7 @@ Team.new( slack_channel: "#cats", quotes: ["go cats!"], - quotes_days: ["yarn", "catnip", "nap", "scratches", "mischief"], + quotes_days: %w[yarn catnip nap scratches mischief], morning_seal_quotes: true, seal_prs: true, ) @@ -68,7 +68,7 @@ let(:teams) { [lions, cats] } it "posts quotes if a quotes day" do - Timecop.freeze(Time.local(2023, 04, 24)) # Monday + Timecop.freeze(Time.local(2023, 0o4, 24)) # Monday expect(Message).to receive(:new).with("go lions!").and_return(message) expect(SlackPoster).to receive(:new).with(lions.channel, anything) @@ -76,7 +76,7 @@ end it "doesn't posts quotes if not a quotes day" do - Timecop.freeze(Time.local(2023, 04, 27)) # Thursday + Timecop.freeze(Time.local(2023, 0o4, 27)) # Thursday expect(Message).not_to receive(:new).with("go lions!") expect(SlackPoster).not_to receive(:new).with(lions.channel, anything) @@ -91,7 +91,7 @@ end it "posts quotes if day downcased" do - Timecop.freeze(Time.local(2023, 04, 25)) # Tuesday + Timecop.freeze(Time.local(2023, 0o4, 25)) # Tuesday expect(Message).to receive(:new).with("go lions!").and_return(message) expect(SlackPoster).to receive(:new).with(lions.channel, anything) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 09875199..598e8f6f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -45,8 +45,8 @@ Timecop.return end - config.before do - allow($stdout).to receive(:puts) + config.before do + allow($stdout).to receive(:puts) end # The settings below are suggested to provide a good initial experience diff --git a/spec/team_builder_spec.rb b/spec/team_builder_spec.rb index b8945926..65e1900e 100644 --- a/spec/team_builder_spec.rb +++ b/spec/team_builder_spec.rb @@ -40,15 +40,15 @@ "This is a quote", "This is also a quote", ], - "quotes_days" => [ - "Monday", - "Tuesday", - "Wednesday", - "Thursday", - "Friday", - ], - "repos" => [ - "Africa" + "quotes_days" => %w[ + Monday + Tuesday + Wednesday + Thursday + Friday + ], + "repos" => %w[ + Africa ], }, "tigers" => { @@ -65,8 +65,8 @@ }, "govuk-generic-team" => { "channel" => "#govuk-generic-team", - "repos" => [ - "generic-repo" + "repos" => %w[ + generic-repo ], }, ))