forked from defunkt/cijoe
/
server.rb
114 lines (93 loc) · 2.84 KB
/
server.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'sinatra/base'
require 'erb'
require 'json'
class CIJoe
class Server < Sinatra::Base
attr_reader :joe
dir = File.dirname(File.expand_path(__FILE__))
set :views, "#{dir}/views"
set :public_folder, "#{dir}/public"
set :static, true
set :lock, true
before { joe.restore }
get '/ping' do
if joe.building? || !joe.last_build || !joe.last_build.worked?
halt 412, (joe.building? || joe.last_build.nil?) ? "building" : joe.last_build.sha
end
joe.last_build.sha
end
get '/?' do
erb(:template, {}, :joe => joe)
end
post '/?' do
unless params[:rebuild]
payload = JSON.parse(params[:payload])
pushed_branch = payload["ref"].split('/').last
end
# Only build if we were given an explicit branch via `?branch=blah`
# or the payload exists and the "ref" property matches our
# specified build branch.
if params[:branch] || params[:rebuild] || pushed_branch == joe.git_branch
joe.build(params[:branch])
end
redirect request.path
end
get '/api/json' do
response = [200, {'Content-Type' => 'application/json'}]
response_json = erb(:json, {}, :joe => joe)
if params[:jsonp]
response << params[:jsonp] + '(' + response_json + ')'
else
response << response_json
end
response
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
# thanks integrity!
def ansi_color_codes(string)
string.gsub("\e[0m", '</span>').
gsub(/\e\[(\d+)m/, "<span class=\"color\\1\">")
end
def pretty_time(time)
time.strftime("%Y-%m-%d %H:%M")
end
def cijoe_root
root = request.path
root = "" if root == "/"
root
end
end
def initialize(*args)
super
check_project
@joe = CIJoe.new(options.project_path)
end
def self.start(host, port, project_path)
set :project_path, project_path
CIJoe::Server.run! :host => host, :port => port
end
def self.rack_start(project_path)
set :project_path, project_path
self.new
end
def self.project_path=(project_path)
user, pass = Config.cijoe(project_path).user.to_s, Config.cijoe(project_path).pass.to_s
if user != '' && pass != ''
use Rack::Auth::Basic do |username, password|
[ username, password ] == [ user, pass ]
end
puts "Using HTTP basic auth"
end
set :project_path, Proc.new{project_path}, true
end
def check_project
if options.project_path.nil? || !File.exists?(File.expand_path(options.project_path))
puts "Whoops! I need the path to a Git repo."
puts " $ git clone git@github.com:username/project.git project"
abort " $ cijoe project"
end
end
end
end