forked from evergreen-ci/spruce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EVG-19151: Generate unique webhook secrets (evergreen-ci#1733)
- Loading branch information
Showing
8 changed files
with
222 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/pages/projectSettings/tabs/NotificationsTab/getGqlPayload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { projectTriggers, allowedSelectors } from "constants/triggers"; | ||
import { SubscriptionInput } from "gql/generated/types"; | ||
import { NotificationMethods } from "types/subscription"; | ||
import { ExtraField } from "types/triggers"; | ||
import { Unpacked } from "types/utils"; | ||
import { | ||
FormState, | ||
Notification, | ||
FormExtraFields, | ||
FormRegexSelector, | ||
} from "./types"; | ||
import { getTargetForMethod, generateWebhookSecret } from "./utils"; | ||
|
||
// Converts the form regexSelector into the proper format for GQL payload. | ||
// We need to check if the trigger has regex selectors because it's possible for data | ||
// from other dependencies to persist. | ||
const regexFormToGql = ( | ||
hasRegexSelectors: boolean, | ||
regexForm: FormRegexSelector[] | ||
) => | ||
hasRegexSelectors && regexForm | ||
? regexForm.map((r) => ({ | ||
type: r.regexSelect, | ||
data: r.regexInput, | ||
})) | ||
: []; | ||
|
||
const webhookFormToGql = (webhookInput: Notification["webhookInput"]) => { | ||
if (!webhookInput) { | ||
return null; | ||
} | ||
return { | ||
url: webhookInput.urlInput, | ||
// Use existing secret if it was already generated, otherwise generate a new secret. | ||
secret: webhookInput.secretInput || generateWebhookSecret(), | ||
headers: | ||
webhookInput.httpHeaders?.map(({ keyInput, valueInput }) => ({ | ||
key: keyInput, | ||
value: valueInput, | ||
})) ?? [], | ||
}; | ||
}; | ||
|
||
const jiraFormToGql = (jiraInput: Notification["jiraIssueInput"]) => { | ||
if (!jiraInput) { | ||
return null; | ||
} | ||
return { | ||
project: jiraInput.projectInput, | ||
issueType: jiraInput.issueInput, | ||
}; | ||
}; | ||
|
||
// Converts the form extraFields into the proper format for GQL payload. | ||
// We need to check what extraFields exist for a particular trigger because it's possible | ||
// for data from other dependencies to persist. | ||
const extraFieldsFormToGql = ( | ||
extraFieldsToInclude: ExtraField[], | ||
extraFieldsForm: FormExtraFields | ||
) => | ||
(extraFieldsToInclude || []).reduce((acc, e) => { | ||
if (extraFieldsForm[e.key]) { | ||
acc[e.key] = extraFieldsForm[e.key].toString(); | ||
} | ||
return acc; | ||
}, {} as { [key: string]: string }); | ||
|
||
export const getGqlPayload = | ||
(projectId: string) => | ||
(subscription: Unpacked<FormState["subscriptions"]>): SubscriptionInput => { | ||
const { subscriptionData } = subscription; | ||
const event = projectTriggers[subscriptionData.event.eventSelect]; | ||
const { | ||
resourceType = "", | ||
trigger, | ||
extraFields, | ||
regexSelectors, | ||
} = event || {}; | ||
|
||
const triggerData = extraFieldsFormToGql( | ||
extraFields, | ||
subscriptionData.event.extraFields | ||
); | ||
|
||
const regexData = regexFormToGql( | ||
!!regexSelectors, | ||
subscriptionData.event.regexSelector | ||
); | ||
|
||
const method = subscriptionData.notification.notificationSelect; | ||
const subscriber = getTargetForMethod( | ||
method, | ||
subscriptionData?.notification | ||
); | ||
|
||
const selectors = Object.entries(triggerData) | ||
.map(([key, value]) => ({ | ||
type: key, | ||
data: value.toString(), | ||
})) | ||
.filter(({ type }) => allowedSelectors.has(type)); | ||
|
||
return { | ||
id: subscriptionData.id, | ||
owner_type: "project", | ||
regex_selectors: regexData, | ||
resource_type: resourceType, | ||
selectors: [{ type: "project", data: projectId }, ...selectors], | ||
subscriber: { | ||
type: method, | ||
target: subscriber, | ||
webhookSubscriber: | ||
method === NotificationMethods.WEBHOOK | ||
? webhookFormToGql(subscriptionData.notification?.webhookInput) | ||
: undefined, | ||
jiraIssueSubscriber: | ||
method === NotificationMethods.JIRA_ISSUE | ||
? jiraFormToGql(subscriptionData.notification?.jiraIssueInput) | ||
: undefined, | ||
}, | ||
trigger, | ||
trigger_data: triggerData, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.