Skip to content

Commit

Permalink
Add Rubocop to default Rake task and apply fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MuriloDalRi committed Apr 9, 2024
1 parent 351c50c commit 49a4261
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 65 deletions.
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
8 changes: 4 additions & 4 deletions lib/bank_holidays.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
2 changes: 1 addition & 1 deletion lib/github_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 13 additions & 13 deletions lib/message_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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?
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/seal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 24 additions & 23 deletions lib/slack_poster.rb
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/team_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/bank_holidays_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
expect(Date.new(2018, 3, 28).bank_holiday?).to eq(false)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/message_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,4 @@
end
end
end
end
end
10 changes: 5 additions & 5 deletions spec/seal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down Expand Up @@ -68,15 +68,15 @@
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)

subject.bark(mode: "morning_seal_quotes")
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)

Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions spec/team_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand All @@ -65,8 +65,8 @@
},
"govuk-generic-team" => {
"channel" => "#govuk-generic-team",
"repos" => [
"generic-repo"
"repos" => %w[
generic-repo
],
},
))
Expand Down

0 comments on commit 49a4261

Please sign in to comment.