@@ -5,33 +5,41 @@ import { GITLAB_EVENT_WEBTRIGGER, STORAGE_KEYS, STORAGE_SECRETS } from '../const
5
5
import { generateSignature } from '../utils/generate-signature-utils' ;
6
6
7
7
export const setupAndValidateWebhook = async ( groupId : number ) : Promise < number > => {
8
- const [ existingWebhook , groupToken ] = await Promise . all ( [
9
- storage . get ( `${ STORAGE_KEYS . WEBHOOK_KEY_PREFIX } ${ groupId } ` ) ,
10
- storage . getSecret ( `${ STORAGE_SECRETS . GROUP_TOKEN_KEY_PREFIX } ${ groupId } ` ) ,
11
- ] ) ;
12
-
13
- const isWebhookValid = existingWebhook && ( await getGroupWebhook ( groupId , existingWebhook , groupToken ) ) !== null ;
14
-
15
- if ( isWebhookValid ) {
16
- return existingWebhook ;
8
+ console . log ( 'Setting up webhook' ) ;
9
+ try {
10
+ const [ existingWebhook , groupToken ] = await Promise . all ( [
11
+ storage . get ( `${ STORAGE_KEYS . WEBHOOK_KEY_PREFIX } ${ groupId } ` ) ,
12
+ storage . getSecret ( `${ STORAGE_SECRETS . GROUP_TOKEN_KEY_PREFIX } ${ groupId } ` ) ,
13
+ ] ) ;
14
+
15
+ const isWebhookValid = existingWebhook && ( await getGroupWebhook ( groupId , existingWebhook , groupToken ) ) !== null ;
16
+
17
+ if ( isWebhookValid ) {
18
+ console . log ( 'Using existing webhook' ) ;
19
+ return existingWebhook ;
20
+ }
21
+
22
+ const webtriggerURL = await webTrigger . getUrl ( GITLAB_EVENT_WEBTRIGGER ) ;
23
+ const webtriggerURLWithGroupId = `${ webtriggerURL } ?groupId=${ groupId } ` ;
24
+ const webhookSignature = generateSignature ( ) ;
25
+ const webhookId = await registerGroupWebhook ( {
26
+ groupId,
27
+ url : webtriggerURLWithGroupId ,
28
+ token : groupToken ,
29
+ signature : webhookSignature ,
30
+ } ) ;
31
+
32
+ await Promise . all ( [
33
+ storage . set ( `${ STORAGE_KEYS . WEBHOOK_KEY_PREFIX } ${ groupId } ` , webhookId ) ,
34
+ storage . set ( `${ STORAGE_KEYS . WEBHOOK_SIGNATURE_PREFIX } ${ groupId } ` , webhookSignature ) ,
35
+ ] ) ;
36
+
37
+ console . log ( 'Successfully created webhook' ) ;
38
+ return webhookId ;
39
+ } catch ( e ) {
40
+ console . log ( 'Error setting up webhook, ' , e ) ;
41
+ return null ;
17
42
}
18
-
19
- const webtriggerURL = await webTrigger . getUrl ( GITLAB_EVENT_WEBTRIGGER ) ;
20
- const webtriggerURLWithGroupId = `${ webtriggerURL } ?groupId=${ groupId } ` ;
21
- const webhookSignature = generateSignature ( ) ;
22
- const webhookId = await registerGroupWebhook ( {
23
- groupId,
24
- url : webtriggerURLWithGroupId ,
25
- token : groupToken ,
26
- signature : webhookSignature ,
27
- } ) ;
28
-
29
- await Promise . all ( [
30
- storage . set ( `${ STORAGE_KEYS . WEBHOOK_KEY_PREFIX } ${ groupId } ` , webhookId ) ,
31
- storage . set ( `${ STORAGE_KEYS . WEBHOOK_SIGNATURE_PREFIX } ${ groupId } ` , webhookSignature ) ,
32
- ] ) ;
33
-
34
- return webhookId ;
35
43
} ;
36
44
37
45
export const deleteWebhook = async ( groupId : number ) : Promise < void > => {
0 commit comments