@@ -10,12 +10,13 @@ const Koa = require('koa')
10
10
const bodyParser = require ( 'koa-bodyparser' )
11
11
const requireDir = require ( 'require-dir' )
12
12
const { verifySignature } = require ( './utils' )
13
- const issueActions = requireDir ( './modules/issues' )
14
- const pullRequestActions = requireDir ( './modules/pull_request' )
15
- const releasesActions = requireDir ( './modules/releases' )
16
13
const app = new Koa ( )
17
14
const githubEvent = new EventEmitter ( )
18
15
const { 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' ] || { }
19
20
20
21
app . use ( bodyParser ( ) )
21
22
@@ -31,7 +32,7 @@ app.use(ctx => {
31
32
32
33
accessLog . info ( `receive event: ${ eventName } ` )
33
34
34
- githubEvent . emit ( eventName , {
35
+ githubEvent . emit ( ` ${ payload . repository . full_name } @ ${ eventName } ` , {
35
36
repo : payload . repository . name ,
36
37
payload
37
38
} )
@@ -42,7 +43,47 @@ app.use(ctx => {
42
43
}
43
44
} )
44
45
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
+
46
87
Object . keys ( actions ) . forEach ( ( key ) => {
47
88
actions [ key ] ( githubEvent . on . bind ( githubEvent ) )
48
89
appLog . info ( `bind ${ key } success!` )
0 commit comments