Skip to content

feat: add sentry integration #272

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

Open
wants to merge 8 commits into
base: main
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
4 changes: 4 additions & 0 deletions actions/sentry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.trigger
trigger
trigger.config.ts
4 changes: 4 additions & 0 deletions actions/sentry/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
12 changes: 12 additions & 0 deletions actions/sentry/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"slug": "sentry",
"name": "sentry",
"description": "",
"triggers": [
{
"type": "source_webhook",
"entities": ["sentry"]
}
],
"integrations": ["sentry"]
}
98 changes: 98 additions & 0 deletions actions/sentry/handlers/get-inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
ActionEventPayload,
getTeams,
IntegrationAccount,
Team,
} from '@tegonhq/sdk';
import axios from 'axios';
import { getSentryHeaders } from 'utils';
import * as console from 'node:console';

export const getInputs = async (payload: ActionEventPayload) => {
const { workspaceId, integrationAccounts } = payload;

const integrationAccount = integrationAccounts.sentry;
// Fetch teams from the API

const teams = await getTeams({ workspaceId });

// Create a map of teams with label and value properties
const teamOptions = teams.map((team: Team) => ({
label: team.name,
value: team.id,
}));

const sentryProjects = await getAllSentryProjects(integrationAccount);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let projectId: any = {
type: 'text',
title: 'Channels',
validation: {
required: true,
},
};

if (sentryProjects.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const projectOptions = sentryProjects.map((project: any) => ({
label: project.name,
value: project.id,
}));
projectId = {
type: 'select',
title: 'Channels',
validation: {
required: true,
},
options: projectOptions,
};
}

return {
type: 'object',
properties: {
projectTeamMappings: {
type: 'array',
title: 'Project to Team Mappings',
description: 'Map each project to a team',
items: {
type: 'object',
properties: {
projectId,
teamId: {
type: 'select',
title: 'Teams',
validation: {
required: true,
},
options: teamOptions,
},
},
},
},
},
};
};

async function getAllSentryProjects(integrationAccount: IntegrationAccount) {
try {
const { orgSlug } = integrationAccount.integrationConfiguration as any;

const url = `https://sentry.io/api/0/organizations/${orgSlug}/projects/`;
const response = await axios.get(
url,
getSentryHeaders(integrationAccount), // Assume getSentryHeaders sets the authorization headers
);

const sentryProjects = response.data;

if (response.status !== 200) {
throw new Error(`Error fetching Sentry projects: ${response.statusText}`);
}

return sentryProjects;
} catch (error) {
return [];
}
}
61 changes: 61 additions & 0 deletions actions/sentry/handlers/webhook-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
ActionEventPayload,
logger,
getWorkflowsByTeam,
createIssue,
} from '@tegonhq/sdk';

export const webhookHandler = async (payload: ActionEventPayload) => {
const { eventBody } = payload;

if (eventBody.action === 'triggered') {
return createTegonIssue(payload);
}
};

async function createTegonIssue(payload: ActionEventPayload): Promise<any> {
const { eventBody, integrationAccounts, action } = payload;

const { project, issue_url, title, exception, web_url, user } = eventBody.data
.event as any;

// Find the channel mapping for the given channel ID
const projectMapping = action.data.inputs.projectTeamMappings.find(
({ projectId: mappedProjectId }: { projectId: string }) =>
mappedProjectId.toString() === project.toString(),
);

if (!projectMapping) {
logger.debug(`The projectMapping is not connected`);
return undefined;
}

const teamId = projectMapping.teamId;

const workflows = (await getWorkflowsByTeam({
teamId,
})) as any[];

const todoWorkflow = workflows.find(
(workflow) => workflow.category === 'BACKLOG',
);

await createIssue({
teamId,
title,
description: JSON.stringify(exception),
stateId: todoWorkflow.id,
linkIssueData: {
url: issue_url,
sourceId: integrationAccounts.sentry.integrationDefinitionId,
sourceData: {
title,
issueId: web_url,
project,
actor: user,
},
},
});

return;
}
18 changes: 18 additions & 0 deletions actions/sentry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ActionEventPayload, ActionTypesEnum } from '@tegonhq/sdk';
import { webhookHandler } from './handlers/webhook-handler';
import { getInputs } from './handlers/get-inputs';

export async function run(eventPayload: ActionEventPayload) {
switch (eventPayload.event) {
case ActionTypesEnum.GET_INPUTS:
return getInputs(eventPayload);

case ActionTypesEnum.SOURCE_WEBHOOK:
return webhookHandler(eventPayload);

default:
return {
message: `The event payload type "${eventPayload.event}" is not recognized`,
};
}
}
30 changes: 30 additions & 0 deletions actions/sentry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "sentry",
"version": "0.1.0",
"description": "Basic slack workflows",
"private": true,
"scripts": {
"lint": "eslint . --fix",
"trigger-dev": "npx trigger.dev@beta dev -a http://localhost:3030"
},
"packageManager": "pnpm@8.15.6",
"dependencies": {
"@tegonhq/sdk": "^0.1.12",
"@trigger.dev/sdk": "3.0.0-beta.56",
"axios": "^1.6.7",
"form-data": "^4.0.0"
},
"devDependencies": {
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^22.1.0",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-only-warn": "^1.1.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-unused-imports": "^3.0.0",
"jsonwebtoken": "^9.0.2",
"typescript": "^5.6.3"
}
}
Loading