-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ru
50 lines (47 loc) · 1.42 KB
/
config.ru
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
#!/usr/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'json'
require 'logger'
require_relative 'rtclient'
require_relative 'config'
class GitLabRT
TICKET_REGEXP = /#([0-9]+)/
LINKTYPE = 'ReferredToBy'
def initialize(rt, log:Logger.new(STDERR))
@rt = rt
@log = log
@mutex = Mutex.new
end
def update_links(ticket, type)
@mutex.synchronize do
current_links = @rt.call('ticket', ticket, 'links', format:'l').body.first[type].rt_split.sort
links = yield current_links
((current_links|links)-(current_links&links)).size.times do
@rt.call 'ticket', ticket, 'links', content:{type=>links.rt_join}
@log.info "Sent an update to RT"
end
end
end
def call(env)
req = Rack::Request.new env
begin
push = JSON.parse body=req.body.read
rescue JSON::ParserError
@log.error "Cannot parse request #{body}"
return [400, {}, []]
end
push['commits'].each do |commit|
url = commit['url']
commit['message'].scan(TICKET_REGEXP).flatten.each do |ticket|
update_links(ticket, LINKTYPE){|links| links + [url]}
@log.info "Updated #{LINKTYPE} links for ticket #{ticket} to include #{url}"
end
end
[200, {}, []]
end
end
http, path = RTClient.make_http_and_path RTURI, RTCERT
rt = RTClient.new http:http, path:path, user:RTUSER, pass:RTPASS
if !rt.login; STDERR.puts "login failure"; exit 1; end
run GitLabRT.new rt