This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopilot.go
484 lines (409 loc) · 16.5 KB
/
copilot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
)
// CopilotService provides access to the Copilot-related functions
// in the GitHub API.
//
// GitHub API docs: https://docs.github.com/en/rest/copilot/
type CopilotService service
// CopilotOrganizationDetails represents the details of an organization's Copilot for Business subscription.
type CopilotOrganizationDetails struct {
SeatBreakdown *CopilotSeatBreakdown `json:"seat_breakdown"`
PublicCodeSuggestions string `json:"public_code_suggestions"`
CopilotChat string `json:"copilot_chat"`
SeatManagementSetting string `json:"seat_management_setting"`
}
// CopilotSeatBreakdown represents the breakdown of Copilot for Business seats for the organization.
type CopilotSeatBreakdown struct {
Total int `json:"total"`
AddedThisCycle int `json:"added_this_cycle"`
PendingCancellation int `json:"pending_cancellation"`
PendingInvitation int `json:"pending_invitation"`
ActiveThisCycle int `json:"active_this_cycle"`
InactiveThisCycle int `json:"inactive_this_cycle"`
}
// ListCopilotSeatsResponse represents the Copilot for Business seat assignments for an organization.
type ListCopilotSeatsResponse struct {
TotalSeats int64 `json:"total_seats"`
Seats []*CopilotSeatDetails `json:"seats"`
}
// CopilotSeatDetails represents the details of a Copilot for Business seat.
type CopilotSeatDetails struct {
// Assignee can either be a User, Team, or Organization.
Assignee interface{} `json:"assignee"`
AssigningTeam *Team `json:"assigning_team,omitempty"`
PendingCancellationDate *string `json:"pending_cancellation_date,omitempty"`
LastActivityAt *Timestamp `json:"last_activity_at,omitempty"`
LastActivityEditor *string `json:"last_activity_editor,omitempty"`
CreatedAt *Timestamp `json:"created_at"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
PlanType *string `json:"plan_type,omitempty"`
}
// SeatAssignments represents the number of seats assigned.
type SeatAssignments struct {
SeatsCreated int `json:"seats_created"`
}
// SeatCancellations represents the number of seats cancelled.
type SeatCancellations struct {
SeatsCancelled int `json:"seats_cancelled"`
}
// CopilotUsageSummaryListOptions represents the optional parameters to the CopilotService.GetOrganizationUsage method.
type CopilotUsageSummaryListOptions struct {
Since *time.Time `url:"since,omitempty"`
Until *time.Time `url:"until,omitempty"`
ListOptions
}
// CopilotUsageBreakdown represents the breakdown of Copilot usage for a specific language and editor.
type CopilotUsageBreakdown struct {
Language string `json:"language"`
Editor string `json:"editor"`
SuggestionsCount int64 `json:"suggestions_count"`
AcceptancesCount int64 `json:"acceptances_count"`
LinesSuggested int64 `json:"lines_suggested"`
LinesAccepted int64 `json:"lines_accepted"`
ActiveUsers int `json:"active_users"`
}
// CopilotUsageSummary represents the daily breakdown of aggregated usage metrics for Copilot completions and Copilot Chat in the IDE across an organization.
type CopilotUsageSummary struct {
Day string `json:"day"`
TotalSuggestionsCount int64 `json:"total_suggestions_count"`
TotalAcceptancesCount int64 `json:"total_acceptances_count"`
TotalLinesSuggested int64 `json:"total_lines_suggested"`
TotalLinesAccepted int64 `json:"total_lines_accepted"`
TotalActiveUsers int64 `json:"total_active_users"`
TotalChatAcceptances int64 `json:"total_chat_acceptances"`
TotalChatTurns int64 `json:"total_chat_turns"`
TotalActiveChatUsers int `json:"total_active_chat_users"`
Breakdown []*CopilotUsageBreakdown `json:"breakdown"`
}
func (cp *CopilotSeatDetails) UnmarshalJSON(data []byte) error {
// Using an alias to avoid infinite recursion when calling json.Unmarshal
type alias CopilotSeatDetails
var seatDetail alias
if err := json.Unmarshal(data, &seatDetail); err != nil {
return err
}
cp.AssigningTeam = seatDetail.AssigningTeam
cp.PendingCancellationDate = seatDetail.PendingCancellationDate
cp.LastActivityAt = seatDetail.LastActivityAt
cp.LastActivityEditor = seatDetail.LastActivityEditor
cp.CreatedAt = seatDetail.CreatedAt
cp.UpdatedAt = seatDetail.UpdatedAt
cp.PlanType = seatDetail.PlanType
switch v := seatDetail.Assignee.(type) {
case map[string]interface{}:
jsonData, err := json.Marshal(seatDetail.Assignee)
if err != nil {
return err
}
if v["type"] == nil {
return errors.New("assignee type field is not set")
}
if t, ok := v["type"].(string); ok && t == "User" {
user := &User{}
if err := json.Unmarshal(jsonData, user); err != nil {
return err
}
cp.Assignee = user
} else if t, ok := v["type"].(string); ok && t == "Team" {
team := &Team{}
if err := json.Unmarshal(jsonData, team); err != nil {
return err
}
cp.Assignee = team
} else if t, ok := v["type"].(string); ok && t == "Organization" {
organization := &Organization{}
if err := json.Unmarshal(jsonData, organization); err != nil {
return err
}
cp.Assignee = organization
} else {
return fmt.Errorf("unsupported assignee type %v", v["type"])
}
default:
return fmt.Errorf("unsupported assignee type %T", v)
}
return nil
}
// GetUser gets the User from the CopilotSeatDetails if the assignee is a user.
func (cp *CopilotSeatDetails) GetUser() (*User, bool) { u, ok := cp.Assignee.(*User); return u, ok }
// GetTeam gets the Team from the CopilotSeatDetails if the assignee is a team.
func (cp *CopilotSeatDetails) GetTeam() (*Team, bool) { t, ok := cp.Assignee.(*Team); return t, ok }
// GetOrganization gets the Organization from the CopilotSeatDetails if the assignee is an organization.
func (cp *CopilotSeatDetails) GetOrganization() (*Organization, bool) {
o, ok := cp.Assignee.(*Organization)
return o, ok
}
// GetCopilotBilling gets Copilot for Business billing information and settings for an organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management#get-copilot-seat-information-and-settings-for-an-organization
//
//meta:operation GET /orgs/{org}/copilot/billing
func (s *CopilotService) GetCopilotBilling(ctx context.Context, org string) (*CopilotOrganizationDetails, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/billing", org)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var copilotDetails *CopilotOrganizationDetails
resp, err := s.client.Do(ctx, req, &copilotDetails)
if err != nil {
return nil, resp, err
}
return copilotDetails, resp, nil
}
// ListCopilotSeats lists Copilot for Business seat assignments for an organization.
//
// To paginate through all seats, populate 'Page' with the number of the last page.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management#list-all-copilot-seat-assignments-for-an-organization
//
//meta:operation GET /orgs/{org}/copilot/billing/seats
func (s *CopilotService) ListCopilotSeats(ctx context.Context, org string, opts *ListOptions) (*ListCopilotSeatsResponse, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/billing/seats", org)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var copilotSeats *ListCopilotSeatsResponse
resp, err := s.client.Do(ctx, req, &copilotSeats)
if err != nil {
return nil, resp, err
}
return copilotSeats, resp, nil
}
// ListCopilotEnterpriseSeats lists Copilot for Business seat assignments for an enterprise.
//
// To paginate through all seats, populate 'Page' with the number of the last page.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management#list-all-copilot-seat-assignments-for-an-enterprise
//
//meta:operation GET /enterprises/{enterprise}/copilot/billing/seats
func (s *CopilotService) ListCopilotEnterpriseSeats(ctx context.Context, enterprise string, opts *ListOptions) (*ListCopilotSeatsResponse, *Response, error) {
u := fmt.Sprintf("enterprises/%v/copilot/billing/seats", enterprise)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var copilotSeats *ListCopilotSeatsResponse
resp, err := s.client.Do(ctx, req, &copilotSeats)
if err != nil {
return nil, resp, err
}
return copilotSeats, resp, nil
}
// AddCopilotTeams adds teams to the Copilot for Business subscription for an organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management#add-teams-to-the-copilot-subscription-for-an-organization
//
//meta:operation POST /orgs/{org}/copilot/billing/selected_teams
func (s *CopilotService) AddCopilotTeams(ctx context.Context, org string, teamNames []string) (*SeatAssignments, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/billing/selected_teams", org)
body := struct {
SelectedTeams []string `json:"selected_teams"`
}{
SelectedTeams: teamNames,
}
req, err := s.client.NewRequest("POST", u, body)
if err != nil {
return nil, nil, err
}
var seatAssignments *SeatAssignments
resp, err := s.client.Do(ctx, req, &seatAssignments)
if err != nil {
return nil, resp, err
}
return seatAssignments, resp, nil
}
// RemoveCopilotTeams removes teams from the Copilot for Business subscription for an organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management#remove-teams-from-the-copilot-subscription-for-an-organization
//
//meta:operation DELETE /orgs/{org}/copilot/billing/selected_teams
func (s *CopilotService) RemoveCopilotTeams(ctx context.Context, org string, teamNames []string) (*SeatCancellations, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/billing/selected_teams", org)
body := struct {
SelectedTeams []string `json:"selected_teams"`
}{
SelectedTeams: teamNames,
}
req, err := s.client.NewRequest("DELETE", u, body)
if err != nil {
return nil, nil, err
}
var seatCancellations *SeatCancellations
resp, err := s.client.Do(ctx, req, &seatCancellations)
if err != nil {
return nil, resp, err
}
return seatCancellations, resp, nil
}
// AddCopilotUsers adds users to the Copilot for Business subscription for an organization
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management#add-users-to-the-copilot-subscription-for-an-organization
//
//meta:operation POST /orgs/{org}/copilot/billing/selected_users
func (s *CopilotService) AddCopilotUsers(ctx context.Context, org string, users []string) (*SeatAssignments, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/billing/selected_users", org)
body := struct {
SelectedUsernames []string `json:"selected_usernames"`
}{
SelectedUsernames: users,
}
req, err := s.client.NewRequest("POST", u, body)
if err != nil {
return nil, nil, err
}
var seatAssignments *SeatAssignments
resp, err := s.client.Do(ctx, req, &seatAssignments)
if err != nil {
return nil, resp, err
}
return seatAssignments, resp, nil
}
// RemoveCopilotUsers removes users from the Copilot for Business subscription for an organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management#remove-users-from-the-copilot-subscription-for-an-organization
//
//meta:operation DELETE /orgs/{org}/copilot/billing/selected_users
func (s *CopilotService) RemoveCopilotUsers(ctx context.Context, org string, users []string) (*SeatCancellations, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/billing/selected_users", org)
body := struct {
SelectedUsernames []string `json:"selected_usernames"`
}{
SelectedUsernames: users,
}
req, err := s.client.NewRequest("DELETE", u, body)
if err != nil {
return nil, nil, err
}
var seatCancellations *SeatCancellations
resp, err := s.client.Do(ctx, req, &seatCancellations)
if err != nil {
return nil, resp, err
}
return seatCancellations, resp, nil
}
// GetSeatDetails gets Copilot for Business seat assignment details for a user.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management#get-copilot-seat-assignment-details-for-a-user
//
//meta:operation GET /orgs/{org}/members/{username}/copilot
func (s *CopilotService) GetSeatDetails(ctx context.Context, org, user string) (*CopilotSeatDetails, *Response, error) {
u := fmt.Sprintf("orgs/%v/members/%v/copilot", org, user)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var seatDetails *CopilotSeatDetails
resp, err := s.client.Do(ctx, req, &seatDetails)
if err != nil {
return nil, resp, err
}
return seatDetails, resp, nil
}
// GetOrganizationUsage gets daily breakdown of aggregated usage metrics for Copilot completions and Copilot Chat in the IDE across an organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-usage#get-a-summary-of-copilot-usage-for-organization-members
//
//meta:operation GET /orgs/{org}/copilot/usage
func (s *CopilotService) GetOrganizationUsage(ctx context.Context, org string, opts *CopilotUsageSummaryListOptions) ([]*CopilotUsageSummary, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/usage", org)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var usage []*CopilotUsageSummary
resp, err := s.client.Do(ctx, req, &usage)
if err != nil {
return nil, resp, err
}
return usage, resp, nil
}
// GetEnterpriseUsage gets daily breakdown of aggregated usage metrics for Copilot completions and Copilot Chat in the IDE across an enterprise.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-usage#get-a-summary-of-copilot-usage-for-enterprise-members
//
//meta:operation GET /enterprises/{enterprise}/copilot/usage
func (s *CopilotService) GetEnterpriseUsage(ctx context.Context, enterprise string, opts *CopilotUsageSummaryListOptions) ([]*CopilotUsageSummary, *Response, error) {
u := fmt.Sprintf("enterprises/%v/copilot/usage", enterprise)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var usage []*CopilotUsageSummary
resp, err := s.client.Do(ctx, req, &usage)
if err != nil {
return nil, resp, err
}
return usage, resp, nil
}
// GetEnterpriseTeamUsage gets daily breakdown of aggregated usage metrics for Copilot completions and Copilot Chat in the IDE for a team in the enterprise.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-usage#get-a-summary-of-copilot-usage-for-an-enterprise-team
//
//meta:operation GET /enterprises/{enterprise}/team/{team_slug}/copilot/usage
func (s *CopilotService) GetEnterpriseTeamUsage(ctx context.Context, enterprise, team string, opts *CopilotUsageSummaryListOptions) ([]*CopilotUsageSummary, *Response, error) {
u := fmt.Sprintf("enterprises/%v/team/%v/copilot/usage", enterprise, team)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var usage []*CopilotUsageSummary
resp, err := s.client.Do(ctx, req, &usage)
if err != nil {
return nil, resp, err
}
return usage, resp, nil
}
// GetOrganizationTeamUsage gets daily breakdown of aggregated usage metrics for Copilot completions and Copilot Chat in the IDE for a team in the organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-usage#get-a-summary-of-copilot-usage-for-a-team
//
//meta:operation GET /orgs/{org}/team/{team_slug}/copilot/usage
func (s *CopilotService) GetOrganizationTeamUsage(ctx context.Context, org, team string, opts *CopilotUsageSummaryListOptions) ([]*CopilotUsageSummary, *Response, error) {
u := fmt.Sprintf("orgs/%v/team/%v/copilot/usage", org, team)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var usage []*CopilotUsageSummary
resp, err := s.client.Do(ctx, req, &usage)
if err != nil {
return nil, resp, err
}
return usage, resp, nil
}