-
Notifications
You must be signed in to change notification settings - Fork 262
feat: add capture feedback API #1419
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
al1re3a
wants to merge
3
commits into
getsentry:master
Choose a base branch
from
al1re3a:feat/capture-feedback-842
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package sentry | ||
|
|
||
| // Feedback contains user-provided feedback to send to Sentry. | ||
| // | ||
| // Message is the only required field. AssociatedEventID may be set to link the | ||
| // feedback to a previously captured event. | ||
| type Feedback struct { | ||
| Message string | ||
| Name string | ||
| Email string | ||
| URL string | ||
| Source string | ||
| AssociatedEventID EventID | ||
| } | ||
|
|
||
| func (feedback *Feedback) context() Context { | ||
| context := Context{ | ||
| "message": feedback.Message, | ||
| } | ||
|
|
||
| if feedback.Name != "" { | ||
| context["name"] = feedback.Name | ||
| } | ||
| if feedback.Email != "" { | ||
| context["contact_email"] = feedback.Email | ||
| } | ||
| if feedback.URL != "" { | ||
| context["url"] = feedback.URL | ||
| } | ||
| if feedback.Source != "" { | ||
| context["source"] = feedback.Source | ||
| } | ||
| if feedback.AssociatedEventID != "" { | ||
| context["associated_event_id"] = feedback.AssociatedEventID | ||
| } | ||
|
|
||
| return context | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package sentry | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/getsentry/sentry-go/internal/protocol" | ||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| func TestCaptureFeedback(t *testing.T) { | ||
| client, _, transport := setupClientTest() | ||
| scope := NewScope() | ||
| scope.SetTag("component", "feedback-form") | ||
|
|
||
| eventID := client.CaptureFeedback(&Feedback{ | ||
| Message: "The save button does not work", | ||
| Name: "Jane Doe", | ||
| Email: "jane@example.com", | ||
| URL: "https://example.com/settings", | ||
| Source: "custom-form", | ||
| AssociatedEventID: "b81c5be4d31e48959103a1f878a1efcb", | ||
| }, nil, scope) | ||
|
|
||
| if eventID == nil { | ||
| t.Fatal("CaptureFeedback returned a nil event ID") | ||
| } | ||
|
|
||
| wantContext := Context{ | ||
| "message": "The save button does not work", | ||
| "name": "Jane Doe", | ||
| "contact_email": "jane@example.com", | ||
| "url": "https://example.com/settings", | ||
| "source": "custom-form", | ||
| "associated_event_id": EventID("b81c5be4d31e48959103a1f878a1efcb"), | ||
| } | ||
| if diff := cmp.Diff(wantContext, transport.lastEvent.Contexts[feedbackType]); diff != "" { | ||
| t.Errorf("Feedback context mismatch (-want +got):\n%s", diff) | ||
| } | ||
| if transport.lastEvent.Type != feedbackType { | ||
| t.Errorf("Event type = %q, want %q", transport.lastEvent.Type, feedbackType) | ||
| } | ||
| if transport.lastEvent.Level != LevelInfo { | ||
| t.Errorf("Event level = %q, want %q", transport.lastEvent.Level, LevelInfo) | ||
| } | ||
| if got := transport.lastEvent.Tags["component"]; got != "feedback-form" { | ||
| t.Errorf("Scope tag = %q, want %q", got, "feedback-form") | ||
| } | ||
|
|
||
| item, err := transport.lastEvent.ToEnvelopeItem() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if item.Header.Type != protocol.EnvelopeItemTypeFeedback { | ||
| t.Errorf("Envelope item type = %q, want %q", item.Header.Type, protocol.EnvelopeItemTypeFeedback) | ||
| } | ||
| var payload map[string]interface{} | ||
| if err := json.Unmarshal(item.Payload, &payload); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got := payload["type"]; got != feedbackType { | ||
| t.Errorf("Payload type = %q, want %q", got, feedbackType) | ||
| } | ||
| } | ||
|
|
||
| func TestCaptureFeedbackOptionalFieldsAreOmitted(t *testing.T) { | ||
| client, scope, transport := setupClientTest() | ||
|
|
||
| client.CaptureFeedback(&Feedback{Message: "It works"}, nil, scope) | ||
|
|
||
| want := Context{"message": "It works"} | ||
| if diff := cmp.Diff(want, transport.lastEvent.Contexts[feedbackType]); diff != "" { | ||
| t.Errorf("Feedback context mismatch (-want +got):\n%s", diff) | ||
| } | ||
| } | ||
|
|
||
| func TestCaptureFeedbackNil(t *testing.T) { | ||
| client, scope, transport := setupClientTest() | ||
|
|
||
| if eventID := client.CaptureFeedback(nil, nil, scope); eventID != nil { | ||
| t.Errorf("CaptureFeedback(nil) = %q, want nil", *eventID) | ||
| } | ||
| if transport.lastEvent != nil { | ||
| t.Error("CaptureFeedback(nil) sent an event") | ||
| } | ||
| } | ||
|
|
||
| func TestCaptureFeedbackIgnoresErrorSamplingAndBeforeSend(t *testing.T) { | ||
| client, scope, transport := setupClientTest() | ||
| client.options.SampleRate = 0 | ||
| beforeSendCalled := false | ||
| client.options.BeforeSend = func(_ *Event, _ *EventHint) *Event { | ||
| beforeSendCalled = true | ||
| return nil | ||
| } | ||
|
|
||
| eventID := client.CaptureFeedback(&Feedback{Message: "Feedback"}, nil, scope) | ||
|
|
||
| if eventID == nil { | ||
| t.Fatal("CaptureFeedback returned a nil event ID") | ||
| } | ||
| if beforeSendCalled { | ||
| t.Error("CaptureFeedback called the error BeforeSend hook") | ||
| } | ||
| if transport.lastEvent == nil { | ||
| t.Error("CaptureFeedback did not send an event") | ||
| } | ||
| } | ||
|
|
||
| func TestEventFromFeedbackMarshalJSONIncludesType(t *testing.T) { | ||
| client, _, _ := setupClientTest() | ||
| event := client.EventFromFeedback(&Feedback{Message: "Feedback"}) | ||
|
|
||
| payload, err := json.Marshal(event) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var got map[string]interface{} | ||
| if err := json.Unmarshal(payload, &got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got["type"] != feedbackType { | ||
| t.Errorf("Payload type = %q, want %q", got["type"], feedbackType) | ||
| } | ||
| } | ||
|
|
||
| func TestHubCaptureFeedbackDoesNotChangeLastEventID(t *testing.T) { | ||
| hub, _, _ := setupHubTest() | ||
| messageID := hub.CaptureMessage("before feedback") | ||
|
|
||
| feedbackID := hub.CaptureFeedback(&Feedback{Message: "Feedback"}) | ||
|
|
||
| if feedbackID == nil { | ||
| t.Fatal("CaptureFeedback returned a nil event ID") | ||
| } | ||
| if got := hub.LastEventID(); got != *messageID { | ||
| t.Errorf("LastEventID() = %q, want %q", got, *messageID) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.