Skip to content

Commit 8e37be1

Browse files
committed
Cleanup
1 parent 6a381aa commit 8e37be1

File tree

5 files changed

+27
-66
lines changed

5 files changed

+27
-66
lines changed

_integration_tests/appstoreconnect_tests/appstoreconnect_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func TestListBundleIDs(t *testing.T) {
1111
keyID, issuerID, privateKey, enterpriseAccount := getAPIKey(t)
1212

13-
client := appstoreconnect.NewClient(appstoreconnect.NewRetryableHTTPClient(), keyID, issuerID, []byte(privateKey), enterpriseAccount, appstoreconnect.StdoutTracker{})
13+
client := appstoreconnect.NewClient(appstoreconnect.NewRetryableHTTPClient(), keyID, issuerID, []byte(privateKey), enterpriseAccount, appstoreconnect.NoOpAnalyticsTracker{})
1414

1515
response, err := client.Provisioning.ListBundleIDs(&appstoreconnect.ListBundleIDsOptions{})
1616
require.NoError(t, err)

autocodesign/devportalclient/appstoreconnect/appstoreconnect.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,18 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
257257
resp, err := c.client.Do(req)
258258
duration := time.Since(startTime)
259259

260-
if err != nil {
261-
c.tracker.TrackAPIError(req.Method, req.URL.Host, req.URL.Path, 0, err.Error())
262-
return nil, err
263-
}
264-
265260
c.Debugf("Response:")
266261
if c.EnableDebugLogs {
267262
if err := httputil.PrintResponse(resp); err != nil {
268263
c.Debugf("Failed to print response: %s", err)
269264
}
270265
}
266+
267+
if err != nil {
268+
c.tracker.TrackAPIError(req.Method, req.URL.Host, req.URL.Path, 0, err.Error())
269+
return nil, err
270+
}
271+
271272
defer func() {
272273
if cerr := resp.Body.Close(); cerr != nil {
273274
log.Warnf("Failed to close response body: %s", cerr)

autocodesign/devportalclient/appstoreconnect/appstoreconnect_test.go

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func TestNewEnterpriseClient(t *testing.T) {
3636
require.Equal(t, wantURL, got.BaseURL)
3737
}
3838

39-
// Mock types for testing Client.Do analytics tracking behavior
4039
type mockAnalyticsTracker struct {
4140
apiRequests []apiRequestRecord
4241
apiErrors []apiErrorRecord
@@ -94,7 +93,7 @@ func (m *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
9493
return m.resp, m.err
9594
}
9695

97-
func TestClientDoAnalyticsTracking(t *testing.T) {
96+
func TestTracking(t *testing.T) {
9897
t.Run("successful request", func(t *testing.T) {
9998
mockTracker := &mockAnalyticsTracker{}
10099
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -133,15 +132,6 @@ func TestClientDoAnalyticsTracking(t *testing.T) {
133132

134133
t.Run("error response", func(t *testing.T) {
135134
mockTracker := &mockAnalyticsTracker{}
136-
137-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
138-
w.WriteHeader(http.StatusBadRequest)
139-
_, err := w.Write([]byte(`{"errors": [{"code": "PARAMETER_ERROR.INVALID", "title": "Invalid parameter"}]}`))
140-
require.NoError(t, err)
141-
}))
142-
defer server.Close()
143-
144-
// Use a mock HTTP client to avoid JWT token generation issues
145135
mockHTTPClient := &mockHTTPClient{
146136
resp: &http.Response{
147137
StatusCode: 400,
@@ -158,12 +148,12 @@ func TestClientDoAnalyticsTracking(t *testing.T) {
158148
req, err := http.NewRequest("POST", "https://example.com/test", nil)
159149
require.NoError(t, err)
160150
_, err = client.Do(req, nil)
161-
require.Errorf(t, err, "Expected error due to 400 Bad Request response")
151+
require.Error(t, err, "Expected error due to 400 Bad Request response")
162152

163-
require.Truef(t, mockHTTPClient.called, "Expected HTTP client to be called")
153+
require.True(t, mockHTTPClient.called, "Expected HTTP client to be called")
164154

165-
require.Lenf(t, mockTracker.apiRequests, 0, "Expected 0 (successful) API requests tracked")
166-
require.Lenf(t, mockTracker.apiErrors, 1, "Expected 1 API error tracked")
155+
require.Len(t, mockTracker.apiRequests, 0, "Expected 0 (successful) API requests tracked")
156+
require.Len(t, mockTracker.apiErrors, 1, "Expected 1 API error tracked")
167157

168158
errorRecord := mockTracker.apiErrors[0]
169159
require.Equal(t, "POST", errorRecord.method)
@@ -187,23 +177,12 @@ func TestClientDoAnalyticsTracking(t *testing.T) {
187177
_, err = client.Do(req, nil)
188178
require.Error(t, err)
189179

190-
if len(mockTracker.apiRequests) != 0 {
191-
t.Errorf("Expected 0 API requests tracked, got %d", len(mockTracker.apiRequests))
192-
}
193-
194-
if len(mockTracker.apiErrors) != 1 {
195-
t.Errorf("Expected 1 API error tracked, got %d", len(mockTracker.apiErrors))
196-
}
180+
require.Len(t, mockTracker.apiRequests, 0, "Expected 0 API requests tracked")
181+
require.Len(t, mockTracker.apiErrors, 1, "Expected 1 API error tracked")
197182

198183
record := mockTracker.apiErrors[0]
199-
if record.method != "GET" {
200-
t.Errorf("Expected method GET, got %s", record.method)
201-
}
202-
if record.statusCode != 0 {
203-
t.Errorf("Expected status code 0 for network error, got %d", record.statusCode)
204-
}
205-
if record.errorMessage != "network connection failed" {
206-
t.Errorf("Expected error message 'network connection failed', got %s", record.errorMessage)
207-
}
184+
require.Equal(t, "GET", record.method)
185+
require.Equal(t, 0, record.statusCode)
186+
require.Equal(t, "network connection failed", record.errorMessage)
208187
})
209188
}

autocodesign/devportalclient/appstoreconnect/tracker.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package appstoreconnect
22

33
import (
4-
"fmt"
54
"time"
65

76
"github.com/bitrise-io/go-utils/v2/analytics"
@@ -23,47 +22,29 @@ type Tracker interface {
2322
// NoOpAnalyticsTracker is a dummy implementation used in tests.
2423
type NoOpAnalyticsTracker struct{}
2524

26-
// TrackAPIRequest tracks API requests (no-op implementation).
25+
// TrackAPIRequest ...
2726
func (n NoOpAnalyticsTracker) TrackAPIRequest(method, host, endpoint string, statusCode int, duration time.Duration) {
2827
}
29-
// TrackAPIError tracks API errors (no-op implementation).
28+
// TrackAPIError ...
3029
func (n NoOpAnalyticsTracker) TrackAPIError(method, host, endpoint string, statusCode int, errorMessage string) {
3130
}
32-
// TrackAuthError tracks authentication errors (no-op implementation).
31+
// TrackAuthError ...
3332
func (n NoOpAnalyticsTracker) TrackAuthError(errorMessage string) {}
3433

35-
// StdoutTracker logs all analytics events to stdout for debugging and testing purposes.
36-
type StdoutTracker struct{}
37-
38-
// TrackAPIRequest logs API requests to stdout.
39-
func (s StdoutTracker) TrackAPIRequest(method, host, endpoint string, statusCode int, duration time.Duration) {
40-
fmt.Printf("[ANALYTICS] API Request: %s %s%s -> %d (%v)\n", method, host, endpoint, statusCode, duration)
41-
}
42-
43-
// TrackAPIError logs API errors to stdout.
44-
func (s StdoutTracker) TrackAPIError(method, host, endpoint string, statusCode int, errorMessage string) {
45-
fmt.Printf("[ANALYTICS] API Error: %s %s%s -> %d: %s\n", method, host, endpoint, statusCode, errorMessage)
46-
}
47-
48-
// TrackAuthError logs authentication errors to stdout.
49-
func (s StdoutTracker) TrackAuthError(errorMessage string) {
50-
fmt.Printf("[ANALYTICS] Auth Error: %s\n", errorMessage)
51-
}
52-
53-
// DefaultTracker is the default implementation of Tracker that sends analytics events.
34+
// DefaultTracker is the main implementation of Tracker
5435
type DefaultTracker struct {
5536
tracker analytics.Tracker
5637
envRepo env.Repository
5738
}
5839

59-
// NewDefaultTracker creates a new DefaultTracker instance.
40+
// NewDefaultTracker ...
6041
func NewDefaultTracker(tracker analytics.Tracker, envRepo env.Repository) *DefaultTracker {
6142
return &DefaultTracker{
6243
tracker: tracker,
6344
envRepo: envRepo,
6445
}
6546
}
66-
// TrackAPIRequest tracks a completed API request.
47+
// TrackAPIRequest ...
6748
func (d *DefaultTracker) TrackAPIRequest(method, host, endpoint string, statusCode int, duration time.Duration) {
6849
d.tracker.Enqueue("step_appstoreconnect_request", analytics.Properties{
6950
"build_slug": d.envRepo.Get("BITRISE_BUILD_SLUG"),
@@ -74,7 +55,7 @@ func (d *DefaultTracker) TrackAPIRequest(method, host, endpoint string, statusCo
7455
"duration_ms": duration.Truncate(time.Millisecond).Milliseconds(),
7556
})
7657
}
77-
// TrackAPIError tracks a failed API request with error details.
58+
// TrackAPIError ...
7859
func (d *DefaultTracker) TrackAPIError(method, host, endpoint string, statusCode int, errorMessage string) {
7960
d.tracker.Enqueue("step_appstoreconnect_error", analytics.Properties{
8061
"build_slug": d.envRepo.Get("BITRISE_BUILD_SLUG"),
@@ -85,7 +66,7 @@ func (d *DefaultTracker) TrackAPIError(method, host, endpoint string, statusCode
8566
"error_message": errorMessage,
8667
})
8768
}
88-
// TrackAuthError tracks authentication-specific errors.
69+
// TrackAuthError ...
8970
func (d *DefaultTracker) TrackAuthError(errorMessage string) {
9071
d.tracker.Enqueue("step_appstoreconnect_auth_error", analytics.Properties{
9172
"build_slug": d.envRepo.Get("BITRISE_BUILD_SLUG"),

autocodesign/devportalclient/devportalclient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/bitrise-io/go-steputils/v2/ruby"
99
"github.com/bitrise-io/go-utils/retry"
10+
"github.com/bitrise-io/go-utils/v2/analytics"
1011
"github.com/bitrise-io/go-utils/v2/command"
1112
"github.com/bitrise-io/go-utils/v2/env"
1213
"github.com/bitrise-io/go-utils/v2/fileutil"
@@ -74,8 +75,7 @@ func (f Factory) Create(credentials devportalservice.Credentials, teamID string)
7475
f.logger.Println()
7576
f.logger.Infof("Initializing Developer Portal client")
7677
var devportalClient autocodesign.DevPortalClient
77-
// tracker := appstoreconnect.NewDefaultTracker(analytics.NewDefaultTracker(f.logger), env.NewRepository())
78-
tracker := appstoreconnect.StdoutTracker{}
78+
tracker := appstoreconnect.NewDefaultTracker(analytics.NewDefaultTracker(f.logger), env.NewRepository())
7979
if credentials.APIKey != nil {
8080
httpClient := appstoreconnect.NewRetryableHTTPClient()
8181
client := appstoreconnect.NewClient(

0 commit comments

Comments
 (0)