Last active
June 25, 2023 07:53
-
-
Save mbround18/a2b1588f696f6483d855b0447f0cb4df to your computer and use it in GitHub Desktop.
TestRail notification to Slack
This file contains 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
require 'slack-ruby-client' # You will need to install the slack-ruby-client gem | |
require 'faye/websocket' # you will need to install the faye-websocket gem | |
require 'midi-smtp-server' # you will need to install the midi-smtp-server gem | |
require 'mail' # you will need to install the mail gem | |
class TestRailInterceptor < MidiSmtpServer::Smtpd | |
Slack.configure do |config| | |
config.token = ENV['SLACK_API_TOKEN'] # this in an environment variable and add in your Slack Api token for the bot desired | |
end | |
def start | |
@slack_bot = Slack::Web::Client.new | |
super | |
end | |
# Gather the incoming web data | |
def on_message_data_event(ctx) | |
mail = Mail.read_from_string(ctx[:message][:data]) # process the data to mail object | |
email_body = mail.body.decoded # grab the email body | |
url_to_assignment = email_body.strip.split("\n").select {|element| element =~ /index.php?/im } # extract the testrail url for the assignment | |
@slack_bot.users_list.to_hash['members'].each do |user| # iterate over the user list | |
if mail.to.include? user['profile']['email'] # select all destination users | |
@slack_bot.chat_postMessage(channel: user['id'], text: "#{mail.subject}! #{url_to_assignment.join(',')}", as_user: true) # send them the message about the assignment | |
end | |
end | |
send_email_to_user(mail) # pass the email along to the actial mail server | |
# Output for debug | |
logger.debug("[#{ctx[:envelope][:from]}] for recipient(s): [#{ctx[:envelope][:to]}]...") | |
# Just decode message ones to make sure, that this message ist readable | |
@mail = Mail.read_from_string(ctx[:message][:data]) | |
# handle incoming mail, just show the message source | |
logger.debug(@mail.to_s) | |
end | |
def send_email_to_user(mail) # Absolutely change the address to point to your email server and change the settings below as needed | |
mail.delivery_method :smtp, address: 'REPLACE ME', enable_ssl: false, openssl_verify_mode: 'none' # See https://github.com/mikel/mail for other settings | |
mail.deliver! # Finally pass the email along. | |
end | |
end | |
# Output for debug | |
puts "#{Time.now}: Starting TestRailInterceptor..." | |
# Create a new server instance listening at localhost interfaces 127.0.0.1:2525. Replace the 127.0.0.1 with 0.0.0.0 to make it open from anywhere. | |
# and accepting a maximum of 4 simultaneous connections | |
server = TestRailInterceptor.new(2525,'127.0.0.1',12) | |
# Start the server | |
server.start | |
# Run on server forever | |
server.join | |
# setup exit code | |
BEGIN { | |
at_exit { | |
# check to shutdown connection | |
if server | |
# Output for debug | |
puts "#{Time.now}: Shutdown TestRailInterceptor..." | |
# gracefully connections down | |
server.shutdown | |
# check once if some connection(s) need(s) more time | |
sleep 2 unless server.connections == 0 | |
# stop all threads and connections | |
server.stop | |
end | |
# Output for debug | |
puts "#{Time.now}: TestRailInterceptor down!" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment