Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Fork form github-webhook-handler, add support for gitee , gitlab, gitea, gogs. [
- [gitlab](https://gitlab.com/)
- [gitea](https://gitea.io/)
- [gogs](https://gogs.io/)
- [codeup](https://www.aliyun.com/product/yunxiao/codeup)

Git 服务器的仓库都提供了 Webhooks 功能。每当代码仓库中有事件发生时,比如 `push` 代码,提 `issue`,提交 `pull request`,都可以往你配置的 Webhook 地址发送一个带有操作和仓库详细信息的请求。根据请求的信息,我们可以运行特定操作,自动更新代码等。**[Github Webhooks 文档](https://developer.github.com/webhooks/)**

Expand Down
49 changes: 30 additions & 19 deletions git-webhook-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,34 @@ function create (initOptions) {
return Buffer.from(expected).equals(Buffer.from(signature))
}

function verifyCodeup (signature) {
return signature === options.secret
}
function handler (req, res, callback) {
let events

options = findHandler(req.url, initOptions)

if (typeof options.events === 'string' && options.events !== '*') {
events = [options.events]
} else if (Array.isArray(options.events) && options.events.indexOf('*') === -1) {
events = options.events
}

if (req.url.split('?').shift() !== options.path || req.method !== 'POST') {
return callback()
}

function hasError (msg) {
res.writeHead(400, { 'content-type': 'application/json' })
res.end(JSON.stringify({ error: msg }))

const err = new Error(msg)

handler.emit('error', err, req)
callback(err)
}

// get platform
const ua = req.headers['user-agent']
const keyMap = {
Expand Down Expand Up @@ -143,48 +146,53 @@ function create (initOptions) {
keyMap.event = 'x-gogs-event'
keyMap.id = 'x-gogs-delivery'
keyMap.verify = verifyGiteaGogs
}else if (req.headers['x-codeup-token']){
keyMap.sig = 'x-codeup-token'
keyMap.event = 'x-codeup-event'
keyMap.id = 'x-codeup-delivery'
keyMap.verify = verifyCodeup
}

const sig = req.headers[keyMap.sig]
const event = req.headers[keyMap.event]
const id = req.headers[keyMap.id]

if (!sig) {
return hasError(`No ${keyMap.sig} found on request`)
}

if (!event) {
return hasError(`No ${keyMap.event} found on request`)
}

if (!id) {
return hasError(`No ${keyMap.id} found on request`)
}

if (events && events.indexOf(event) === -1) {
return hasError(`No ${keyMap.event} found on request`)
}

req.pipe(bl((err, data) => {
if (err) {
return hasError(err.message)
}

let obj

try {
obj = JSON.parse(data.toString())
} catch (e) {
return hasError(e)
}

if (!keyMap.verify(sig, data, obj)) {
return hasError(`${keyMap.sig} does not match blob signature`)
}

res.writeHead(200, { 'content-type': 'application/json' })
res.end('{"ok":true}')

const emitData = {
event: event,
id: id,
Expand All @@ -194,7 +202,7 @@ function create (initOptions) {
url: req.url,
path: options.path
}

// set common event
function commonEvent (event) {
if (event === 'Push Hook') {
Expand All @@ -203,9 +211,12 @@ function create (initOptions) {
if (event === 'Issue Hook') {
return 'issues'
}
if (event === 'Merge Request Hook'){
return 'merge'
}
return event
}

handler.emit(commonEvent(event), emitData)
handler.emit('*', emitData)
}))
Expand Down