-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Enable selection of all CI pipelines at once when the Environment filter is applied in Notifications #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0949f72
server.ts refactored
prkhrkat 1e9facf
Merge branch 'notif-test' into notifier-refact
prkhrkat 1ae839f
sendNotification refactored
prkhrkat 571a617
sendNotificationV2 added
prkhrkat 4cf4544
logs added
prkhrkat aa3a9aa
Merge pull request #131 from devtron-labs/notifier-refact-sendNotif
prkhrkat 3c9faa5
Merge branch 'develop' into notifier-refact
prkhrkat d5f5f22
removed example
prkhrkat feba36a
v2 support in nats client
prkhrkat 52ee334
send notif with always nats
prkhrkat f5444d2
Merge branch 'develop' into notifier-refact
prkhrkat caccc8a
Revert "send notif with always nats"
prkhrkat c87a881
Merge branch 'develop' into notifier-refact
prkhrkat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,32 @@ | ||
| /* | ||
| * Copyright (c) 2024. Devtron Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import express from 'express'; | ||
| import bodyParser from 'body-parser'; | ||
| import { metricsMiddleware } from './middleware/metrics'; | ||
|
|
||
| export const createApp = () => { | ||
| const app = express(); | ||
|
|
||
| // Middleware | ||
| app.use(bodyParser.json({ limit: '10mb' })); | ||
| app.use(express.json()); | ||
| app.use(metricsMiddleware); | ||
|
|
||
| // Routes | ||
|
|
||
| return app; | ||
| }; |
This file contains hidden or 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,66 @@ | ||
| /* | ||
| * Copyright (c) 2024. Devtron Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { ConnectionOptions, createConnection } from "typeorm"; | ||
| import { NotificationSettings } from "../entities/notificationSettings"; | ||
| import { NotifierEventLog } from "../entities/notifierEventLogs"; | ||
| import { Event } from "../notification/service/notificationService"; | ||
| import { NotificationTemplates } from "../entities/notificationTemplates"; | ||
| import { SlackConfig } from "../entities/slackConfig"; | ||
| import { SesConfig } from "../entities/sesConfig"; | ||
| import { SMTPConfig } from "../entities/smtpConfig"; | ||
| import { WebhookConfig } from "../entities/webhookconfig"; | ||
| import { Users } from "../entities/users"; | ||
| import { logger } from "./logger"; | ||
| import * as process from "process"; | ||
|
|
||
| export const connectToDatabase = async () => { | ||
| const dbHost: string = process.env.DB_HOST; | ||
| const dbPort: number = +process.env.DB_PORT; | ||
| const user: string = process.env.DB_USER; | ||
| const pwd: string = process.env.DB_PWD; | ||
| const db: string = process.env.DB; | ||
|
|
||
| const dbOptions: ConnectionOptions = { | ||
| type: "postgres", | ||
| host: dbHost, | ||
| port: dbPort, | ||
| username: user, | ||
| password: pwd, | ||
| database: db, | ||
| entities: [ | ||
| NotificationSettings, | ||
| NotifierEventLog, | ||
| Event, | ||
| NotificationTemplates, | ||
| SlackConfig, | ||
| SesConfig, | ||
| SMTPConfig, | ||
| WebhookConfig, | ||
| Users | ||
| ] | ||
| }; | ||
|
|
||
| try { | ||
| const connection = await createConnection(dbOptions); | ||
| logger.info("Connected to DB"); | ||
| return connection; | ||
| } catch (error) { | ||
| logger.error("TypeORM connection error: ", error); | ||
| logger.error("shutting down notifier due to un-successful database connection..."); | ||
| process.exit(1); | ||
| } | ||
| }; |
This file contains hidden or 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,28 @@ | ||
| /* | ||
| * Copyright (c) 2024. Devtron Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as winston from 'winston'; | ||
|
|
||
| export const logger = winston.createLogger({ | ||
| level: 'info', | ||
| format: winston.format.combine( | ||
| winston.format.timestamp(), | ||
| winston.format.printf(info => { | ||
| return `${info.timestamp} ${info.level}: ${info.message}`; | ||
| }) | ||
| ), | ||
| transports: [new winston.transports.Console()] | ||
| }); |
This file contains hidden or 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,80 @@ | ||
| /* | ||
| * Copyright (c) 2024. Devtron Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { connect, NatsConnection } from "nats"; | ||
| import { logger } from "./logger"; | ||
| import { PubSubServiceImpl } from "../pubSub/pubSub"; | ||
| import { NOTIFICATION_EVENT_TOPIC } from "../pubSub/utils"; | ||
| import { Event } from "../notification/service/notificationService"; | ||
| import { NotificationService } from "../notification/service/notificationService"; | ||
| import { successNotificationMetricsCounter, failedNotificationMetricsCounter } from '../common/metrics'; | ||
|
|
||
| export const natsEventHandler = (notificationService: NotificationService) => async (msg: string) => { | ||
| const eventAsString = JSON.parse(msg); | ||
| const parsedData = JSON.parse(eventAsString); | ||
| let response; | ||
|
|
||
| try { | ||
| // First try to parse as V2 format (which includes both event and notificationSettings) | ||
| logger.info('Attempting to parse as V2 payload'); | ||
| if (parsedData.event && parsedData.notificationSettings) { | ||
| // This is a V2 payload | ||
| const { event, notificationSettings } = parsedData; | ||
| logger.info({ natsEventBodyV2: { event, notificationSettingsCount: notificationSettings.length } }); | ||
| response = await notificationService.sendNotificationV2(event, notificationSettings); | ||
| } else { | ||
| // Fall back to V1 format (which only includes the event) | ||
| logger.info('Falling back to V1 payload format'); | ||
| const event = parsedData as Event; | ||
| logger.info({ natsEventBodyV1: event }); | ||
| response = await notificationService.sendNotification(event); | ||
| } | ||
| } catch (error: any) { | ||
| { | ||
| logger.error(`Failed to process message in both V1 and V2 formats: ${error.message || 'Unknown error'}`); | ||
| failedNotificationMetricsCounter.inc(); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| // Handle response metrics | ||
| if (response.status != 0) { | ||
| successNotificationMetricsCounter.inc(); | ||
| } else { | ||
| failedNotificationMetricsCounter.inc(); | ||
| } | ||
| }; | ||
|
|
||
| export const connectToNats = async (notificationService: NotificationService) => { | ||
| const natsUrl = process.env.NATS_URL; | ||
|
|
||
| if (!natsUrl) { | ||
| logger.info("NATS_URL not provided, skipping NATS connection"); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| logger.info("Connecting to NATS server..."); | ||
| const conn: NatsConnection = await connect({ servers: natsUrl }); | ||
| const jsm = await conn.jetstreamManager(); | ||
| const pubSubService = new PubSubServiceImpl(conn, jsm, logger); | ||
| await pubSubService.Subscribe(NOTIFICATION_EVENT_TOPIC, natsEventHandler(notificationService)); | ||
| logger.info("Successfully connected to NATS"); | ||
| return conn; | ||
| } catch (err) { | ||
| logger.error("Error connecting to NATS:", err); | ||
| } | ||
| }; | ||
This file contains hidden or 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,27 @@ | ||
| /* | ||
| * Copyright (c) 2024. Devtron Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { Request, Response, NextFunction } from 'express'; | ||
| import { httpRequestMetricsCounter } from '../common/metrics'; | ||
|
|
||
| export const metricsMiddleware = (req: Request, res: Response, next: NextFunction) => { | ||
| httpRequestMetricsCounter.labels({ | ||
| method: req.method, | ||
| endpoint: req.url, | ||
| statusCode: res.statusCode | ||
| }).inc(); | ||
| next(); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.