This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
111 lines (90 loc) · 3.32 KB
/
main.ts
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
import * as Restify from "restify"
import * as Crypto from "crypto"
import * as Request from "request"
import { ErrorHandler } from "./errors"
import { Space, SpaceIssue } from "./space"
import { GitHubWebhookIssuesEvent, GitHubWebhookPayload, toIssueTemplate } from "./github"
import * as fs from "fs"
import * as DotEnv from "dotenv"
import Log, { LogLevel } from "./log"
DotEnv.config()
let space: Space
const WEBHOOK_SECRET = process.env['GITHUB_SECRET'] || ""
const PORT = process.env['PORT'] || 2652
const SPACE_URL = process.env['SPACE_URL'] || ""
const SPACE_PROJECT = process.env['SPACE_PROJECT'] || ""
const SPACE_TOKEN = process.env['SPACE_TOKEN'] || ""
const SPACE_DEFAULT_STATUS = process.env['SPACE_DEFAULT_STATUS'] || ""
let SPACE_ISSUE_TEMPLATE = "{{body}}\r"
if (fs.existsSync('templates/issue.txt')) {
SPACE_ISSUE_TEMPLATE = fs.readFileSync('templates/issue.txt', 'utf8')
}
const server = Restify.createServer();
new ErrorHandler(server);
server.use(Restify.plugins.bodyParser())
server.post('/', async (req: Restify.Request, res: Restify.Response, next: Restify.Next) => {
let body: GitHubWebhookPayload = req.body
let signature = req.header('X-Hub-Signature')
let event = req.header('X-GitHub-Event')
if (process.env['NODE_ENV'] === "production") {
let hmac = Crypto.createHmac("sha1", WEBHOOK_SECRET)
let calculatedSignature = "sha1=" + hmac.update(JSON.stringify(req.body)).digest("hex");
if (calculatedSignature !== signature) {
return res.send(401)
}
}
switch (event) {
case 'issues':
await handleIssuesEvent(res, body as GitHubWebhookIssuesEvent)
return
default:
res.send(200)
return
}
});
async function handleIssuesEvent(res: Restify.Response, issueEvent: GitHubWebhookIssuesEvent) {
if (issueEvent === undefined) {
Log.instance.error("Issue event is undefined")
res.send(400)
return
}
if (issueEvent.action !== "opened") {
Log.instance.debug("Ignoring issue event with action: " + issueEvent.action)
res.send(200)
return
}
let title = issueEvent.issue.title
let description = toIssueTemplate(SPACE_ISSUE_TEMPLATE, issueEvent)
let issue = new SpaceIssue(title, description)
try{
await space.createIssue(issue)
res.send(200)
} catch (e) {
Log.instance.error("Error creating issue: " + e)
res.send(500)
}
}
// Main function
async function main() {
space = new Space(SPACE_URL, SPACE_PROJECT, SPACE_TOKEN, SPACE_DEFAULT_STATUS)
await space.init()
Log.instance.debug("Space issue statuses: " + space.getIssueStatusList().map(
status => status.name + " (" + status.id + " - " + status.color + ") " + (status.id == SPACE_DEFAULT_STATUS ? "default" : "")
).join(","))
// Start the server
server.listen(PORT, function () {
console.log('%s listening at %s', server.name, server.url);
});
}
if (process.env['LOG_LEVEL']) {
Log.instance.setLogLevel(parseInt(process.env['LOG_LEVEL']) as LogLevel)
} else if (process.env['NODE_ENV'] === "production") {
Log.instance.setLogLevel(LogLevel.Info)
} else {
Log.instance.setLogLevel(LogLevel.Debug)
}
Log.instance.info("Starting server...")
main().catch(err => {
console.error(err)
process.exit(1)
})