forked from github/github-services
/
lighthouse.rb
47 lines (40 loc) · 1.82 KB
/
lighthouse.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
class Service::Lighthouse < Service
string :subdomain, :project_id, :token
boolean :private, :send_only_ticket_commits
def receive_push
# matches string with square braces with content starting with # and a digit.
check_for_lighthouse_flags = /\[#\d.+?\]/
payload['commits'].each do |commit|
next if commit['message'] =~ /^x /
next if data['send_only_ticket_commits'] == false && (commit['message'] =~ check_for_lighthouse_flags).nil?
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)
diff = YAML.dump([]) if data['private']
title = "Changeset [%s] by %s" % [commit_id, commit['author']['name']]
body = "#{commit['message']}\n#{commit['url']}"
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://#{data['subdomain']}.lighthouseapp.com"
begin
http.basic_auth data['token'], 'x'
http.headers['Content-Type'] = 'application/xml'
http_post '%s/projects/%d/changesets.xml' % [
"http://#{data['subdomain']}.lighthouseapp.com", data['project_id'].to_i],
changeset_xml
rescue URI::InvalidURIError
raise_config_error "Invalid subdomain: #{data['subdomain']}"
end
end
end
end