-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.rb
More file actions
71 lines (55 loc) · 2.41 KB
/
app.rb
File metadata and controls
71 lines (55 loc) · 2.41 KB
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
enable :raise_errors
configure do
if File.exist?("./config.yml")
yaml = YAML.load_file("./config.yml")
ENV['LIGHTHOUSE_ACCOUNT'] = yaml['account']
ENV['LIGHTHOUSE_TOKEN'] = yaml['token']
ENV['LIGHTHOUSE_PROJECT'] = yaml['project']
end
end
get '/' do
'Spree patch monitor is online.'
end
post '/' do
payload = JSON.parse(params[:payload])
payload['commits'].each do |commit|
next if commit['message'] =~ /^x /
commit_id = commit['id']
added = commit['added'].map { |f| ['A', f] }
removed = commit['removed'].map { |f| ['R', f] }
modified = commit['modified'].map { |f| ['M', f] }
diff = YAML.dump(added + removed + modified)
# filter out an LH ticket commands (we're only going to allow certain operations through commit msgs)
next unless regex_match = /\[#[0-9]*/.match(commit['message'])
ticket_id = regex_match[0].gsub('[#', '')
commit['message'] = commit['message'].gsub(/\[#[^]]*\]/, '')
title = "Changeset [%s] by %s" % [commit_id, commit['author']['name']]
body = "#{commit['message']}\n#{commit['url']}\n[##{ticket_id}]"
changeset_xml = <<-XML.strip
<changeset>
<title>#{CGI.escapeHTML(title)}</title>
<body>#{CGI.escapeHTML(body)}</body>
<changes type="yaml">#{CGI.escapeHTML(diff)}</changes>
<committer>#{CGI.escapeHTML(commit['author']['name'])}</committer>
<revision>#{CGI.escapeHTML(commit_id)}</revision>
<changed-at type="datetime">#{CGI.escapeHTML(commit['timestamp'])}</changed-at>
</changeset>
XML
account = "http://#{ENV['LIGHTHOUSE_ACCOUNT']}.lighthouseapp.com"
url = URI.parse('%s/projects/%d/changesets.xml' % [account, ENV['LIGHTHOUSE_PROJECT']])
req = Net::HTTP::Post.new(url.path)
req.basic_auth "#{ENV['LIGHTHOUSE_TOKEN']}", 'x'
req.body = changeset_xml
req.set_content_type('application/xml')
Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
# udpate the lighthouse ticket and mark as patched (when applicable)
Lighthouse.token = ENV['LIGHTHOUSE_TOKEN']
Lighthouse.account = ENV['LIGHTHOUSE_ACCOUNT']
project = Lighthouse::Project.find(ENV['LIGHTHOUSE_PROJECT'])
ticket = Lighthouse::Ticket.find(ticket_id, :params => {:project_id => ENV['LIGHTHOUSE_PROJECT']})
next unless ticket and %w{new open reopened hold stalled}.include?(ticket.state)
ticket.state = "patched"
ticket.save
end
"Yay!"
end