@@ -4,20 +4,26 @@ import fetch from 'node-fetch';
4
4
import httpStatus from 'http-status' ;
5
5
import bodyParser from 'body-parser' ;
6
6
7
- export default function ( openshiftWebhookUrl ) { // eslint-disable-line no-unused-vars
7
+ export default function ( whiteListMiddleware , openshiftWebhookUrl , urlWhiteList ) { // eslint-disable-line no-unused-vars
8
8
const logger = createLogger ( ) ;
9
9
10
10
return new Router ( )
11
- . post ( '/:project/:buildConfig/:id' , bodyParser . json ( ) , handleHook )
12
- . post ( '/namespaces/:project/buildconfigs/:buildConfig/webhooks/:id/generic' , bodyParser . json ( ) , handleHook )
13
- . post ( '/apis/build.openshift.io/v1/namespaces/:project/buildconfigs/:buildConfig/webhooks/:id/generic' , bodyParser . json ( ) , handleHook )
11
+ . post ( '/:project/:buildConfig/:id' , whiteListMiddleware , bodyParser . json ( ) , handleHook )
12
+ . post ( '/namespaces/:project/buildconfigs/:buildConfig/webhooks/:id/generic' , whiteListMiddleware , bodyParser . json ( ) , handleHook )
13
+ . post ( '/apis/build.openshift.io/v1/namespaces/:project/buildconfigs/:buildConfig/webhooks/:id/generic' , whiteListMiddleware , bodyParser . json ( ) , handleHook )
14
+ . post ( '/url' , bodyParser . json ( ) , handleUrlHook )
14
15
. use ( handleError ) ;
15
16
16
17
function handleHook ( req , res ) {
17
18
logger . debug ( 'webhookRoute/handleHook' ) ;
18
19
const { project, buildConfig, id} = req . params ;
19
20
const data = req . body ;
20
- logger . debug ( 'data: ' , data ) ;
21
+
22
+ if ( 'repository' in data && 'branch' in data ) { // eslint-disable-line functional/no-conditional-statements
23
+ logger . debug ( 'Repository: ' , data . repository ) ;
24
+ logger . debug ( 'Branch: ' , data . branch ) ;
25
+ }
26
+
21
27
const triggerUrl = `${ openshiftWebhookUrl } /${ project } /buildconfigs/${ buildConfig } /webhooks/${ id } /generic` ;
22
28
fetch (
23
29
triggerUrl ,
@@ -32,6 +38,28 @@ export default function (openshiftWebhookUrl) { // eslint-disable-line no-unused
32
38
res . status ( httpStatus . OK ) . json ( { status : 200 } ) ;
33
39
}
34
40
41
+ function handleUrlHook ( req , res ) {
42
+ const { triggerUrl} = req . query ;
43
+
44
+ if ( ! urlWhiteList . some ( urlRegexp => new RegExp ( urlRegexp , 'u' ) . test ( triggerUrl ) ) ) {
45
+ return res . status ( httpStatus . FORBIDDEN ) . json ( { status : 403 } ) ;
46
+ }
47
+
48
+ const data = req . body ;
49
+
50
+ fetch (
51
+ triggerUrl ,
52
+ {
53
+ method : 'post' ,
54
+ headers : {
55
+ 'Content-Type' : 'application/json'
56
+ } ,
57
+ body : JSON . stringify ( data )
58
+ }
59
+ ) ;
60
+ res . status ( httpStatus . OK ) . json ( { status : 200 } ) ;
61
+ }
62
+
35
63
function handleError ( err , req , res , next ) {
36
64
logger . debug ( 'webhookRoute/handleError' ) ;
37
65
logger . error ( 'Error: ' , err ) ;
0 commit comments