Skip to content
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

feat(api): Add slack integration #531

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@nestjs/swagger": "^7.3.0",
"@nestjs/throttler": "^6.2.1",
"@nestjs/websockets": "^10.3.7",
"@slack/bolt": "^3.22.0",
"@socket.io/redis-adapter": "^8.3.0",
"@supabase/supabase-js": "^2.39.6",
"class-transformer": "^0.5.1",
Expand Down
3,573 changes: 3,573 additions & 0 deletions apps/api/pnpm-lock.yaml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions apps/api/src/integration/integration.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface DiscordIntegrationMetadata extends IntegrationMetadata {
webhookUrl: string
}

export interface SlackIntegrationMetadata extends IntegrationMetadata {
botToken: string;
signingSecret: string;
channelId: string;
}

export interface IntegrationWithWorkspace extends Integration {
workspace: Workspace
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IntegrationType } from '@prisma/client'
import { BaseIntegration } from '../base.integration'
import { DiscordIntegration } from '../discord/discord.integration'
import { InternalServerErrorException } from '@nestjs/common'

import { SlackIntegration } from '../slack/slack.integration'
/**
* Factory class to create integrations. This class will be called to create an integration,
* based on the integration type. This has only a single factory method. You will need to
Expand All @@ -20,6 +20,8 @@ export default class IntegrationFactory {
switch (integrationType) {
case IntegrationType.DISCORD:
return new DiscordIntegration()
case IntegrationType.SLACK:
return new SlackIntegration()
default:
throw new InternalServerErrorException('Integration type not found')
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IntegrationType } from '@prisma/client'
import { SlackIntegration } from './slack.integration'

describe('Slack Integration Test', () => {
let integration: SlackIntegration

beforeEach(() => {
integration = new SlackIntegration()
})

it('should generate slack integration', () => {
expect(integration).toBeDefined()
expect(integration.integrationType).toBe(IntegrationType.SLACK)
})

it('should have the correct permitted events', () => {
const events = integration.getPermittedEvents()
expect(events).toBeDefined()
expect(events.size).toBe(26)
})

it('should have the correct required metadata parameters', () => {
const metadata = integration.getRequiredMetadataParameters()
expect(metadata).toBeDefined()
expect(metadata.size).toBe(3)
expect(metadata.has('botToken')).toBe(true)
expect(metadata.has('signingSecret')).toBe(true)
expect(metadata.has('channelId')).toBe(true)
})
})
122 changes: 122 additions & 0 deletions apps/api/src/integration/plugins/slack/slack.integration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {EventType, IntegrationType} from '@prisma/client'
import {SlackIntegrationMetadata,IntegrationEventData} from '../../integration.types'
import {App} from '@slack/bolt'
import { BaseIntegration } from '../base.integration'
import { Logger } from '@nestjs/common'
import { set, string } from 'zod'
import { MetadataScanner } from '@nestjs/core'

export class SlackIntegration extends BaseIntegration {
private readonly logger = new Logger('SlackIntegration')
private app : App;
constructor() {
super(IntegrationType.SLACK);
}


public getPermittedEvents(): Set<EventType> {
return new Set([
EventType.INTEGRATION_ADDED,
EventType.INTEGRATION_UPDATED,
EventType.INTEGRATION_DELETED,
EventType.INVITED_TO_WORKSPACE,
EventType.REMOVED_FROM_WORKSPACE,
EventType.ACCEPTED_INVITATION,
EventType.DECLINED_INVITATION,
EventType.CANCELLED_INVITATION,
EventType.LEFT_WORKSPACE,
EventType.WORKSPACE_UPDATED,
EventType.WORKSPACE_CREATED,
EventType.WORKSPACE_ROLE_CREATED,
EventType.WORKSPACE_ROLE_UPDATED,
EventType.WORKSPACE_ROLE_DELETED,
EventType.PROJECT_CREATED,
EventType.PROJECT_UPDATED,
EventType.PROJECT_DELETED,
EventType.SECRET_UPDATED,
EventType.SECRET_DELETED,
EventType.SECRET_ADDED,
EventType.VARIABLE_UPDATED,
EventType.VARIABLE_DELETED,
EventType.VARIABLE_ADDED,
EventType.ENVIRONMENT_UPDATED,
EventType.ENVIRONMENT_DELETED,
EventType.ENVIRONMENT_ADDED,
EventType.INTEGRATION_ADDED,
EventType.INTEGRATION_UPDATED,
EventType.INTEGRATION_DELETED
])
}

public getRequiredMetadataParameters(): Set<string> {
return new Set(['botToken','signingSecret','channelId'])
}

async emitEvent(
data: IntegrationEventData,
metadata: SlackIntegrationMetadata
) : Promise<void> {
this.logger.log(`Emitting event to Slack: ${data.title}`)
try{
if(!this.app)
{
this.app = new App({
token: metadata.botToken,
signingSecret: metadata.signingSecret
})
}
const block = [
{
type: 'header',
text: {
type: 'plain_text',
text: 'Update occurred on keyshade',
emoji: true
}
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*${data.title ?? 'No title provided'}*\n${data.description ?? 'No description provided'}`
}
},
{
type: 'divider'
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Event:*\n${data.title}`
},
{
type: 'mrkdwn',
text: `*Source:*\n${data.source}`
}
]
},
{
type: 'context',
elements: [
{
type: 'mrkdwn',
text: '<https://keyshade.xyz|View in Keyshade>'
}
]
}
];
await this.app.client.chat.postMessage({
channel: metadata.channelId,
blocks: block,
text:data.title
});
}
catch(error){
this.logger.error(`Failed to emit event to Slack: ${error.message}`);
console.error(error);
throw error;
}
}
}
Loading