This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
88 lines (74 loc) · 2.31 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
$:.unshift File.join(File.dirname(__FILE__), 'lib')
require 'sinatra'
require 'sinatra/url_for'
require 'sinatra/static_assets'
require './helpers'
require 'gh/org'
require 'gh/user'
require 'gh/client'
require 'gh/request'
require 'json'
require 'rubygems'
require 'bundler'
Bundler.require
Dotenv.load
enable :sessions
configure do
set :client_id, ENV['GH_CLIENT_ID']
set :client_secret, ENV['GH_CLIENT_SECRET']
set :github_scopes, ['user', 'read:org', 'repo']
end
# Public
get '/' do
haml :index
end
get '/callback' do
session[:access_token] = github_client.get_access_token! params[:code]
redirect '/orgs'
end
get '/orgs' do
haml :orgs
end
get '/orgs/:org' do
if access_token
org = current_user.orgs.detect{|o| o.login == params[:org]}
issues = github_client.fetch "https://api.github.com/orgs/#{org.login}/issues?filter=created&state=open"
@issues = issues.select { |issue|
issue.fetch('pull_request', {})['html_url'] && !issue['assignee']
}.map { |issue|
repo = issue['repository']['name']
collaborators_url = "https://api.github.com/repos/#{org.login}/#{repo}/collaborators"
collaborators = github_client.fetch(collaborators_url).map { |collab|
collab['login'] if collab['login'] != current_user.login
}.compact
issue.merge('merger' => collaborators.sample)
}
haml :pull_requests
else
redirect '/'
end
end
post '/orgs/:org/:repo/:issue/assign/:assignee' do
if access_token
# Post a comment
url = "https://api.github.com/repos/#{params[:org]}/#{params[:repo]}/issues/#{params[:issue]}/comments"
body = { body: "Oraclocat has spoken: @#{params[:assignee]} will merge this PR!" }.to_json
GH::Request.post(url, payload: body, headers: { content_type: :json }, access_token: access_token)
# Assign issue
url = "https://api.github.com/repos/#{params[:org]}/#{params[:repo]}/issues/#{params[:issue]}"
body = { assignee: params[:assignee] }.to_json
result = GH::Request.patch(url, payload: body, headers: { content_type: :json }, access_token: access_token)
@url = JSON.parse(result)['url']
"Assigned! <a href='#{@url}'>@#{params[:assignee]} was assigned!</a>"
else
redirect '/'
end
end
get '/logout' do
session.clear
redirect '/'
end
# Helpers
helpers do
include Oraclocat::Helpers
end