-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab-webhook-handler.js
More file actions
44 lines (41 loc) · 1.34 KB
/
gitlab-webhook-handler.js
File metadata and controls
44 lines (41 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
36
37
38
39
40
41
42
43
44
class GitlabWebHookHandler {
/**
*
* @param {express object} app
*/
constructor(app) {
this.app = app
this.lock = {}
}
/**
*
* @param {route} route
* @param {The function that needs to be executed after the webhook responds} func
* @param {The token set in your gitlab} gitlabToken
* @param {Locking means that the next webhook request will not be responded to before the current request is completed to avoid conflicts.} needLocking
*/
on(route, func,gitlabToken,needLocking = true) {
if(needLocking) this.lock[route] = false
let that = this
this.app.post(route, async function(req, res) {
console.log(route," has been called")
let headers = req.headers
let isLocking
if(needLocking) isLocking = that.lock[route]
else isLocking = false
if (!isLocking && headers['x-gitlab-token'] === gitlabToken) {
try{
isLocking = true
await func()
}catch(err){
console.log("func err",err)
}finally{
isLocking = false
}
}else{
console.log("is Locked")
}
})
}
}
module.exports = {GitlabWebHookHandler}