-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
41 lines (35 loc) · 1.44 KB
/
index.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
import express = require('express');
import fetch from 'node-fetch';
import * as msg from './generate-message'
const app = express();
app.use(express.json())
app.post('/', async (req, res)=>{
try {
if (!req.query.url) throw `Please specify a URL to send to`;
const url: string = `${req.query.url as string}&token=${req.query.token}`
const tstping = await fetch(url)
if (tstping.status!==401) throw `Invalid URL: Expected test ping response of 401, got ${tstping.status}`;
const event = req.header("X-GitHub-Event");
let sendMessage;
switch (event) {
case "push":
sendMessage = msg.generatePushMessage(req.body)
break;
case "pull_request":
sendMessage = msg.generatePRMessage(req.body)
break;
case "issues":
sendMessage = msg.generateIssueMessage(req.body)
break;
default:
sendMessage = msg.generateDefaultMessage(req.body, event)
break;
}
const whres = await sendMessage(url);
res.status(whres.status).send(`URL: ${whres.url} \n Repsonse: ${whres.status}: ${whres.statusText} \n Response Body: ${whres.body.read()}`)
} catch (error) {
res.status(400).send(`Bad Request: ${error}`)
}
})
app.get('/', (_, res)=>res.redirect("https://github.com/one23four56/github-to-google-chat"))
app.listen(8000)