-
-
Notifications
You must be signed in to change notification settings - Fork 115
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 #524
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
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; | ||
} | ||
} | ||
} | ||
|
||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,11 @@ | |
"version": "0.0.0", | ||
"private": true, | ||
"devDependencies": { | ||
"@vercel/style-guide": "^5.0.0", | ||
"eslint-config-turbo": "^1.10.12", | ||
"typescript": "^4.5.3" | ||
"@vercel/style-guide": "^5.0.0", | ||
"eslint-config-turbo": "^1.10.12", | ||
"typescript": "^4.5.3" | ||
}, | ||
"dependencies": { | ||
"eslint-config-custom": "file:" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why you included this |
||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"@keyshade/schema": "file:", | ||
"zod": "^3.23.6" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry , I did not intend to add all of the package.json changes in the final commit. It has no significance whatsoever I was just testing/exploring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, but you should remove it in your next commit :)