Last active
December 11, 2024 07:46
-
-
Save amkisko/af7d648bd19436271601991133cd5e1c to your computer and use it in GitHub Desktop.
rspec errors collector and reporter to Slack
This file contains hidden or 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
class SuiteErrorsReporter | |
def self.report_errors(link_url:, link_label:, slack_webhook_url:, errors: fetch_examples_with_errors) | |
return if errors.empty? | |
WebMock.allow_net_connect! if defined?(WebMock) | |
message = "Test suite failed in <#{link_url}|#{link_label}>" | |
locations = errors.map(&:location) | |
Net::HTTP.post( | |
URI(slack_webhook_url), | |
{ | |
text: message, | |
attachments: [ | |
{ | |
color: "danger", | |
text: locations.join("\n") | |
} | |
] | |
}.to_json | |
) | |
WebMock.disable_net_connect! if defined?(WebMock) | |
end | |
def self.fetch_examples_with_errors | |
@@spec_suite_errors ||= [] | |
end | |
def self.append_example_with_error(example) | |
@@spec_suite_errors ||= [] | |
@@spec_suite_errors << example | |
end | |
end | |
RSpec.configure do |config| | |
config.after(:each) do |example| | |
if example.exception | |
SuiteErrorsReporter.append_example_with_error(example) | |
end | |
end | |
config.after(:suite) do | |
next if ENV["CI"].blank? | |
github_run_id = ENV["GITHUB_RUN_ID"] | |
if github_run_id.blank? | |
puts "GITHUB_RUN_ID is not set, skipping errors reporting" | |
next | |
end | |
slack_webhook_url = ENV["SLACK_WEBHOOK_URL"] | |
if slack_webhook_url.blank? | |
puts "SLACK_WEBHOOK_URL is not set, skipping errors reporting" | |
next | |
end | |
link_url = "#{ENV["GITHUB_SERVER_URL"]}/#{ENV["GITHUB_REPOSITORY"]}/actions/runs/#{github_run_id}" | |
link_label = "#{ENV["GITHUB_REPOSITORY"]}/#{github_run_id}" | |
SuiteErrorsReporter.report_errors( | |
link_url: link_url, | |
link_label: link_label, | |
slack_webhook_url: slack_webhook_url | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment