This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.rb
122 lines (106 loc) · 3.26 KB
/
app.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
115
116
117
118
119
120
121
122
#
# Copyright 2015, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'sinatra'
require 'sinatra_auth_github'
require 'tilt/erb'
require 'librato-rack'
require_relative './model'
require_relative './warden_travis/strategy'
module Nightlies
class Application < Sinatra::Application
configure do
# Let's get this party started!
enable :sessions
set :session_secret, ENV['GITHUB_VERIFIER_SECRET']
set :public_folder, 'public'
use Rack::Protection::AuthenticityToken
# Load librato
use Librato::Rack if ENV['LIBRATO_TOKEN']
# Load the GithHub authentication stuffs.
set :github_options, {scopes: 'read:org user:email repo:status write:repo_hook repo_deployment'}
register Sinatra::Auth::Github
# Reconfigure Warden to use our strategy instead.
use Class.new {
def initialize(app)
@app = app
end
def call(env)
env['warden'].config.default_strategies :travis
@app.call(env)
end
}
end
helpers do
def travis_token
authenticated? && github_user.attribs['travis_token']
end
def travis_api
raise "No travis token" unless travis_token
Travis::Client.new(access_token: travis_token, agent_info: 'nightli.es')
end
def no_cache!
cache_control :no_cache, :no_store, :must_revalidate
headers['Expires'] = '0'
headers['Pragma'] = 'no-cache'
end
end
before do
headers 'Content-Type' => 'text/html; charset=utf8'
end
get '/' do
no_cache!
if !authenticated?
erb :landing
else
@repos = travis_api.get_raw('/hooks')['hooks'].inject([]) do |memo, data|
if data['admin'] && data['active']
row = Nightlies::Model.by_id(data['id'])
memo << data.merge(
'last_nightly' => row && row[:last_nightly],
'enabled' => row && !!row[:travis_token],
)
end
memo
end
@extra_js = 'dashboard.js'
erb :dashboard
end
end
get '/login' do
no_cache!
authenticate!
redirect '/'
end
get '/logout' do
no_cache!
logout!
redirect '/'
end
post '/enable/:id' do
logger.warn "Enabling nightly builds for #{params['slug']} via #{github_user.login}"
Nightlies::Model.enable!(github_user, params['id'].to_i, params['slug'])
content_type :json
{success: true}.to_json
end
post '/disable/:id' do
logger.warn "Disabling nightly builds for #{params['slug']} via #{github_user.login}"
Nightlies::Model.disable!(github_user, params['id'].to_i)
content_type :json
{success: true}.to_json
end
run! if app_file == $0
end
end