This repository has been archived by the owner on Jan 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
reviewthis.rb
102 lines (85 loc) · 2.56 KB
/
reviewthis.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'rubygems'
require 'sinatra'
require 'json'
require 'mustache/sinatra'
require 'pony'
require 'octopussy'
configure do
set :mustache, {
:views => 'views/',
:templates => 'templates/'
}
# regex's
USER = /[^a-z0-9_]@([a-z0-9_]+)/i
HASH = /[^a-z0-9_]#([a-z0-9_]+)/i # not used yet, but perhaps soon?
REVIEW = /[^a-z0-9_](#reviewthis)[^a-z0-9_]+/i
EMAIL = /\b([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b/i
end
# production vars
configure :production do
# only run on Heroku
set :from, ENV['SENDGRID_USERNAME']
set :via, :smtp
set :via_options, {
:address => "smtp.sendgrid.net",
:port => "25",
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN'],
}
end
# development vars
configure :development, :test do
set :from, 'reviewthis@localhost'
set :via, :sendmail
set :via_options, {}
end
helpers do
# mail helper. Thnx Pony!
def mail(vars)
body = mustache :email, {}, vars
html_body = mustache :email_html, {}, vars
Pony.mail(:to => vars[:email], :from => options.from, :subject => "[#{vars[:repo_name]}] code review request from #{vars[:commit_author]}", :body => body,:html_body => html_body, :via => options.via, :via_options => options.via_options)
end
end
# test!
get '/' do
"#reviewthis @github!"
end
# the meat
post '/' do
push = JSON.parse(params[:payload])
# check every commit, not just the first
push['commits'].each do |commit|
message = commit['message']
# we've got a #reviewthis hash
if message.match(REVIEW)
# set some template vars
vars = {
:commit_id => commit['id'],
:commit_message => message,
:commit_timestamp => commit['timestamp'],
:commit_relative_time => Time.parse( commit['timestamp'] ).strftime("%m/%d/%Y at %I:%M%p"),
:commit_author => commit['author']['name'],
:commit_url => commit['url'],
:repo_name => push['repository']['name'],
:repo_url => push['repository']['url'],
}
# let's find all the github users
message.scan(USER) do |username|
user = Octopussy.user(username) # get the github user info
vars[:username] = user.name
vars[:email] = user.email
mail(vars)
end
# now let's find any email addresses
message.scan(EMAIL) do |email|
vars[:username] = email
vars[:email] = email
mail(vars)
end
end
end
return
end