@@ -11,7 +11,7 @@ import { logger } from "@app/lib/logger";
11
11
import { TProjectDALFactory } from "../project/project-dal" ;
12
12
import { TProjectEnvDALFactory } from "../project-env/project-env-dal" ;
13
13
import { TWebhookDALFactory } from "./webhook-dal" ;
14
- import { WebhookType } from "./webhook-types" ;
14
+ import { TWebhookPayloads , WebhookEvents , WebhookType } from "./webhook-types" ;
15
15
16
16
const WEBHOOK_TRIGGER_TIMEOUT = 15 * 1000 ;
17
17
@@ -54,29 +54,64 @@ export const triggerWebhookRequest = async (
54
54
return req ;
55
55
} ;
56
56
57
- export const getWebhookPayload = (
58
- eventName : string ,
59
- details : {
60
- workspaceName : string ;
61
- workspaceId : string ;
62
- environment : string ;
63
- secretPath ?: string ;
64
- type ?: string | null ;
57
+ export const getWebhookPayload = ( event : TWebhookPayloads ) => {
58
+ if ( event . type === WebhookEvents . SecretModified ) {
59
+ const { projectName, projectId, environment, secretPath, type } = event . payload ;
60
+
61
+ switch ( type ) {
62
+ case WebhookType . SLACK :
63
+ return {
64
+ text : "A secret value has been added or modified." ,
65
+ attachments : [
66
+ {
67
+ color : "#E7F256" ,
68
+ fields : [
69
+ {
70
+ title : "Project" ,
71
+ value : projectName ,
72
+ short : false
73
+ } ,
74
+ {
75
+ title : "Environment" ,
76
+ value : environment ,
77
+ short : false
78
+ } ,
79
+ {
80
+ title : "Secret Path" ,
81
+ value : secretPath ,
82
+ short : false
83
+ }
84
+ ]
85
+ }
86
+ ]
87
+ } ;
88
+ case WebhookType . GENERAL :
89
+ default :
90
+ return {
91
+ event : event . type ,
92
+ project : {
93
+ workspaceId : projectId ,
94
+ projectName,
95
+ environment,
96
+ secretPath
97
+ }
98
+ } ;
99
+ }
65
100
}
66
- ) => {
67
- const { workspaceName , workspaceId , environment, secretPath, type } = details ;
101
+
102
+ const { projectName , projectId , environment, secretPath, type, reminderNote , secretName } = event . payload ;
68
103
69
104
switch ( type ) {
70
105
case WebhookType . SLACK :
71
106
return {
72
- text : "A secret value has been added or modified. " ,
107
+ text : "You have a secret reminder " ,
73
108
attachments : [
74
109
{
75
110
color : "#E7F256" ,
76
111
fields : [
77
112
{
78
113
title : "Project" ,
79
- value : workspaceName ,
114
+ value : projectName ,
80
115
short : false
81
116
} ,
82
117
{
@@ -88,6 +123,16 @@ export const getWebhookPayload = (
88
123
title : "Secret Path" ,
89
124
value : secretPath ,
90
125
short : false
126
+ } ,
127
+ {
128
+ title : "Secret Name" ,
129
+ value : secretName ,
130
+ short : false
131
+ } ,
132
+ {
133
+ title : "Reminder Note" ,
134
+ value : reminderNote ,
135
+ short : false
91
136
}
92
137
]
93
138
}
@@ -96,11 +141,14 @@ export const getWebhookPayload = (
96
141
case WebhookType . GENERAL :
97
142
default :
98
143
return {
99
- event : eventName ,
144
+ event : event . type ,
100
145
project : {
101
- workspaceId,
146
+ workspaceId : projectId ,
147
+ projectName,
102
148
environment,
103
- secretPath
149
+ secretPath,
150
+ secretName,
151
+ reminderNote
104
152
}
105
153
} ;
106
154
}
@@ -110,6 +158,7 @@ export type TFnTriggerWebhookDTO = {
110
158
projectId : string ;
111
159
secretPath : string ;
112
160
environment : string ;
161
+ event : TWebhookPayloads ;
113
162
webhookDAL : Pick < TWebhookDALFactory , "findAllWebhooks" | "transaction" | "update" | "bulkUpdate" > ;
114
163
projectEnvDAL : Pick < TProjectEnvDALFactory , "findOne" > ;
115
164
projectDAL : Pick < TProjectDALFactory , "findById" > ;
@@ -124,8 +173,9 @@ export const fnTriggerWebhook = async ({
124
173
projectId,
125
174
webhookDAL,
126
175
projectEnvDAL,
127
- projectDAL,
128
- secretManagerDecryptor
176
+ event,
177
+ secretManagerDecryptor,
178
+ projectDAL
129
179
} : TFnTriggerWebhookDTO ) => {
130
180
const webhooks = await webhookDAL . findAllWebhooks ( projectId , environment ) ;
131
181
const toBeTriggeredHooks = webhooks . filter (
@@ -134,21 +184,20 @@ export const fnTriggerWebhook = async ({
134
184
) ;
135
185
if ( ! toBeTriggeredHooks . length ) return ;
136
186
logger . info ( { environment, secretPath, projectId } , "Secret webhook job started" ) ;
137
- const project = await projectDAL . findById ( projectId ) ;
187
+ let { projectName } = event . payload ;
188
+ if ( ! projectName ) {
189
+ const project = await projectDAL . findById ( event . payload . projectId ) ;
190
+ projectName = project . name ;
191
+ }
192
+
138
193
const webhooksTriggered = await Promise . allSettled (
139
- toBeTriggeredHooks . map ( ( hook ) =>
140
- triggerWebhookRequest (
141
- hook ,
142
- secretManagerDecryptor ,
143
- getWebhookPayload ( "secrets.modified" , {
144
- workspaceName : project . name ,
145
- workspaceId : projectId ,
146
- environment,
147
- secretPath,
148
- type : hook . type
149
- } )
150
- )
151
- )
194
+ toBeTriggeredHooks . map ( ( hook ) => {
195
+ const formattedEvent = {
196
+ type : event . type ,
197
+ payload : { ...event . payload , type : hook . type , projectName }
198
+ } as TWebhookPayloads ;
199
+ return triggerWebhookRequest ( hook , secretManagerDecryptor , getWebhookPayload ( formattedEvent ) ) ;
200
+ } )
152
201
) ;
153
202
154
203
// filter hooks by status
0 commit comments