Skip to content

Commit adaa008

Browse files
authored
fix: throwing on parsing for a 204 response (#8825)
1 parent 4973be6 commit adaa008

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

backend/plugins/gh-copilot/tasks/enterprise_metrics_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func CollectEnterpriseMetrics(taskCtx plugin.SubTaskContext) errors.Error {
9393
},
9494
Incremental: true,
9595
Concurrency: 1,
96-
AfterResponse: ignore404,
96+
AfterResponse: ignoreNoContent,
9797
ResponseParser: func(res *http.Response) ([]json.RawMessage, errors.Error) {
9898
body, readErr := io.ReadAll(res.Body)
9999
res.Body.Close()

backend/plugins/gh-copilot/tasks/org_metrics_collector.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func CollectOrgMetrics(taskCtx plugin.SubTaskContext) errors.Error {
8787
},
8888
Incremental: true,
8989
Concurrency: 1,
90-
AfterResponse: ignore404,
90+
AfterResponse: ignoreNoContent,
9191
ResponseParser: func(res *http.Response) ([]json.RawMessage, errors.Error) {
9292
body, readErr := io.ReadAll(res.Body)
9393
res.Body.Close()
@@ -100,9 +100,19 @@ func CollectOrgMetrics(taskCtx plugin.SubTaskContext) errors.Error {
100100

101101
var meta reportMetadataResponse
102102
if jsonErr := json.Unmarshal(body, &meta); jsonErr != nil {
103+
snippet := string(body)
104+
if len(snippet) > 200 {
105+
snippet = snippet[:200]
106+
}
107+
logger.Error(jsonErr, "failed to parse report metadata, body=%s", snippet)
103108
return nil, errors.Default.Wrap(jsonErr, "failed to parse report metadata")
104109
}
105110

111+
if len(meta.DownloadLinks) == 0 {
112+
logger.Info("No download links for report day=%s, skipping", meta.ReportDay)
113+
return nil, nil
114+
}
115+
106116
var results []json.RawMessage
107117
for _, link := range meta.DownloadLinks {
108118
reportBody, dlErr := downloadReport(link, logger)

backend/plugins/gh-copilot/tasks/report_download_helper.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ func utcDate(t time.Time) time.Time {
5050
return time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
5151
}
5252

53-
// ignore404 is an AfterResponse callback that skips 404 responses.
53+
// ignoreNoContent is an AfterResponse callback that skips 404 and 204 responses.
5454
// The report API returns 404 when no report is available for a given day,
55-
// which is normal and should not be treated as an error.
56-
func ignore404(res *http.Response) errors.Error {
57-
if res.StatusCode == http.StatusNotFound {
55+
// and 204 (No Content) when data is not yet available or the org had fewer
56+
// than 5 active Copilot users on that day. Both are normal and should not
57+
// be treated as errors.
58+
func ignoreNoContent(res *http.Response) errors.Error {
59+
if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusNoContent {
5860
return helper.ErrIgnoreAndContinue
5961
}
6062
return nil

backend/plugins/gh-copilot/tasks/user_metrics_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func CollectUserMetrics(taskCtx plugin.SubTaskContext) errors.Error {
9292
},
9393
Incremental: true,
9494
Concurrency: 1,
95-
AfterResponse: ignore404,
95+
AfterResponse: ignoreNoContent,
9696
ResponseParser: func(res *http.Response) ([]json.RawMessage, errors.Error) {
9797
body, readErr := io.ReadAll(res.Body)
9898
res.Body.Close()

0 commit comments

Comments
 (0)