{
"version": 3,
"sources": ["../../../server/chat-plugins/github.ts"],
"sourcesContent": ["/**\n * Chat plugin to view GitHub events in a chatroom.\n * By Mia, with design / html from xfix's original bot, https://github.com/xfix/GitHub-Bot-Legacy/\n * @author mia-pi-git\n */\n\nimport { FS, Utils } from '../../lib';\n\nconst STAFF_REPOS = Config.staffrepos || [\n\t'pokemon-showdown', 'pokemon-showdown-client', 'Pokemon-Showdown-Dex', 'pokemon-showdown-loginserver',\n];\nconst COOLDOWN = 10 * 60 * 1000;\n\nexport const gitData: GitData = JSON.parse(FS(\"config/chat-plugins/github.json\").readIfExistsSync() || \"{}\");\n\ninterface GitHookHandler {\n\ton(event: 'pull_request', callback: (repo: string, ref: string | undefined, result: PullRequest) => void): void;\n\ton(event: 'push', callback: (repo: string, ref: string, result: Push) => void): void;\n\ton(event: string, callback: (repo: string, ref: string | undefined, result: any) => void): void;\n\tlisten(): void;\n\tserver: import('http').Server;\n}\n\ninterface Push {\n\tcommits: Commit[];\n\tsender: { login: string };\n\tcompare: string;\n}\n\ninterface PullRequest {\n\taction: string;\n\tnumber: number;\n\tpull_request: {\n\t\turl: string,\n\t\thtml_url: string,\n\t\ttitle: string,\n\t\tuser: {\n\t\t\tlogin: string,\n\t\t\thtml_url: string,\n\t\t},\n\t\tmerge_commit_sha: string,\n\t};\n\tsender: { login: string };\n}\n\ninterface Commit {\n\tid: string;\n\tmessage: string;\n\tauthor: { name: string, avatar_url: string };\n\turl: string;\n}\n\ninterface GitData {\n\tusernames?: { [username: string]: string };\n\tbans?: { [username: string]: string };\n}\n\nexport const GitHub = new class {\n\treadonly hook: GitHookHandler | null = null;\n\tupdates: { [k: string]: number } = Object.create(null);\n\tconstructor() {\n\t\t// config.github: https://github.com/nlf/node-github-hook#readme\n\t\tif (!Config.github) return;\n\n\t\ttry {\n\t\t\tthis.hook = require('githubhook')({\n\t\t\t\tlogger: {\n\t\t\t\t\tlog: (line: string) => Monitor.debug(line),\n\t\t\t\t\terror: (line: string) => Monitor.notice(line),\n\t\t\t\t},\n\t\t\t\t...Config.github,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tMonitor.crashlog(err, \"GitHub hook\");\n\t\t}\n\t\tthis.listen();\n\t}\n\tlisten() {\n\t\tif (!this.hook) return;\n\t\tthis.hook.listen();\n\t\tthis.hook.on('push', (repo, ref, result) => this.handlePush(repo, ref, result));\n\t\tthis.hook.on('pull_request', (repo, ref, result) => this.handlePull(repo, ref, result));\n\t}\n\tprivate getRepoName(repo: string) {\n\t\tswitch (repo) {\n\t\tcase 'pokemon-showdown':\n\t\t\treturn 'server';\n\t\tcase 'pokemon-showdown-client':\n\t\t\treturn 'client';\n\t\tcase 'Pokemon-Showdown-Dex':\n\t\t\treturn 'dex';\n\t\tdefault:\n\t\t\treturn repo.toLowerCase();\n\t\t}\n\t}\n\thandlePush(repo: string, ref: string, result: Push) {\n\t\tconst branch = /[^/]+$/.exec(ref)?.[0] || \"\";\n\t\tif (branch !== 'master') return;\n\t\tconst messages: { [k: string]: string[] } = {\n\t\t\tstaff: [],\n\t\t\tdevelopment: [],\n\t\t};\n\t\tfor (const commit of result.commits) {\n\t\t\tconst { message, url } = commit;\n\t\t\tconst [shortMessage] = message.split('\\n\\n');\n\t\t\tconst username = this.getUsername(commit.author.name);\n\t\t\tconst repoName = this.getRepoName(repo);\n\t\t\tconst id = commit.id.substring(0, 6);\n\t\t\tmessages.development.push(\n\t\t\t\tUtils.html`[${repoName}] ${id} ${shortMessage} (${username})`\n\t\t\t);\n\t\t\tmessages.staff.push(Utils.html`[${repoName}] ${shortMessage} (${username})`);\n\t\t}\n\t\tfor (const k in messages) {\n\t\t\tthis.report(k as RoomID, repo, messages[k as RoomID]);\n\t\t}\n\t}\n\thandlePull(repo: string, ref: string | undefined, result: PullRequest) {\n\t\tif (this.isRateLimited(result.number)) return;\n\t\tif (this.isGitbanned(result)) return;\n\t\tconst url = result.pull_request.html_url;\n\t\tconst action = this.isValidAction(result.action);\n\t\tif (!action) return;\n\t\tconst repoName = this.getRepoName(repo);\n\t\tconst userName = this.getUsername(result.sender.login);\n\t\tconst title = result.pull_request.title;\n\t\tlet buf = Utils.html`[${repoName}] ${userName} `;\n\t\tbuf += Utils.html`${action} PR#${result.number}: ${title}`;\n\t\tthis.report('development', repo, buf);\n\t}\n\treport(roomid: RoomID, repo: string, messages: string[] | string) {\n\t\tif (!STAFF_REPOS.includes(repo) && roomid === 'staff') return;\n\t\tif (Array.isArray(messages)) messages = messages.join('
');\n\t\tRooms.get(roomid)?.add(`|html|