Skip to content

Commit

Permalink
feat: Support and test Sentry.sendFeedback (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Mar 4, 2024
1 parent e4c7597 commit 668c03f
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/common/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export function normalizeEvent(event: Event, basePath: string): Event {
request.url = normalizeUrlToBase(request.url, basePath);
}

if (event.contexts?.feedback?.url && typeof event.contexts.feedback.url === 'string') {
event.contexts.feedback.url = normalizeUrlToBase(event.contexts.feedback.url, basePath);
}

event.contexts = {
...event.contexts,
runtime: {
Expand Down
2 changes: 1 addition & 1 deletion src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function eventFromEnvelope(envelope: Envelope): [Event, Attachment[], Profile |
let profile: Profile | undefined;

forEachEnvelopeItem(envelope, (item, type) => {
if (type === 'event' || type === 'transaction') {
if (type === 'event' || type === 'transaction' || type === 'feedback') {
event = Array.isArray(item) ? (item as EventItem)[1] : undefined;
} else if (type === 'attachment') {
const [headers, data] = item as AttachmentItem;
Expand Down
29 changes: 18 additions & 11 deletions test/e2e/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Event, Profile, ReplayEvent, Session, Transaction } from '@sentry/types';
import { forEachEnvelopeItem, parseEnvelope } from '@sentry/utils';
import { dropUndefinedKeys, forEachEnvelopeItem, parseEnvelope } from '@sentry/utils';
import { Server } from 'http';
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
Expand Down Expand Up @@ -129,7 +129,12 @@ export class TestServer {
let metrics: string | undefined;

forEachEnvelopeItem(envelope, ([headers, item]) => {
if (headers.type === 'event' || headers.type === 'transaction' || headers.type === 'session') {
if (
headers.type === 'event' ||
headers.type === 'transaction' ||
headers.type === 'session' ||
headers.type === 'feedback'
) {
data = item as Event | Transaction | Session;
}

Expand All @@ -155,15 +160,17 @@ export class TestServer {
});

if (data || metrics) {
this._addEvent({
data: data || {},
attachments,
profile,
metrics,
appId: ctx.params.id,
sentryKey: keyMatch[1],
method: 'envelope',
});
this._addEvent(
dropUndefinedKeys({
data: data || {},
attachments,
profile,
metrics,
appId: ctx.params.id,
sentryKey: keyMatch[1],
method: 'envelope',
}),
);

ctx.status = 200;
ctx.body = 'Success';
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/test-apps/other/feedback/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"method": "envelope",
"sentryKey": "37f8a2ee37c0409d8970bc7559c7c7e4",
"appId": "277345",
"data": {
"contexts": {
"feedback": {
"contact_email": "[email protected]",
"name": "John Doe",
"message": "I really like your App, thanks!",
"url": "app:///src/index.html",
"source": "api"
}
},
"type": "feedback"
}
}
8 changes: 8 additions & 0 deletions test/e2e/test-apps/other/feedback/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "feedback",
"version": "1.0.0",
"main": "src/main.js",
"dependencies": {
"@sentry/electron": "3.0.0"
}
}
2 changes: 2 additions & 0 deletions test/e2e/test-apps/other/feedback/recipe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: User Feedback
command: yarn
24 changes: 24 additions & 0 deletions test/e2e/test-apps/other/feedback/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script>
const crypto = require('crypto');
const { init, sendFeedback } = require('@sentry/electron/renderer');

init({
debug: true,
});

setTimeout(() => {
sendFeedback({
name: 'John Doe',
email: '[email protected]',
message: 'I really like your App, thanks!',
});
}, 500);
</script>
</body>
</html>
23 changes: 23 additions & 0 deletions test/e2e/test-apps/other/feedback/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('path');

const { app, BrowserWindow } = require('electron');
const { init } = require('@sentry/electron');

init({
dsn: '__DSN__',
debug: true,
autoSessionTracking: false,
onFatalError: () => {},
});

app.on('ready', () => {
const mainWindow = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});

mainWindow.loadFile(path.join(__dirname, 'index.html'));
});

0 comments on commit 668c03f

Please sign in to comment.