-
Notifications
You must be signed in to change notification settings - Fork 3
/
tangle.rb
64 lines (49 loc) · 1.31 KB
/
tangle.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
require "sinatra/base"
require "sinatra/reloader"
require "net/http"
require 'json/pure'
require 'yaml'
require 'pp'
class Tangle < Sinatra::Base
register Sinatra::Reloader
get '/' do
"TorqueBox says 'hi'."
end
post '/' do
if ( params[:token] != ENV['TANGLE_TOKEN'] )
$stderr.puts "Invalid token: #{params[:token]}"
return 404
end
payload = JSON params[:payload]
#pp payload
repo = payload['repository']['name']
ref = payload['ref']
branch_name = nil
if ( ref =~ %r(^refs/heads/(.*)$) )
branch_name = $1
end
config = YAML.load( File.read( File.dirname( __FILE__ ) + '/wiring.yml' ) )
repo_config = config['repositories'][repo]
if ( repo_config )
branch_config = repo_config[branch_name]
if ( branch_config )
[ branch_config ].flatten.each do |trigger_url|
dispatch_trigger( trigger_url )
end
end
end
end
def dispatch_trigger(trigger_url)
token = ENV['CLOUDBEES_TOKEN']
token_part = ""
if ( trigger_url =~ /\?/ )
token_part = "&token=#{token}"
else
token_part = "?token=#{token}"
end
full_url = "#{trigger_url}#{token_part}"
$stderr.puts "Dispatch #{full_url}"
`curl '#{full_url}' --insecure --silent`
$stderr.puts "Dispatch complete #{$?}"
end
end