Skip to content

Commit

Permalink
feat: add SENTRY_PROJECT_SLUG_ALLOW_LIST env
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanngocptn committed May 22, 2024
1 parent b9100ec commit 2070879
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ TELEGRAM_GROUP_ID=<your telegram group id or telegram channel id>
TELEGRAM_TOPIC_ID=<not require - your telegram topic (when enable Topics in group setting)>
SENTRY_INTEGRATION_TOKEN=<token in Custom Integrations in Sentry Setting>
SENTRY_ORGANIZATION_SLUG=<your Sentry org slug>
SENTRY_PROJECT_SLUG_ALLOW_LIST=<your Project slug list (Not require - split with comma `,` character)>
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ $ npm install

## Environment variables

| Name | Is Require | Type | Note | Value |
| ------------------------ | ---------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| LANGUAGE | Yes | string | The language of message when send | vi,en |
| TELEGRAM_BOT_TOKEN | Yes | string | The token from @BotFather telegram check [HERE](https://core.telegram.org/bots/tutorial#obtain-your-bot-token) for see how to get this. | |
| TELEGRAM_GROUP_ID | Yes | number | The id of your telegram group. You can use telegram API for check that through Postman. Check [HERE](https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id) for more detail. Normally, that start with -100... | |
| TELEGRAM_TOPIC_ID | No | number | The id of you topic if you enable Topics in group setting. | |
| SENTRY_INTEGRATION_TOKEN | Yes | string | The token that geneate by sentry. Check [HERE](https://docs.sentry.io/organization/integrations/integration-platform/#permissions) for more detail. | |
| SENTRY_ORGANIZATION_SLUG | Yes | string | The organization slug in your sentry organization setting | |
| Name | Is Require | Type | Note | Value |
| ------------------------------ | ---------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| LANGUAGE | Yes | string | The language of message when send | vi,en |
| TELEGRAM_BOT_TOKEN | Yes | string | The token from @BotFather telegram check [HERE](https://core.telegram.org/bots/tutorial#obtain-your-bot-token) for see how to get this. | |
| TELEGRAM_GROUP_ID | Yes | number | The id of your telegram group. You can use telegram API for check that through Postman. Check [HERE](https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id) for more detail. Normally, that start with -100... | |
| TELEGRAM_TOPIC_ID | No | number | The id of you topic if you enable Topics in group setting. | |
| SENTRY_INTEGRATION_TOKEN | Yes | string | The token that geneate by sentry. Check [HERE](https://docs.sentry.io/organization/integrations/integration-platform/#permissions) for more detail. | |
| SENTRY_ORGANIZATION_SLUG | Yes | string | The organization slug in your sentry organization setting | |
| SENTRY_PROJECT_SLUG_ALLOW_LIST | No | string | your Project slug list (Not require - split with comma `,` character)setting | |

## Custom Integrations Configuration

Expand Down Expand Up @@ -72,6 +73,7 @@ $ npm install
TELEGRAM_TOPIC_ID=<not require - your telegram topic (when enable Topics in group setting)>
SENTRY_INTEGRATION_TOKEN=<token in Custom Integrations in Sentry Setting>
SENTRY_ORGANIZATION_SLUG=<your Sentry org slug>
SENTRY_PROJECT_SLUG_ALLOW_LIST=<your Project slug list (Not require - split with comma `,` charactor)>
```

3. Run with npm
Expand Down Expand Up @@ -105,6 +107,7 @@ $ npm install
-e TELEGRAM_TOPIC_ID=<TELEGRAM_TOPIC_ID> \ #not require - your telegram topic when enable Topics in group setting
-e SENTRY_INTEGRATION_TOKEN=<SENTRY_INTEGRATION_TOKEN> \ #token in Custom Integrations in Sentry Setting
-e SENTRY_ORGANIZATION_SLUG=<SENTRY_ORGANIZATION_SLUG> \ #your Sentry org slug
-e SENTRY_PROJECT_SLUG_ALLOW_LIST=<SENTRY_PROJECT_SLUG_ALLOW_LIST> \ your Project slug list (Not require - split with comma `,` character)
tuanngocptn/sentry-telegram-webhook:latest
```

Expand All @@ -126,6 +129,7 @@ $ npm install
- TELEGRAM_TOPIC_ID=<not require - your telegram topic (when enable Topics in group setting)>
- SENTRY_INTEGRATION_TOKEN=<token in Custom Integrations in Sentry Setting>
- SENTRY_ORGANIZATION_SLUG=<your Sentry org slug>
- SENTRY_PROJECT_SLUG_ALLOW_LIST=<your Project slug list (Not require - split with comma `,` character)>
ports:
- 3000:3000
volumes:
Expand All @@ -150,6 +154,7 @@ $ npm install
- TELEGRAM_TOPIC_ID=<not require - your telegram topic (when enable Topics in group setting)>
- SENTRY_INTEGRATION_TOKEN=<token in Custom Integrations in Sentry Setting>
- SENTRY_ORGANIZATION_SLUG=<your Sentry org slug>
- SENTRY_PROJECT_SLUG_ALLOW_LIST=<your Project slug list (Not require - split with comma `,` character)>
```

- Run with docker:
Expand All @@ -176,6 +181,7 @@ $ npm install
- TELEGRAM_TOPIC_ID=<not require - your telegram topic (when enable Topics in group setting)>
- SENTRY_INTEGRATION_TOKEN=<token in Custom Integrations in Sentry Setting>
- SENTRY_ORGANIZATION_SLUG=<your Sentry org slug>
- SENTRY_PROJECT_SLUG_ALLOW_LIST=<your Project slug list (Not require - split with comma `,` character)>
```

- Run with docker:
Expand Down
1 change: 1 addition & 0 deletions docker-compose-prod-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
- TELEGRAM_TOPIC_ID=<not require - your telegram topic (when enable Topics in group setting)>
- SENTRY_INTEGRATION_TOKEN=<token in Custom Integrations in Sentry Setting>
- SENTRY_ORGANIZATION_SLUG=<your Sentry org slug>
- SENTRY_PROJECT_SLUG_ALLOW_LIST=<your Project slug list (Not require - split with comma `,` character)>
build:
context: .
ports:
Expand Down
1 change: 1 addition & 0 deletions docker-compose-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
- TELEGRAM_TOPIC_ID=<not require - your telegram topic (when enable Topics in group setting)>
- SENTRY_INTEGRATION_TOKEN=<token in Custom Integrations in Sentry Setting>
- SENTRY_ORGANIZATION_SLUG=<your Sentry org slug>
- SENTRY_PROJECT_SLUG_ALLOW_LIST=<your Project slug list (Not require - split with comma `,` character)>
build:
context: .
ports:
Expand Down
9 changes: 7 additions & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Logger } from 'winston';
import { HookMessageDataType, SentryRequestType } from './app';
import { AppInterceptor } from './app.interceptor';
import { AppService } from './app.service';
import { AppHelper } from './app.helper';

@Controller()
@UseInterceptors(AppInterceptor)
Expand All @@ -20,6 +21,7 @@ export class AppController {
@Inject('winston')
private readonly logger: Logger,
private readonly appService: AppService,
private readonly appHelper: AppHelper,
) {}

@Get()
Expand All @@ -31,9 +33,12 @@ export class AppController {
@HttpCode(HttpStatus.OK)
webhooks(@Body() reqBody: SentryRequestType) {
const running = async () => {
this.logger.info(reqBody.data);
const { issue } = reqBody.data;
try {
this.logger.info(reqBody.data);
const { issue } = reqBody.data;
if (!this.appHelper.isAllowNotification(issue.project.slug)) {
return;
}
const issueDetails = await this.appService.getIssueDetail(issue.id);
const hookMessageData: HookMessageDataType = {
issueAction: reqBody.action,
Expand Down
23 changes: 23 additions & 0 deletions src/app.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,27 @@ export class AppHelper {
}
return generateHookMessageEn(data);
}

isAllowNotification(projectSlug: string): boolean {
const sentryProjectSlugFilter: string =
process.env.SENTRY_PROJECT_SLUG_ALLOW_LIST;

if (!sentryProjectSlugFilter) {
return true;
}

const projectSlugFilter = sentryProjectSlugFilter
.split(',')
.map((item) => item.trim())
.filter((item) => !!item);

if (
projectSlugFilter.length > 0 &&
!projectSlugFilter.includes(projectSlug)
) {
return false;
}

return true;
}
}

0 comments on commit 2070879

Please sign in to comment.