mirror of
https://github.com/xuexb/github-bot.git
synced 2026-01-25 14:06:59 +00:00
chore: update config file
This commit is contained in:
parent
bf92708145
commit
daf7a5a238
40
package.json
40
package.json
@ -53,30 +53,26 @@
|
||||
]
|
||||
},
|
||||
"github-bot": {
|
||||
"github-bot": {
|
||||
"issue": {
|
||||
"replyNeedDemo": {
|
||||
"enabled": true,
|
||||
"data": {
|
||||
"need demo": "您好,请填写相关 Demo 链接。",
|
||||
"need update package": "请更新版本号。"
|
||||
}
|
||||
},
|
||||
"autoAssign": {
|
||||
"enabled": true,
|
||||
"data": {
|
||||
"bug": "xuexb",
|
||||
"enhancement": "xuexb",
|
||||
"question": "xuexb"
|
||||
}
|
||||
"xuexb/github-bot": {
|
||||
"issue/replyNeedDemo": {
|
||||
"enabled": true,
|
||||
"data": {
|
||||
"need demo": "您好,请填写相关 Demo 链接。",
|
||||
"need update package": "请更新版本号。"
|
||||
}
|
||||
},
|
||||
"pullRequest": {
|
||||
"labelToReviewer": {
|
||||
"enabled": true,
|
||||
"data": {
|
||||
"bug": "xuexb"
|
||||
}
|
||||
"issue/autoAssign": {
|
||||
"enabled": true,
|
||||
"data": {
|
||||
"bug": "xuexb",
|
||||
"enhancement": "xuexb",
|
||||
"question": "xuexb"
|
||||
}
|
||||
},
|
||||
"pullRequest/autoReviewRequest": {
|
||||
"enabled": true,
|
||||
"data": {
|
||||
"bug": "xuexb"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
62
src/app.js
62
src/app.js
@ -4,19 +4,15 @@
|
||||
*/
|
||||
|
||||
require('dotenv').config()
|
||||
require('./register')
|
||||
|
||||
const EventEmitter = require('events')
|
||||
const event = require('./event')
|
||||
const Koa = require('koa')
|
||||
const bodyParser = require('koa-bodyparser')
|
||||
const requireDir = require('require-dir')
|
||||
const { verifySignature } = require('./utils')
|
||||
const app = new Koa()
|
||||
const githubEvent = new EventEmitter()
|
||||
const { appLog, accessLog } = require('./logger')
|
||||
const pkg = require('../package.json')
|
||||
|
||||
pkg.config = pkg.config || {}
|
||||
pkg.config['github-bot'] = pkg.config['github-bot'] || {}
|
||||
const port = 8000
|
||||
|
||||
app.use(bodyParser())
|
||||
|
||||
@ -32,7 +28,7 @@ app.use(ctx => {
|
||||
|
||||
accessLog.info(`receive event: ${eventName}`)
|
||||
|
||||
githubEvent.emit(`${payload.repository.full_name}@${eventName}`, {
|
||||
event.emit(`${payload.repository.full_name}@${eventName}`, {
|
||||
repo: payload.repository.name,
|
||||
payload
|
||||
})
|
||||
@ -43,55 +39,5 @@ app.use(ctx => {
|
||||
}
|
||||
})
|
||||
|
||||
const events = {
|
||||
uid: 0
|
||||
}
|
||||
const actions = Object.assign(
|
||||
{},
|
||||
requireDir('./modules/issues'),
|
||||
requireDir('./modules/pullRequest'),
|
||||
requireDir('./modules/releases')
|
||||
)
|
||||
Object.keys(actions).forEach(key => {
|
||||
const name = actions[key].name
|
||||
if (events[name]) {
|
||||
appLog.error(`${name} is existed`)
|
||||
return
|
||||
}
|
||||
events[name] = actions[key].register
|
||||
})
|
||||
Object.keys(pkg.config['github-bot']).forEach(repo => {
|
||||
Object.keys(pkg.config['github-bot'][repo]).forEach(type => {
|
||||
Object.keys(pkg.config['github-bot'][repo][type]).forEach(name => {
|
||||
const config = pkg.config['github-bot'][repo][type][name]
|
||||
const register = events[`${type}/${name}`]
|
||||
if (config.enabled === true && register) {
|
||||
register((eventName, callback) => {
|
||||
const uid = events.uid++
|
||||
githubEvent.on(`${repo}@${eventName}@source@${uid}`, data => {
|
||||
callback(data, {
|
||||
config: pkg.config['github-bot'][repo],
|
||||
scope: config.data || {}
|
||||
})
|
||||
})
|
||||
githubEvent.on(`${repo}@${eventName}`, data => {
|
||||
githubEvent.emit(`${repo}@${eventName}@source@${uid}`, data)
|
||||
})
|
||||
})
|
||||
} else if (config.enabled !== true) {
|
||||
appLog.info(`pkg.config.github-bot.${repo}.${type}.${name} is not enabled.`)
|
||||
} else {
|
||||
appLog.info(`pkg.config.github-bot.${repo}.${type}.${name} is config error.`)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Object.keys(actions).forEach((key) => {
|
||||
actions[key](githubEvent.on.bind(githubEvent))
|
||||
appLog.info(`bind ${key} success!`)
|
||||
})
|
||||
|
||||
const port = 8000
|
||||
app.listen(port)
|
||||
appLog.info('Listening on http://0.0.0.0:', port)
|
||||
|
||||
7
src/event.js
Normal file
7
src/event.js
Normal file
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* @file 事件管理
|
||||
* @author xuexb <fe.xiaowu@gmail.com>
|
||||
*/
|
||||
|
||||
const Events = require('events')
|
||||
module.exports = new Events()
|
||||
70
src/register.js
Normal file
70
src/register.js
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* @file github-bot 入口文件
|
||||
* @author xuexb <fe.xiaowu@gmail.com>
|
||||
* @description 注册流程:
|
||||
* 1. 先获取所有的功能模块源代码,并以功能名称存放起来
|
||||
* 2. 读取 package.json 中的配置信息,以仓库+功能为粒度
|
||||
* 3. 判断功能的开关是否打开,如果打开则以功能的名称去查找对应的功能源代码
|
||||
* 4. 去注册个事件,以 `项目名@事件名` 注册,并再注册一个代理事件,因为触发 `项目名@事件名` 会有很多,而代理事件的 uid 是唯一的
|
||||
* 5. 在代理事件内去触发真实的功能方法的回调
|
||||
*/
|
||||
|
||||
const event = require('./event')
|
||||
const requireDir = require('require-dir')
|
||||
const { appLog } = require('./logger')
|
||||
const pkg = require('../package.json')
|
||||
const KEY = 'github-bot'
|
||||
|
||||
pkg.config = pkg.config || {}
|
||||
pkg.config[KEY] = pkg.config[KEY] || {}
|
||||
|
||||
const events = {
|
||||
uid: 0
|
||||
}
|
||||
const actions = Object.assign(
|
||||
{},
|
||||
requireDir('./modules/issues'),
|
||||
requireDir('./modules/pullRequest'),
|
||||
requireDir('./modules/releases')
|
||||
)
|
||||
|
||||
Object.keys(actions).forEach(key => {
|
||||
const name = actions[key].name
|
||||
if (events[name]) {
|
||||
appLog.error(`${name} is existed`)
|
||||
return
|
||||
}
|
||||
events[name] = actions[key].register
|
||||
})
|
||||
|
||||
Object.keys(pkg.config[KEY]).forEach(repoName => {
|
||||
const repo = pkg.config[KEY][repoName]
|
||||
|
||||
Object.keys(repo).forEach(name => {
|
||||
const config = repo[name]
|
||||
const register = events[name]
|
||||
const isEnabled = String(config.enabled).toLowerCase() === 'true'
|
||||
|
||||
if (isEnabled && register) {
|
||||
register((eventName, callback) => {
|
||||
const uid = events.uid++
|
||||
event.on(`${repoName}@${eventName}@source@${uid}`, data => {
|
||||
callback(data, {
|
||||
config: repo,
|
||||
scope: config.data || {}
|
||||
})
|
||||
})
|
||||
event.on(`${repoName}@${eventName}`, data => {
|
||||
event.emit(`${repoName}@${eventName}@source@${uid}`, data)
|
||||
})
|
||||
})
|
||||
appLog.info(`${repoName} - ${name} is register success.`)
|
||||
} else if (isEnabled && !register) {
|
||||
appLog.info(`${repoName} - ${name} is not register.`)
|
||||
} else if (!isEnabled) {
|
||||
appLog.info(`${repoName} - ${name} is not enabled.`)
|
||||
} else {
|
||||
appLog.info(`${repoName} - ${name} is config error.`)
|
||||
}
|
||||
})
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user