This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
162 lines (146 loc) · 5.32 KB
/
server.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"use strict"
var format = require('python-format')
var request = require('request')
var port = +process.env.npm_package_config_webhookport
if (!port) {
console.error("Start the bot using 'npm start'.")
return
}
var secret = process.env.npm_package_config_secret
if (!secret) {
console.error("Secret not defined, please use 'npm config set psdevbot:secret value'.")
return
}
var Showdown = require('./showdown')
var parameters = {}
Object.keys(Showdown.keys).forEach(function (key) {
parameters[key] = process.env[`npm_package_config_${key}`]
})
var client = new Showdown(parameters)
client.connect()
var allowedAuthLevels = new Set("~#*&@%")
var github = require('githubhook')({
port: port,
secret: secret,
logger: console
})
function shorten(url, callback) {
function shortenCallback(error, response, body) {
var shortenedUrl = url
if (!error && response.headers.location) {
shortenedUrl = response.headers.location
}
callback(shortenedUrl)
}
request.post('https://git.io', {form: {url: url}}, shortenCallback)
}
function getRepoName(repo) {
switch (repo) {
case 'Pokemon-Showdown':
return 'server'
case 'Pokemon-Showdown-Client':
return 'client'
case 'Pokemon-Showdown-Dex':
return 'dex'
default:
return repo
}
}
var escape = require('escape-html')
github.on('push', function push(repo, ref, result) {
var url = result.compare
var branch = /[^/]+$/.exec(ref)[0]
shorten(url, function pushShortened(url) {
var messages = []
var message = result.commits.length === 1 ?
"[<font color='FF00FF'>{}</font>] <font color='909090'>{}</font> {} <b>{}</b> new commit to <font color='800080'>{}</font>: <a href=\"{5}\">{5}</a>" :
"[<font color='FF00FF'>{}</font>] <font color='909090'>{}</font> {} <b>{}</b> new commits to <font color='800080'>{}</font>: <a href=\"{5}\">{5}</a>"
messages.push(format(
message,
escape(getRepoName(repo)),
escape(result.pusher.name),
result.created ? 'pushed <font color="red">in new branch</font>' : result.forced ? '<font color="red">force-pushed</font>' : 'pushed',
escape(result.commits.length),
escape(branch),
escape(url)
))
result.commits.forEach(function (commit) {
var commitMessage = commit.message
var shortCommit = /.+/.exec(commitMessage)[0]
if (commitMessage !== shortCommit) {
shortCommit += '…'
}
messages.push(format(
"<font color='FF00FF'>{}</font>/<font color='800080'>{}</font> <a href=\"{}\"><font color='606060'>{}</font></a> <font color='909090'>{}</font>: {}",
escape(getRepoName(repo)),
escape(branch),
escape(commit.url),
escape(commit.id.substring(0, 6)),
escape(commit.author.name),
escape(shortCommit)
))
})
client.report('/addhtmlbox ' + messages.join("<br>"))
})
})
var updates = {}
github.on('pull_request', function pullRequest(repo, ref, result) {
if (gitBans.has(result.sender.login.toLowerCase()) || gitBans.has(result.pull_request.user.login.toLowerCase())) {
return
}
var COOLDOWN = 10 * 60 * 1000
var requestNumber = result.pull_request.number
var url = result.pull_request.html_url
var action = result.action
if (action === 'synchronize') {
action = 'updated'
}
if (action === 'review_requested') {
action = 'requested a review for'
}
// Nobody cares about labels
if (action === 'labeled' || action === 'unlabeled') {
return
}
var now = +new Date
if (updates[requestNumber] && updates[requestNumber] + COOLDOWN > now) {
return
}
updates[requestNumber] = now
shorten(url, function pullRequestShortened(url) {
client.report(format(
"/addhtmlbox [<font color='FF00FF'>{}</font>] <font color='909090'>{}</font> {} pull request <a href=\"{}\">#{}</a>: {}",
escape(getRepoName(repo)),
escape(result.sender.login),
escape(action),
escape(url),
escape(requestNumber),
escape(result.pull_request.title)
))
})
})
var gitBans = new Set
client.on('message', function (user, message) {
if (allowedAuthLevels.has(user.charAt(0)) && message.charAt(0) === '.') {
var parts = message.substring(1).split(' ')
var command = parts[0]
var argument = parts.slice(1).join(" ").toLowerCase()
if (command === "gitban") {
if (gitBans.has(argument)) {
client.report(`/modnote '${argument}' is already banned from being reported`)
return
}
gitBans.add(argument)
client.report(`/modnote '${argument}' was banned from being reported by this bot`)
}
else if (command === "gitunban") {
if (!gitBans.has(argument)) {
client.report(`/modnote '${argument}' is already allowed to be reported`)
return
}
gitBans.delete(argument)
client.report(`/modnote '${argument}' was unbanned from being reported by this bot`)
}
}
})
github.listen()