foca / integrity

Don't mind this, go to http://github.com/integrity/integrity

This URL has Read+Write access

foca (author)
Thu Nov 20 09:44:26 -0800 2008
commit  15af31307e22df99d83339bef76f816b45b5d064
tree    caa708d0e1789e4ed08b3aeb2712c39303e7f14b
parent  a7f3bdc678492d935888f3349ddb2c26dcc559ed
integrity / app.rb
100644 261 lines (209 sloc) 6.648 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require File.dirname(__FILE__) + '/lib/integrity'
require 'sinatra'
require 'diddies'
require 'hacks'
 
set :root, Integrity.root
set :public, Integrity.root / "public"
set :views, Integrity.root / "views"
 
enable :sessions
 
include Integrity
 
configure :development do
  config = Integrity.root / "config" / "config.yml"
  Integrity.config = config if File.exists? config
end
 
configure do
  Integrity.new
end
 
not_found do
  status 404
  show :not_found, :title => "lost, are we?"
end
 
error do
  @error = request.env['sinatra.error']
  status 500
  show :error, :title => "something has gone terribly wrong"
end
 
before do
  # The browser only sends http auth data for requests that are explicitly
  # required to do so. This way we get the real values of +#logged_in?+ and
  # +#current_user+
  login_required if session[:user]
end
 
get "/" do
  @projects = Project.all(authorized? ? {} : { :public => true })
  show :home, :title => "projects"
end
 
get "/login" do
  login_required
  session[:user] = current_user
  redirect "/"
end
 
get "/new" do
  login_required
 
  @project = Project.new
  show :new, :title => ["projects", "new project"]
end
 
post "/" do
  login_required
  
  @project = Project.new(params[:project_data])
  if @project.save
    @project.enable_notifiers(params["enabled_notifiers[]"], params["notifiers"])
    redirect project_url(@project)
  else
    show :new, :title => ["projects", "new project"]
  end
end
 
get "/:project" do
  login_required unless current_project.public?
  show :project, :title => ["projects", current_project.permalink]
end
 
put "/:project" do
  login_required
  
  if current_project.update_attributes(params[:project_data])
    current_project.enable_notifiers(params["enabled_notifiers[]"], params["notifiers"])
    redirect project_url(current_project)
  else
    show :new, :title => ["projects", current_project.permalink, "edit"]
  end
end
 
delete "/:project" do
  login_required
 
  current_project.destroy
  redirect "/"
end
 
get "/:project/edit" do
  login_required
 
  show :new, :title => ["projects", current_project.permalink, "edit"]
end
 
post "/:project/push" do
  login_required
  
  content_type 'text/plain'
 
  begin
    payload = JSON.parse(params[:payload] || "")
    payload['commits'].reverse.each do |commit|
      current_project.build(commit['id']) if payload['ref'] =~ /#{current_project.branch}/
    end
    'Thanks, build started.'
  rescue JSON::ParserError => exception
    invalid_payload!(exception.to_s)
  end
end
 
post "/:project/builds" do
  login_required
 
  current_project.build
  redirect project_url(@project)
end
 
get '/:project/builds/:build' do
  login_required unless current_project.public?
  show :build, :title => ["projects", current_project.permalink, current_build.short_commit_identifier]
end
 
get "/integrity.css" do
  header "Content-Type" => "text/css; charset=utf-8"
  sass :integrity
end
 
helpers do
  include Rack::Utils
  include Sinatra::Authorization
  alias_method :h, :escape_html
 
  def authorization_realm
    "Integrity"
  end
 
  def authorized?
    return true unless Integrity.config[:use_basic_auth]
    !!request.env['REMOTE_USER']
  end
 
  def authorize(user, password)
    if Integrity.config[:hash_admin_password]
      password = Digest::SHA1.hexdigest(password)
    end
 
    !Integrity.config[:use_basic_auth] ||
    (Integrity.config[:admin_username] == user &&
      Integrity.config[:admin_password] == password)
  end
 
  def unauthorized!(realm=authorization_realm)
    header 'WWW-Authenticate' => %(Basic realm="#{realm}")
    throw :halt, [401, show(:unauthorized, :title => "incorrect credentials")]
  end
 
  def invalid_payload!(msg=nil)
    throw :halt, [422, msg || 'No payload given']
  end
 
  def current_project
    @project ||= Project.first(:permalink => params[:project]) or raise Sinatra::NotFound
  end
 
  def current_build
    @build ||= current_project.builds.first(:commit_identifier => params[:build]) or raise Sinatra::NotFound
  end
 
  def show(view, options={})
    @title = breadcrumbs(*options[:title])
    haml view
  end
 
  def pages
    @pages ||= [["projects", "/"], ["new project", "/new"]]
  end
 
  def breadcrumbs(*crumbs)
    crumbs[0..-2].map do |crumb|
      if page_data = pages.detect {|c| c.first == crumb }
        %Q(<a href="#{page_data.last}">#{page_data.first}</a>)
      elsif @project && @project.permalink == crumb
        %Q(<a href="#{project_url(@project)}">#{@project.permalink}</a>)
      end
    end + [crumbs.last]
  end
 
  def cycle(*values)
    @cycles ||= {}
    @cycles[values] ||= -1 # first value returned is 0
    next_value = @cycles[values] = (@cycles[values] + 1) % values.size
    values[next_value]
  end
 
  def project_url(project, *path)
    "/" << [project.permalink, *path].join("/")
  end
 
  def push_url_for(project)
    Addressable::URI.parse(Integrity.config[:base_uri]).join("#{project_url(project)}/push").to_s
  end
 
  def build_url(build)
    "/#{build.project.permalink}/builds/#{build.commit_identifier}"
  end
 
  def filter_attributes_of(model)
    valid = model.properties.collect {|p| p.name.to_s }
    Hash[*params.dup.select {|k,_| valid.include?(k) }.flatten]
  end
 
  def errors_on(object, field)
    return "" unless errors = object.errors.on(field)
    errors.map {|e| e.gsub(/#{field} /i, "") }.join(", ")
  end
 
  def error_class(object, field)
    object.errors.on(field).nil? ? "" : "with_errors"
  end
  
  def checkbox(name, condition, extras={})
    attrs = { :name => name, :type => "checkbox" }.merge(condition ? { :checked => "checked" } : {})
    attrs.merge(extras)
  end
 
  def bash_color_codes(string)
    string.gsub("\e[0m", '</span>').
      gsub("\e[31m", '<span class="color31">').
      gsub("\e[32m", '<span class="color32">').
      gsub("\e[33m", '<span class="color33">').
      gsub("\e[34m", '<span class="color34">').
      gsub("\e[35m", '<span class="color35">').
      gsub("\e[36m", '<span class="color36">').
      gsub("\e[37m", '<span class="color37">')
  end
 
  def pretty_date(date_time)
    today = Date.today
    if date_time.day == today.day && date_time.month == today.month && date_time.year == today.year
      "today"
    elsif date_time.day == today.day - 1 && date_time.month == today.month && date_time.year == today.year
      "yesterday"
    else
      date_time.strftime("on %b %d%o")
    end
  end
  
  def notifier_form(notifier)
    haml(notifier.to_haml, :layout => :notifier, :locals => {
      :config => current_project.config_for(notifier),
      :notifier => "#{notifier.to_s.split(/::/).last}",
      :enabled => current_project.notifies?(notifier)
    })
  end
end