Uh oh!
There was an error while loading. Please reload this page.
forked from github/github-services
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-services.rb
More file actions
Latest commit
148 lines (128 loc) · 4.21 KB
/
Copy pathgithub-services.rb
File metadata and controls
148 lines (128 loc) · 4.21 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
requireFile.expand_path('../config/load',__FILE__)
set:run,true
set:environment,:production
set:port,ARGV.first || 8080
HOSTNAME=`hostname`.chomp
begin
require'mongrel'
set:server,'mongrel'
rescueLoadError
begin
require'thin'
set:server,'thin'
rescueLoadError
set:server,'webrick'
end
end
begin
require'system_timer'
ServiceTimeout=SystemTimer
rescueLoadError
require'timeout'
ServiceTimeout=Timeout
end
moduleGitHub
classServiceTimeoutError < Timeout::Error
end
# Raised when an unexpected error occurs during service hook execution.
classServiceError < StandardError
attr_reader:original_exception
definitialize(message,original_exception=nil)
original_exception=messageifmessage.kind_of?(Exception)
@original_exception=original_exception
super(message)
end
end
# Raised when a service hook fails due to bad configuration. Services that
# fail with this exception may be automatically disabled.
classServiceConfigurationError < ServiceError
end
defservice(name)
post"/#{name}/"do
begin
data=JSON.parse(params[:data])
payload=parse_payload(params[:payload])
ServiceTimeout.timeout(20,ServiceTimeoutError){yielddata,payload}
status200
""
rescueGitHub::ServiceConfigurationError=>boom
status400
boom.message
rescueGitHub::ServiceTimeoutError=>boom
status504
"Service Timeout"
rescueObject=>boom
# redact sensitive info in hook_data hash
hook_data=data || params[:data]
hook_payload=payload || params[:payload]
#%w[password token].each { |key| hook_data[key] &&= '<redacted>' }
owner=hook_payload['repository']['owner']['name']rescuenil
repo=hook_payload['repository']['name']rescuenil
report_exceptionboom,
:hook_name=>name,
:hook_data=>hook_data.inspect,
:hook_payload=>hook_payload.inspect,
:user=>owner,
:repo=>"#{owner}/#{repo}"
status500
"ERROR"
end
end
end
defparse_payload(json)
payload=JSON.parse(json)
payload['ref_name']=payload['ref'].to_s.sub(/\Arefs\/(heads|tags)\//,'')
payload
end
defshorten_url(url)
ServiceTimeout.timeout(6,ServiceTimeoutError)do
short=Net::HTTP.get("api.bit.ly","/shorten?version=2.0.1&longUrl=#{url}&login=github&apiKey=R_261d14760f4938f0cda9bea984b212e4")
short=JSON.parse(short)
short["errorCode"].zero? ? short["results"][url]["shortUrl"] : url
end
rescueServiceTimeoutError
url
end
defreport_exception(exception,other)
backtrace=Array(exception.backtrace)[0..500]
data={
'app'=>'github-services',
'type'=>'exception',
'class'=>exception.class.to_s,
'server'=>HOSTNAME,
'message'=>exception.message[0..254],
'backtrace'=>backtrace.join("\n"),
'rollup'=>Digest::MD5.hexdigest(exception.class.to_s + backtrace[0])
}
ifexception.kind_of?(GitHub::ServiceError)
ifexception.original_exception
data['original_class']=exception.original_exception.to_s
data['backtrace']=exception.original_exception.backtrace.join("\n")
data['message']=exception.original_exception.message[0..254]
end
elsif !exception.kind_of?(GitHub::ServiceTimeoutError)
data['original_class']=data['class']
data['class']='GitHub::ServiceError'
end
# optional
other.each{ |key,value| data[key.to_s]=value.to_s}
ifHOSTNAME =~ /^sh1\.(rs|stg)\.github\.com$/
# run only in github's production environment
Net::HTTP.new('haystack',80).
post('/async',"json=#{Rack::Utils.escape(data.to_json)}")
else
$stderr.putsdata['message']
$stderr.putsdata['backtrace']
end
rescue=>boom
$stderr.puts"reporting exception failed:"
$stderr.puts"#{boom.class}: #{boom}"
$stderr.puts"#{boom.backtrace.join("\n")}"
# swallow errors
end
end
includeGitHub
get"/"do
"ok"
end
Dir["#{File.dirname(__FILE__)}/services/**/*.rb"].each{ |service| loadservice}