Created
October 29, 2012 17:16
-
-
Save amolpujari/3974972 to your computer and use it in GitHub Desktop.
ruby based mail watcher to trigger jenkin jobs passing args
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 '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