-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
82 lines (59 loc) · 1.43 KB
/
web.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
require 'dotenv/load'
require "sinatra"
require "./models"
require "./client"
require "omniauth-github"
require "octicons"
if ENV["BUGSNAG_API_KEY"]
require "bugsnag"
Bugsnag.configure do |config|
config.api_key = ENV["BUGSNAG_API_KEY"]
end
use Bugsnag::Rack
enable :raise_errors
end
use Rack::Session::Cookie, secret: ENV["SECRET"]
use OmniAuth::Strategies::GitHub, ENV["CLIENT_ID"], ENV["CLIENT_SECRET"]
get '/' do
if authenticated?
erb :index
else
erb :login
end
end
post '/alerts' do
if authenticated?
alert = Alert.create(
user_id: @current_user.id,
owner: params["owner"],
repo: params["repo"],
branch: params["branch"],
path: params["path"],
)
sha = Client.head_sha(alert.owner, alert.repo, alert.branch)
alert.update(last_sha: sha)
redirect "/"
end
end
# Provided by omniauth gem
# get '/auth/github'
get '/auth/github/callback' do
auth_hash = request.env['omniauth.auth']
github_id = auth_hash['uid']
github_login = auth_hash['info']['nickname']
email = auth_hash['info']['email']
user = User.find_or_create(github_id: github_id)
user.update(github_login: github_login, email: email)
session[:user_id] = user.id
redirect "/"
end
def authenticate
@current_user ||= User.find(id: session[:user_id])
end
def authenticated?
authenticate
end
# View helpers
def icon(name, size)
Octicons::Octicon.new(name, width: size).to_svg
end