@@ -10,12 +10,13 @@ const Koa = require('koa')
1010const bodyParser = require ( 'koa-bodyparser' )
1111const requireDir = require ( 'require-dir' )
1212const { verifySignature } = require ( './utils' )
13- const issueActions = requireDir ( './modules/issues' )
14- const pullRequestActions = requireDir ( './modules/pull_request' )
15- const releasesActions = requireDir ( './modules/releases' )
1613const app = new Koa ( )
1714const githubEvent = new EventEmitter ( )
1815const { appLog, accessLog } = require ( './logger' )
16+ const pkg = require ( '../package.json' )
17+
18+ pkg . config = pkg . config || { }
19+ pkg . config [ 'github-bot' ] = pkg . config [ 'github-bot' ] || { }
1920
2021app . use ( bodyParser ( ) )
2122
@@ -31,7 +32,7 @@ app.use(ctx => {
3132
3233 accessLog . info ( `receive event: ${ eventName } ` )
3334
34- githubEvent . emit ( eventName , {
35+ githubEvent . emit ( ` ${ payload . repository . full_name } @ ${ eventName } ` , {
3536 repo : payload . repository . name ,
3637 payload
3738 } )
@@ -42,7 +43,47 @@ app.use(ctx => {
4243 }
4344} )
4445
45- const actions = Object . assign ( { } , issueActions , pullRequestActions , releasesActions )
46+ const events = { }
47+ const actions = Object . assign (
48+ { } ,
49+ requireDir ( './modules/issues' ) ,
50+ requireDir ( './modules/pullRequest' ) ,
51+ requireDir ( './modules/releases' )
52+ )
53+ Object . keys ( actions ) . forEach ( key => {
54+ const name = actions [ key ] . name
55+ if ( events [ name ] ) {
56+ appLog . error ( `${ name } is existed` )
57+ return
58+ }
59+ events [ name ] = actions [ key ] . register
60+ } )
61+ Object . keys ( pkg . config [ 'github-bot' ] ) . forEach ( repo => {
62+ Object . keys ( pkg . config [ 'github-bot' ] [ repo ] ) . forEach ( type => {
63+ Object . keys ( pkg . config [ 'github-bot' ] [ repo ] [ type ] ) . forEach ( name => {
64+ const config = pkg . config [ 'github-bot' ] [ repo ] [ type ] [ name ]
65+ const register = events [ `${ type } /${ name } ` ]
66+ if ( config . enabled === true && register ) {
67+ register ( ( eventName , callback ) => {
68+ githubEvent . on ( `${ repo } @${ eventName } @source` , data => {
69+ callback ( data , {
70+ config : pkg . config [ 'github-bot' ] [ repo ] ,
71+ scope : config . data || { }
72+ } )
73+ } )
74+ githubEvent . on ( `${ repo } @${ eventName } ` , data => {
75+ githubEvent . emit ( `${ repo } @${ eventName } @source` , data )
76+ } )
77+ } )
78+ } else if ( config . enabled !== true ) {
79+ appLog . info ( `pkg.config.github-bot.${ repo } .${ type } .${ name } is not enabled.` )
80+ } else {
81+ appLog . info ( `pkg.config.github-bot.${ repo } .${ type } .${ name } is config error.` )
82+ }
83+ } )
84+ } )
85+ } )
86+
4687Object . keys ( actions ) . forEach ( ( key ) => {
4788 actions [ key ] ( githubEvent . on . bind ( githubEvent ) )
4889 appLog . info ( `bind ${ key } success!` )
0 commit comments