-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add X-CLI-Event header for analytics tracking #15
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
Draft
pjcdawkins
wants to merge
1
commit into
main
Choose a base branch
from
feat/analytics-event-header
base: main
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.
Draft
Changes from all 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
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,70 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // eventCtxKey is the context key for storing the event name. | ||
| type eventCtxKey struct{} | ||
|
|
||
| // interactiveCtxKey is the context key for storing the interactive mode flag. | ||
| type interactiveCtxKey struct{} | ||
|
|
||
| // WithEventName returns a new context that carries the provided event name. | ||
| func WithEventName(ctx context.Context, name string) context.Context { | ||
| return context.WithValue(ctx, eventCtxKey{}, name) | ||
| } | ||
|
|
||
| // EventNameFromContext retrieves an event name previously stored with WithEventName. | ||
| // It returns an empty string if none is set. | ||
| func EventNameFromContext(ctx context.Context) string { | ||
| v, _ := ctx.Value(eventCtxKey{}).(string) | ||
| return v | ||
| } | ||
|
|
||
| // WithInteractive returns a new context that carries the interactive mode flag. | ||
| func WithInteractive(ctx context.Context, interactive bool) context.Context { | ||
| return context.WithValue(ctx, interactiveCtxKey{}, interactive) | ||
| } | ||
|
|
||
| // InteractiveFromContext retrieves the interactive flag previously stored with WithInteractive. | ||
| // It returns true (the default) if none is set. | ||
| func InteractiveFromContext(ctx context.Context) bool { | ||
| v, ok := ctx.Value(interactiveCtxKey{}).(bool) | ||
| if !ok { | ||
| return true // default to interactive | ||
| } | ||
| return v | ||
| } | ||
|
|
||
| // EventTransport wraps an http.RoundTripper to add event tracking headers. | ||
| type EventTransport struct { | ||
| // Base is the underlying RoundTripper to use for requests. | ||
| Base http.RoundTripper | ||
|
|
||
| // EventName is the command name for the X-CLI-Event header. | ||
| // If empty, no header is added. | ||
| EventName string | ||
|
|
||
| // Interactive indicates whether the CLI is running in interactive mode. | ||
| Interactive bool | ||
|
|
||
| // UserAgent is the User-Agent string to send. | ||
| // If empty, or a User-Agent is already set on the request, no header is added. | ||
| UserAgent string | ||
| } | ||
|
|
||
| // RoundTrip adds the X-CLI-Event and User-Agent headers to the request. | ||
| func (t *EventTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| if t.EventName != "" { | ||
| // Format: command=<name>; interactive=<bool> | ||
| headerValue := fmt.Sprintf("command=%s; interactive=%t", t.EventName, t.Interactive) | ||
| req.Header.Set("X-CLI-Event", headerValue) | ||
| } | ||
| if t.UserAgent != "" && req.Header.Get("User-Agent") == "" { | ||
| req.Header.Set("User-Agent", t.UserAgent) | ||
| } | ||
| return t.Base.RoundTrip(req) | ||
| } |
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,178 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestEventTransport_RoundTrip(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| eventName string | ||
| interactive bool | ||
| userAgent string | ||
| existingUserAgent string | ||
| wantEventHeader string | ||
| wantUserAgent string | ||
| }{ | ||
| { | ||
| name: "sets headers with interactive true", | ||
| eventName: "backup:restore", | ||
| interactive: true, | ||
| userAgent: "Upsun-CLI/1.0.0", | ||
| wantEventHeader: "command=backup:restore; interactive=true", | ||
| wantUserAgent: "Upsun-CLI/1.0.0", | ||
| }, | ||
| { | ||
| name: "sets headers with interactive false", | ||
| eventName: "backup:restore", | ||
| interactive: false, | ||
| userAgent: "Upsun-CLI/1.0.0", | ||
| wantEventHeader: "command=backup:restore; interactive=false", | ||
| wantUserAgent: "Upsun-CLI/1.0.0", | ||
| }, | ||
| { | ||
| name: "sets only event header when user agent is empty", | ||
| eventName: "project:info", | ||
| interactive: true, | ||
| userAgent: "", | ||
| wantEventHeader: "command=project:info; interactive=true", | ||
| wantUserAgent: "Go-http-client/1.1", // Go's default User-Agent | ||
| }, | ||
| { | ||
| name: "sets only user agent when event name is empty", | ||
| eventName: "", | ||
| interactive: true, | ||
| userAgent: "Upsun-CLI/1.0.0", | ||
| wantEventHeader: "", | ||
| wantUserAgent: "Upsun-CLI/1.0.0", | ||
| }, | ||
| { | ||
| name: "does not set event header when event name is empty", | ||
| eventName: "", | ||
| interactive: false, | ||
| userAgent: "", | ||
| wantEventHeader: "", | ||
| wantUserAgent: "Go-http-client/1.1", // Go's default User-Agent | ||
| }, | ||
| { | ||
| name: "does not override existing user agent", | ||
| eventName: "init", | ||
| interactive: true, | ||
| userAgent: "Upsun-CLI/1.0.0", | ||
| existingUserAgent: "Custom-Agent/2.0", | ||
| wantEventHeader: "command=init; interactive=true", | ||
| wantUserAgent: "Custom-Agent/2.0", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var receivedEventHeader, receivedUserAgent string | ||
|
|
||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| receivedEventHeader = r.Header.Get("X-CLI-Event") | ||
| receivedUserAgent = r.Header.Get("User-Agent") | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| transport := &EventTransport{ | ||
| Base: http.DefaultTransport, | ||
| EventName: tc.eventName, | ||
| Interactive: tc.interactive, | ||
| UserAgent: tc.userAgent, | ||
| } | ||
|
|
||
| client := &http.Client{Transport: transport} | ||
|
|
||
| req, err := http.NewRequest(http.MethodGet, server.URL, http.NoBody) | ||
| require.NoError(t, err) | ||
|
|
||
| if tc.existingUserAgent != "" { | ||
| req.Header.Set("User-Agent", tc.existingUserAgent) | ||
| } | ||
|
|
||
| resp, err := client.Do(req) | ||
| require.NoError(t, err) | ||
| defer resp.Body.Close() | ||
|
|
||
| assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
| assert.Equal(t, tc.wantEventHeader, receivedEventHeader) | ||
| assert.Equal(t, tc.wantUserAgent, receivedUserAgent) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestWithEventName(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| eventName string | ||
| }{ | ||
| { | ||
| name: "stores and retrieves event name", | ||
| eventName: "backup:restore", | ||
| }, | ||
| { | ||
| name: "handles empty event name", | ||
| eventName: "", | ||
| }, | ||
| { | ||
| name: "handles command with namespace", | ||
| eventName: "project:info", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx = WithEventName(ctx, tc.eventName) | ||
|
|
||
| got := EventNameFromContext(ctx) | ||
| assert.Equal(t, tc.eventName, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestEventNameFromContext_EmptyContext(t *testing.T) { | ||
| ctx := context.Background() | ||
| got := EventNameFromContext(ctx) | ||
| assert.Equal(t, "", got) | ||
| } | ||
|
|
||
| func TestWithInteractive(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| interactive bool | ||
| }{ | ||
| { | ||
| name: "stores interactive true", | ||
| interactive: true, | ||
| }, | ||
| { | ||
| name: "stores interactive false", | ||
| interactive: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx = WithInteractive(ctx, tc.interactive) | ||
|
|
||
| got := InteractiveFromContext(ctx) | ||
| assert.Equal(t, tc.interactive, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestInteractiveFromContext_EmptyContext(t *testing.T) { | ||
| ctx := context.Background() | ||
| got := InteractiveFromContext(ctx) | ||
| assert.True(t, got, "default should be interactive=true") | ||
| } |
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.
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.
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.
we already add User-Agent elsewhere so is this right?