Skip to content

Commit 6282c33

Browse files
feat: add DownloadCopilotMetrics helper method and commented legacy metrics endpoints
1 parent 7d06267 commit 6282c33

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

github/copilot.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111
"errors"
1212
"fmt"
13+
"net/http"
1314
"time"
1415
)
1516

@@ -491,6 +492,10 @@ func (s *CopilotService) GetSeatDetails(ctx context.Context, org, user string) (
491492

492493
// GetEnterpriseMetrics gets Copilot usage metrics for an enterprise.
493494
//
495+
// Deprecated: Focus has shifted to the new Download Copilot Usage Metrics APIs.
496+
// These endpoints are kept around for GitHub Enterprise Server users, but are no longer supported on the Public GitHub service.
497+
// Use GetEnterpriseDailyMetricsReport or GetEnterpriseMetricsReport instead.
498+
//
494499
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/copilot/copilot-metrics#get-copilot-metrics-for-an-enterprise
495500
//
496501
//meta:operation GET /enterprises/{enterprise}/copilot/metrics
@@ -517,6 +522,9 @@ func (s *CopilotService) GetEnterpriseMetrics(ctx context.Context, enterprise st
517522

518523
// GetEnterpriseTeamMetrics gets Copilot usage metrics for an enterprise team.
519524
//
525+
// Deprecated: Focus has shifted to the new Download Copilot Usage Metrics APIs.
526+
// These endpoints are kept around for GitHub Enterprise Server users, but are no longer supported on the Public GitHub service.
527+
//
520528
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/copilot/copilot-metrics#get-copilot-metrics-for-an-enterprise-team
521529
//
522530
//meta:operation GET /enterprises/{enterprise}/team/{team_slug}/copilot/metrics
@@ -543,6 +551,10 @@ func (s *CopilotService) GetEnterpriseTeamMetrics(ctx context.Context, enterpris
543551

544552
// GetOrganizationMetrics gets Copilot usage metrics for an organization.
545553
//
554+
// Deprecated: Focus has shifted to the new Download Copilot Usage Metrics APIs.
555+
// These endpoints are kept around for GitHub Enterprise Server users, but are no longer supported on the Public GitHub service.
556+
// Use GetOrganizationDailyMetricsReport or GetOrganizationMetricsReport instead.
557+
//
546558
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-metrics#get-copilot-metrics-for-an-organization
547559
//
548560
//meta:operation GET /orgs/{org}/copilot/metrics
@@ -569,6 +581,9 @@ func (s *CopilotService) GetOrganizationMetrics(ctx context.Context, org string,
569581

570582
// GetOrganizationTeamMetrics gets Copilot usage metrics for an organization team.
571583
//
584+
// Deprecated: Focus has shifted to the new Download Copilot Usage Metrics APIs.
585+
// These endpoints are kept around for GitHub Enterprise Server users, but are no longer supported on the Public GitHub service.
586+
//
572587
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-metrics#get-copilot-metrics-for-a-team
573588
//
574589
//meta:operation GET /orgs/{org}/team/{team_slug}/copilot/metrics
@@ -784,3 +799,32 @@ func (s *CopilotService) GetOrganizationUsersMetricsReport(ctx context.Context,
784799

785800
return report, resp, nil
786801
}
802+
803+
// DownloadCopilotMetrics downloads a Copilot metrics report from the provided download link
804+
// and returns the metric data. This can be used to download metrics from a link returned by
805+
// GetEnterpriseDailyMetricsReport, GetEnterpriseMetricsReport, GetEnterpriseUsersDailyMetricsReport,
806+
// GetEnterpriseUsersMetricsReport, GetOrganizationDailyMetricsReport, GetOrganizationMetricsReport,
807+
// GetOrganizationUsersDailyMetricsReport, GetOrganizationUsersMetricsReport.
808+
func (s *CopilotService) DownloadCopilotMetrics(ctx context.Context, url string) ([]*CopilotMetrics, *Response, error) {
809+
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
810+
if err != nil {
811+
return nil, nil, err
812+
}
813+
814+
resp, err := s.client.client.Do(req)
815+
if err != nil {
816+
return nil, nil, err
817+
}
818+
defer resp.Body.Close()
819+
820+
if err := CheckResponse(resp); err != nil {
821+
return nil, newResponse(resp), err
822+
}
823+
824+
var metrics []*CopilotMetrics
825+
if err := json.NewDecoder(resp.Body).Decode(&metrics); err != nil {
826+
return nil, newResponse(resp), err
827+
}
828+
829+
return metrics, newResponse(resp), nil
830+
}

github/copilot_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,3 +2778,113 @@ func TestCopilotService_GetOrganizationUsersMetricsReport(t *testing.T) {
27782778
return resp, err
27792779
})
27802780
}
2781+
2782+
func TestCopilotService_DownloadCopilotMetrics(t *testing.T) {
2783+
t.Parallel()
2784+
client, mux, _ := setup(t)
2785+
2786+
mux.HandleFunc("/path/to/download", func(w http.ResponseWriter, r *http.Request) {
2787+
testMethod(t, r, "GET")
2788+
fmt.Fprint(w, `[{
2789+
"date": "2023-01-01",
2790+
"total_active_users": 100,
2791+
"total_engaged_users": 50,
2792+
"copilot_ide_code_completions": {
2793+
"total_engaged_users": 50,
2794+
"languages": [
2795+
{
2796+
"name": "go",
2797+
"total_engaged_users": 10
2798+
}
2799+
],
2800+
"editors": [
2801+
{
2802+
"name": "vscode",
2803+
"total_engaged_users": 10,
2804+
"models": [
2805+
{
2806+
"name": "model1",
2807+
"is_custom_model": false,
2808+
"custom_model_training_date": null,
2809+
"total_engaged_users": 10,
2810+
"languages": [
2811+
{
2812+
"name": "go",
2813+
"total_engaged_users": 10,
2814+
"total_code_suggestions": 100,
2815+
"total_code_acceptances": 50,
2816+
"total_code_lines_suggested": 1000,
2817+
"total_code_lines_accepted": 500
2818+
}
2819+
]
2820+
}
2821+
]
2822+
}
2823+
]
2824+
}
2825+
}]`)
2826+
})
2827+
2828+
ctx := t.Context()
2829+
url := client.BaseURL.String() + "path/to/download"
2830+
got, _, err := client.Copilot.DownloadCopilotMetrics(ctx, url)
2831+
if err != nil {
2832+
t.Errorf("Copilot.DownloadCopilotMetrics returned error: %v", err)
2833+
}
2834+
2835+
want := []*CopilotMetrics{
2836+
{
2837+
Date: "2023-01-01",
2838+
TotalActiveUsers: Ptr(100),
2839+
TotalEngagedUsers: Ptr(50),
2840+
CopilotIDECodeCompletions: &CopilotIDECodeCompletions{
2841+
TotalEngagedUsers: 50,
2842+
Languages: []*CopilotIDECodeCompletionsLanguage{
2843+
{
2844+
Name: "go",
2845+
TotalEngagedUsers: 10,
2846+
},
2847+
},
2848+
Editors: []*CopilotIDECodeCompletionsEditor{
2849+
{
2850+
Name: "vscode",
2851+
TotalEngagedUsers: 10,
2852+
Models: []*CopilotIDECodeCompletionsModel{
2853+
{
2854+
Name: "model1",
2855+
IsCustomModel: false,
2856+
TotalEngagedUsers: 10,
2857+
Languages: []*CopilotIDECodeCompletionsModelLanguage{
2858+
{
2859+
Name: "go",
2860+
TotalEngagedUsers: 10,
2861+
TotalCodeSuggestions: 100,
2862+
TotalCodeAcceptances: 50,
2863+
TotalCodeLinesSuggested: 1000,
2864+
TotalCodeLinesAccepted: 500,
2865+
},
2866+
},
2867+
},
2868+
},
2869+
},
2870+
},
2871+
},
2872+
},
2873+
}
2874+
2875+
if !cmp.Equal(got, want) {
2876+
t.Errorf("Copilot.DownloadCopilotMetrics returned %+v, want %+v", got, want)
2877+
}
2878+
2879+
// Test unexpected status code
2880+
mux.HandleFunc("/path/to/download/error", func(w http.ResponseWriter, r *http.Request) {
2881+
testMethod(t, r, "GET")
2882+
w.WriteHeader(http.StatusNotFound)
2883+
})
2884+
2885+
urlErr := client.BaseURL.String() + "path/to/download/error"
2886+
_, _, err = client.Copilot.DownloadCopilotMetrics(ctx, urlErr)
2887+
if err == nil {
2888+
t.Errorf("Copilot.DownloadCopilotMetrics expected error but got none")
2889+
}
2890+
}

0 commit comments

Comments
 (0)