-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkoa-app.js
More file actions
35 lines (32 loc) · 1.34 KB
/
Copy pathkoa-app.js
File metadata and controls
35 lines (32 loc) · 1.34 KB
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
const Koa = require('koa')
const app = new Koa()
const bodyParser = require('koa-bodyparser')
const { send, deploy9000 } = require('./sendDingMessage')
const { trigger } = require('./triggerJenkins')
var crypto = require('crypto')
var secret = process.env.Token
var pwd = process.env.Pwd
var algorithm = 'sha256'
const cors = require('@koa/cors');
app.use(cors());
app.use(bodyParser({ enableTypes: ['text', 'json'] }))
app.use(async (ctx, next) => {
hmac = crypto.createHmac(algorithm, secret)
hmac.write(JSON.stringify(ctx.request.body)) // write in to the stream
hmac.end() // can't read from the stream until you call end()
hash = hmac.read().toString('hex') // read out hmac digest
if (ctx.request.path === '/rebot/send' && ctx.request.header['x-hub-signature-256'] === 'sha256=' + hash) {
const gitHubEvent = ctx.request.header['x-github-event']
if (gitHubEvent ==='workflow_run' && ctx.request.body.workflow_run && ['requested', 'completed'].includes(ctx.request.body.action)) {
await send(ctx.request.body)
}else if(gitHubEvent ==='push' && ctx.request.body.ref==="refs/heads/pre"){
await deploy9000(pwd)
}
ctx.body = 'success'
} else if (ctx.request.path === '/trigger' && ctx.request.querystring.endsWith('.zip')) {
const query = ctx.request.query
trigger(query.target, query.zipstr, pwd)
ctx.body = 'success'
}
})
app.listen(3002)