/
ghe-status.coffee
78 lines (70 loc) · 2.21 KB
/
ghe-status.coffee
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
# Description:
# Tells the status of GHE
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_GHE_URL
# HUBOT_GHE_USERNAME
# HUBOT_GHE_PASSWORD
#
# Commands:
# hubot ghe (status|info) <info> - gives some information about large repositories
#
# Authors:
# pnsk
module.exports = (robot) ->
robot.respond /ghe (status|info)? ?(.*)$/i, (msg) ->
url = process.env.HUBOT_GHE_URL
user = process.env.HUBOT_GHE_USERNAME
pass = process.env.HUBOT_GHE_PASSWORD
auth = 'Basic ' + new Buffer(user + ':' + pass).toString("base64")
unless user
msg.send "user isn't set."
unless pass
msg.send "pass isn't set."
unless auth
msg.send "auth isn't set."
info_term = msg.match[2]
if info_term is "license"
ghe_license msg,auth,url
if info_term is "largerepo"
ghe_largerepo msg,auth,url
else
msg.send "I don't know about #{info_term}"
ghe_license = (msg, auth,url) ->
msg.http("#{url}/enterprise/settings/license")
.header("Authorization", auth)
.get() (err, res, body) ->
if err
msg.send "error"
else
if res.statusCode is 200
results = JSON.parse body
msg.send "GHE has #{results.seats} seats, and #{results.seats_used} are used. License expires at #{results.expire_at}"
else
msg.send "statusCode: #{res.statusCode}"
ghe_largerepo = (msg, auth, url) ->
repo_min = 2000000
msg.http("#{url}/search/repositories?q=size:>=#{repo_min}")
.header("Authorization", auth)
.get() (err, res, body) ->
if err
msg.send "error"
else
if res.statusCode is 200
results = JSON.parse body
msg.send "more than #{repo_min}KB repos. (aww)"
if results.total_count is 0
msg.send "no repos."
else
results.items.sort compareSize
reporank = ""
for item, index in results.items
reporank = reporank + "#{index+1}: #{item.full_name} #{item.size} \n"
msg.send "#{reporank}"
else
msg.send "statusCode: #{res.statusCode}"
compareSize = (a, b) ->
b.size - a.size