Skip to content

Instantly share code, notes, and snippets.

@amolpujari
Created October 29, 2012 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save amolpujari/3974972 to your computer and use it in GitHub Desktop.
Save amolpujari/3974972 to your computer and use it in GitHub Desktop.
ruby based mail watcher to trigger jenkin jobs passing args
require 'net/imap'
class MailWatcher
Server = 'imap.gmail.com'
Username = ''
Password = ''
Folder = 'INBOX'
PollingInterval = 60
CheckFileLocation = "#{File.dirname(__FILE__)}/../../tmp/checks/"
def fetch
@imap = Net::IMAP.new Server, 993, true
@imap.login Username, Password
@imap.select Folder
@imap.search(["NOT", "SEEN"]).each do |message_id|
env = @imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
@subject = env.subject
return unless @subject.match(/^test .+/)
test_cases.each do |test_case_name|
File.open("#{CheckFileLocation}#{test_case_name}.check",'w') { |file| file.puts base_url }
end
mark_delete message_id
end
@imap.logout
@imap.disconnect
end
def start_watching
while(1) do
fetch
sleep PollingInterval
end
end
def base_url
@base_url ||= "base_url=#{@subject.match(/ on (https?\:\/\/[a-z\.]+)$/)[1]}" rescue 'base_url='
end
def test_cases
@test_cases = @subject.split(' on http').first[5..-1].strip.split(',').collect{ |test_case| test_case.strip.gsub(' ','_')}
end
def mark_delete message_id
@imap.store(message_id, "+FLAGS", [:Seen])
@imap.store(message_id, "+FLAGS", [:Deleted])
end
end
Process.daemon(true)
MailWatcher.new.start_watching
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment