This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
cia.rb
90 lines (80 loc) · 2.38 KB
/
cia.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
class Service::CIA < Service
string :address, :project, :branch
boolean :long_url
def receive_push
repository =
if !(name = data['project'].to_s).empty?
name
else
payload['repository']['name']
end
branch =
if !(branch = data['branch'].to_s).empty?
branch % branch_name
else
ref_name
end
commits = payload['commits']
if commits.size > 5
message = build_cia_commit(repository, branch, payload['after'], commits.last, commits.size - 1)
deliver(message)
else
commits.each do |commit|
sha1 = commit['id']
message = build_cia_commit(repository, branch, sha1, commit)
deliver(message)
end
end
end
attr_writer :xmlrpc_server
def xmlrpc_server
@xmlrpc_server ||= begin
XMLRPC::Client.new2(
(address = data['address'].to_s).present? ?
address : 'http://cia.vc')
end
end
def deliver(message)
xmlrpc_server.call("hub.deliver", message)
rescue StandardError => err
if $!.to_s =~ /content\-type/i || $!.to_s =~ /HTTP\-Error/i
raise_config_error "Check the CIA Address: #{$!.message}"
else
raise
end
end
def build_cia_commit(repository, branch, sha1, commit, size = 1)
log = commit['message']
log << " (+#{size} more commits...)" if size > 1
dt = DateTime.parse(commit['timestamp']).new_offset
timestamp = Time.send(:gm, dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec).to_i
files = commit['modified'] + commit['added'] + commit['removed']
tiny_url = data['long_url'].to_i == 1 ? commit['url'] : shorten_url(commit['url'])
log << " - #{tiny_url}"
<<-MSG
<message>
<generator>
<name>github</name>
<version>1</version>
<url>http://www.github.com</url>
</generator>
<source>
<project>#{repository}</project>
<branch>#{branch}</branch>
</source>
<timestamp>#{timestamp}</timestamp>
<body>
<commit>
<author>#{commit['author']['name']}</author>
<revision>#{sha1[0..6]}</revision>
<log>#{CGI.escapeHTML(log)}</log>
<url>#{commit['url']}</url>
<files>
<file> #{files.join("</file>\n<file>")} </file>
</files>
</commit>
</body>
</message>
MSG
end
end