forked from gratipay/access-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
116 lines (109 loc) · 3.23 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
require 'active_hash'
require 'excon'
require 'camping'
Camping.goes :Dashboard
module Dashboard::Models
class Service < ActiveYaml::Base
set_filename "services"
end
class User < ActiveYaml::Base
set_filename "users"
end
end
module Dashboard::Controllers
class Index < R '/'
def get
@heroku = Excon.new("https://:#{ENV['HEROKU_API_KEY']}@api.heroku.com", :persistent => true)
@github = Excon.new("https://#{ENV['GITHUB_TOKEN']}:[email protected]", :persistent => true)
@balanced = Excon.new("https://auth.balancedpayments.com", :headers => {"Cookie" => "session=#{ENV['BALANCED_COOKIE_SESSION_ID']}"})
@services = Service.all
@apps = []
@services.each do |service|
case service.name
when /heroku/i
service.apps.each do |app|
app[:access] = []
response = @heroku.get(:path => "/apps/#{app['name']}/collaborators")
next if response.status!=200
collaborators = JSON.parse response.body
collaborators.each do |collab|
unless User.find_by_email(collab['email'])
User.create(:email => collab['email'], :username => '*********')
end
end
collaborators.map! {|collab| User.find_by_email collab['email']}
app.merge! access: collaborators
end
when /github/i
service.apps.each do |app|
app[:access] = []
response = @github.get(:path => "/repos/#{app['name']}/teams")
next if response.status!=200
teams = JSON.parse response.body
teams.each do |team|
response = @github.get(:path => "/teams/#{team['id']}/members")
members = JSON.parse response.body
members.each do |m|
unless User.find_by_username(m['login'])
User.create(:username => m['login'])
end
app[:access] << User.find_by_username(m['login'])
end
end
end
end
end
render :index
end
end
end
module Dashboard::Views
def layout
html do
head do
title "Gittip Service Access Dashboard"
link :rel => "stylesheet",
:type => "text/css",
:href => "https://assets-gittipllc.netdna-ssl.com/-/gittip.css"
end
body do
div.hero! do
h2.top { span "Gittip Service Access Dashboard" }
end
div.page! { self << yield }
end
end
end
def index
@services.each do |service|
h1 service.name
case service.name
when /heroku/i, /github/i
# Dynamic service config
if service.apps
service.apps.each do |app|
h2 app['name']
ul do
if app[:access]==[]
li "Service inaccessible"
else
app[:access].each do |collab|
li collab.username
end
end
end
end
end
else
# Manual service config
if service.access
ul do
service.access.each do |collab|
li collab
end
end
end
end
end
end
end