@@ -3,6 +3,7 @@ import type { WebhookEventMap } from '@octokit/webhooks-types';
3
3
import { access , realpath } from 'fs/promises' ;
4
4
import { Resource } from './resource/resource' ;
5
5
import { Operation } from './operation/operation' ;
6
+ import { Task } from './operation/task' ;
6
7
import { ResourceConfig } from '../config/resource-config' ;
7
8
import { OperationConfig } from '../config/operation-config' ;
8
9
import { octokitAuth } from '../utility/octokit' ;
@@ -32,56 +33,72 @@ export class Service {
32
33
return this . operation ;
33
34
}
34
35
35
- public async initService ( resourceConfigPath : string , operationConfigPath : string , app : Probot ) : Promise < void > {
36
+ public async initService ( app : Probot , resourceConfigPath : string , operationConfigPath : string ) : Promise < void > {
36
37
app . log . info ( `Initializing Service: ${ this . name } ` ) ;
38
+ this . app = app ;
37
39
// Get octokit client so Resource object will get context
38
- const octokit = await octokitAuth ( app , Number ( process . env . INSTALLATION_ID ) ) ;
39
- const resConfigObj = new ResourceConfig ( resourceConfigPath , octokit ) ;
40
+ const octokit = await octokitAuth ( this . app , Number ( process . env . INSTALLATION_ID ) ) ;
41
+ const resConfigObj = new ResourceConfig ( octokit , resourceConfigPath ) ;
40
42
this . resource = await resConfigObj . initResource ( ) ;
41
- const opConfigObj = new OperationConfig ( operationConfigPath , app ) ;
43
+ const opConfigObj = new OperationConfig ( operationConfigPath ) ;
42
44
this . operation = await opConfigObj . initOperation ( ) ;
43
- this . app = app ;
45
+ this . _registerEvents ( ) ;
46
+ }
47
+
48
+ private async _registerTasks ( context : any , event : string , tasks : Task [ ] ) : Promise < void > {
49
+ await tasks . reduce ( async ( promise , task ) => {
50
+ await promise ; // Make sure tasks are completed in sequential orders
51
+
52
+ const callPath = await realpath ( `./bin/call/${ task . getCallName ( ) } .js` ) ;
53
+ const callFunc = task . getCallFunc ( ) ;
54
+ const callArgs = task . getCallArgs ( ) ;
55
+
56
+ console . log ( `[${ event } ]: Verify call lib: ${ callPath } ` ) ;
57
+ try {
58
+ await access ( callPath ) ;
59
+ } catch ( e ) {
60
+ console . error ( `ERROR: ${ e } ` ) ;
61
+ }
62
+
63
+ const callStack = await import ( callPath ) ;
64
+ if ( callFunc === 'default' ) {
65
+ console . log ( `[${ event } ]: Call default function: [${ callStack . default . name } ]` ) ;
66
+ await callStack . default ( this . app , context , { ...callArgs } ) ;
67
+ } else {
68
+ console . log ( callStack ) ;
69
+ const callFuncCustom = callStack [ callFunc ] ;
70
+ console . log ( `[${ event } ]: Call custom function: [${ callFuncCustom . name } ]` ) ;
71
+ if ( ! ( typeof callFuncCustom === 'function' ) ) {
72
+ throw new Error ( `[${ event } ]: ${ callFuncCustom } is not a function, please verify in ${ callPath } ` ) ;
73
+ }
74
+ await callFuncCustom ( this . app , context , { ...callArgs } ) ;
75
+ }
76
+ } , Promise . resolve ( ) ) ;
44
77
}
45
78
46
- public async registerEvents ( ) : Promise < void > {
79
+ private async _registerEvents ( ) : Promise < void > {
47
80
const events = this . operation . getEvents ( ) ;
48
81
const tasks = this . operation . getTasks ( ) ;
49
82
console . log ( `Evaluate events: [${ events } ]` ) ;
50
83
if ( ! events ) {
51
84
throw new Error ( 'No events defined in the operation!' ) ;
52
85
}
86
+ if ( events . includes ( 'all' ) && events . length > 1 ) {
87
+ throw new Error ( "Either listen to 'all' events or specific events! Not both!" ) ;
88
+ }
89
+
53
90
events . forEach ( ( event ) => {
54
91
console . log ( `Register event: "${ event } "` ) ;
55
- this . app . on ( event as keyof WebhookEventMap , async ( context ) => {
56
- await tasks . reduce ( async ( promise , task ) => {
57
- await promise ; // Make sure tasks are completed in sequential orders
58
-
59
- const callPath = await realpath ( `./bin/call/${ task . getCallName ( ) } .js` ) ;
60
- const callFunc = task . getCallFunc ( ) ;
61
- const callArgs = task . getCallArgs ( ) ;
62
-
63
- console . log ( `[${ event } ]: Verify call lib: ${ callPath } ` ) ;
64
- try {
65
- await access ( callPath ) ;
66
- } catch ( e ) {
67
- console . error ( `ERROR: ${ e } ` ) ;
68
- }
69
-
70
- const callStack = await import ( callPath ) ;
71
- if ( callFunc === 'default' ) {
72
- console . log ( `[${ event } ]: Call default function: [${ callStack . default . name } ]` ) ;
73
- await callStack . default ( this . app , context , { ...callArgs } ) ;
74
- } else {
75
- console . log ( callStack ) ;
76
- const callFuncCustom = callStack [ callFunc ] ;
77
- console . log ( `[${ event } ]: Call custom function: [${ callFuncCustom . name } ]` ) ;
78
- if ( ! ( typeof callFuncCustom === 'function' ) ) {
79
- throw new Error ( `[${ event } ]: ${ callFuncCustom } is not a function, please verify in ${ callPath } ` ) ;
80
- }
81
- await callFuncCustom ( this . app , context , { ...callArgs } ) ;
82
- }
83
- } , Promise . resolve ( ) ) ;
84
- } ) ;
92
+ if ( event === 'all' ) {
93
+ console . warn ( 'WARNING! All events will be listened based on the config!' ) ;
94
+ this . app . onAny ( async ( context ) => {
95
+ this . _registerTasks ( context , event , tasks ) ;
96
+ } ) ;
97
+ } else {
98
+ this . app . on ( event as keyof WebhookEventMap , async ( context ) => {
99
+ this . _registerTasks ( context , event , tasks ) ;
100
+ } ) ;
101
+ }
85
102
} ) ;
86
103
}
87
104
}
0 commit comments