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
|
# File 'lib/build_buddy/server.rb', line 17
def on_connection(connection)
connection.each_request do |request|
request.body.to_s
case request.path
when '/webhook'
case request.method
when 'POST'
case request.["X-GitHub-Event"]
when 'pull_request'
payload_text = request.body.to_s
if !verify_signature(payload_text, request.["X-Hub-Signature"])
request.respond 500, "Signatures didn't match!"
else
payload = JSON.parse(payload_text)
action = payload['action']
pull_request = payload['pull_request']
info "Got pull request '#{action}' from #{forwarded_for(request)}"
case action
when 'opened', 'reopened', 'synchronize'
build_data = BuildData.new(
:type => :pull_request,
:pull_request => pull_request['number'],
:pull_request_title => pull_request['title'],
:flags => {},
:repo_sha => pull_request['head']['sha'],
:repo_full_name => pull_request['base']['repo']['full_name'],
:started_by => 'github')
info "Got #{action} pull request #{build_data.pull_request} from GitHub"
Celluloid::Actor[:scheduler].queue_a_build(build_data)
request.respond 200, "Building"
else
request.respond 200, "Ignoring"
end
end
when 'ping'
info "Got pinged from #{forwarded_for(request)}"
request.respond 200, "Running"
else
request.respond 404, "Event not supported"
end
else
request.respond 404, "Method not supported"
end
when /^\/build\/([0-9abcdef]{24})\/(log\.html|report\.html|[a-z_]+\.png)$/
if request.method != 'GET'
request.respond 404, "Method not supported"
return
end
build_id = $1
resource_name = $2
if build_id.nil? or resource_name.nil?
request.respond 404, "Not found"
return
end
resource_file_name = File.join(Config.build_output_dir, build_id, resource_name)
if !File.exist?(resource_file_name)
request.respond 404, "Not found"
return
end
if resource_name.end_with?('.html')
request.respond Reel::Response.new(200, { 'content-type' => 'text/html'}, File.open(resource_file_name, 'r'))
else
request.respond Reel::Response.new(200, { 'content-type' => 'image/png'}, File.open(resource_file_name, 'rb'))
end
when /^\/latest-build\/report.html$/
report_uri = Celluloid::Actor[:recorder].find_report_uri
unless report_uri.nil?
request.respond(302, { 'Location' => report_uri })
return
end
else
request.respond 404, "Not found"
end
end
end
|