-
Notifications
You must be signed in to change notification settings - Fork 35
/
commentHandler.ts
75 lines (69 loc) · 2.25 KB
/
commentHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {Response} from 'express';
import Item, {ItemComment} from '../../../models/Item.model';
import SentryInstallation from '../../../models/SentryInstallation.model';
async function handleCreated(item: Item, comment: ItemComment) {
await item.update({comments: [...(item.comments ?? []), comment]});
console.info(`Added new comment from Sentry issue`);
}
async function handleUpdated(item: Item, comment: ItemComment) {
// Create a copy since Array.prototype.splice mutates the original array
const comments = [...(item.comments ?? [])];
const commentIndex = comments.findIndex(
c => c.sentryCommentId === comment.sentryCommentId
);
comments.splice(commentIndex, 1, comment);
await item.update({comments});
console.info(`Updated comment from Sentry issue`);
}
async function handleDeleted(item: Item, comment: ItemComment) {
await item.update({
comments: (item.comments ?? []).filter(
c => c.sentryCommentId !== comment.sentryCommentId
),
});
console.info(`Deleted comment from Sentry issue`);
}
export default async function commentHandler(
response: Response,
action: string,
sentryInstallation: SentryInstallation,
data: Record<string, any>,
actor: Record<string, any>
): Promise<void> {
const item = await Item.findOne({
where: {
sentryId: `${data.issue_id}`,
organizationId: sentryInstallation.organizationId,
},
});
if (!item) {
console.info(`Ignoring comment for unlinked Sentry issue`);
response.status(200);
return;
}
// In your application you may want to map Sentry user IDs (actor.id) to your internal user IDs
// for a richer comment sync experience
const incomingComment: ItemComment = {
text: data.comment,
author: actor.name,
timestamp: data.timestamp,
sentryCommentId: data.comment_id,
};
switch (action) {
case 'created':
await handleCreated(item, incomingComment);
response.status(201);
break;
case 'updated':
await handleUpdated(item, incomingComment);
response.status(200);
break;
case 'deleted':
await handleDeleted(item, incomingComment);
response.status(204);
break;
default:
console.info(`Unexpected Sentry comment action: ${action}`);
response.status(400);
}
}