forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilling.go
More file actions
394 lines (336 loc) · 15.3 KB
/
billing.go
File metadata and controls
394 lines (336 loc) · 15.3 KB
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
// Copyright 2021 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"
"fmt"
)
// BillingService provides access to the billing related functions
// in the GitHub API.
//
// GitHub API docs: https://docs.github.com/rest/billing
type BillingService service
// MinutesUsedBreakdown counts the actions minutes used by machine type (e.g. UBUNTU, WINDOWS, MACOS).
type MinutesUsedBreakdown = map[string]int
// PackagesBilling represents billing of GitHub Packages .
type PackagesBilling struct {
TotalGigabytesBandwidthUsed int `json:"total_gigabytes_bandwidth_used"`
TotalPaidGigabytesBandwidthUsed int `json:"total_paid_gigabytes_bandwidth_used"`
IncludedGigabytesBandwidth int `json:"included_gigabytes_bandwidth"`
}
// StorageBilling represents a GitHub Storage billing.
type StorageBilling struct {
DaysLeftInBillingCycle int `json:"days_left_in_billing_cycle"`
EstimatedPaidStorageForMonth int `json:"estimated_paid_storage_for_month"`
EstimatedStorageForMonth int `json:"estimated_storage_for_month"`
}
// ActiveCommittersListOptions specifies optional parameters to the
// BillingService.GetAdvancedSecurityActiveCommittersOrg method.
type ActiveCommittersListOptions struct {
// The security product to get GitHub Advanced Security active committers for. For standalone
// Code Scanning or Secret Protection products, this parameter is required to specify which
// product you want committer information for. For other plans this parameter cannot be used.
//
// Can be one of: "code_security", "secret_protection".
AdvancedSecurityProduct *string `url:"advanced_security_product,omitempty"`
ListOptions
}
// ActiveCommitters represents the total active committers across all repositories in an Organization.
type ActiveCommitters struct {
TotalAdvancedSecurityCommitters *int `json:"total_advanced_security_committers,omitempty"`
TotalCount *int `json:"total_count,omitempty"`
MaximumAdvancedSecurityCommitters *int `json:"maximum_advanced_security_committers,omitempty"`
PurchasedAdvancedSecurityCommitters *int `json:"purchased_advanced_security_committers,omitempty"`
Repositories []*RepositoryActiveCommitters `json:"repositories"`
}
// RepositoryActiveCommitters represents active committers on each repository.
type RepositoryActiveCommitters struct {
Name string `json:"name"`
AdvancedSecurityCommitters int `json:"advanced_security_committers"`
AdvancedSecurityCommittersBreakdown []*AdvancedSecurityCommittersBreakdown `json:"advanced_security_committers_breakdown"`
}
// AdvancedSecurityCommittersBreakdown represents the user activity breakdown for ActiveCommitters.
type AdvancedSecurityCommittersBreakdown struct {
UserLogin string `json:"user_login"`
LastPushedDate string `json:"last_pushed_date"`
LastPushedEmail string `json:"last_pushed_email"`
}
// UsageReportOptions specifies optional parameters for the enhanced billing platform usage report.
type UsageReportOptions struct {
// If specified, only return results for a single year. The value of year is an integer with four digits representing a year. For example, 2025.
// Default value is the current year.
Year *int `url:"year,omitempty"`
// If specified, only return results for a single month. The value of month is an integer between 1 and 12.
// If no year is specified the default year is used.
Month *int `url:"month,omitempty"`
// If specified, only return results for a single day. The value of day is an integer between 1 and 31.
// If no year or month is specified, the default year and month are used.
Day *int `url:"day,omitempty"`
// If specified, only return results for a single hour. The value of hour is an integer between 0 and 23.
// If no year, month, or day is specified, the default year, month, and day are used.
Hour *int `url:"hour,omitempty"`
}
// PremiumRequestUsageReportOptions specifies optional parameters
// for the enhanced billing platform premium request usage report.
type PremiumRequestUsageReportOptions struct {
// If specified, only return results for a single year.
// The value of year is an integer with four digits representing a year. For example, 2025.
// Default value is the current year.
Year *int `url:"year,omitempty"`
// If specified, only return results for a single month.
// The value of month is an integer between 1 and 12. Default value is the current month.
// If no year is specified the default year is used.
Month *int `url:"month,omitempty"`
// If specified, only return results for a single day.
// The value of day is an integer between 1 and 31.
// If no year or month is specified, the default year and month are used.
Day *int `url:"day,omitempty"`
// The user name to query usage for. The name is not case-sensitive.
User *string `url:"user,omitempty"`
// The model name to query usage for. The name is not case-sensitive.
Model *string `url:"model,omitempty"`
// The product name to query usage for. The name is not case-sensitive.
Product *string `url:"product,omitempty"`
}
// UsageItem represents a single usage item in the enhanced billing platform report.
type UsageItem struct {
Date string `json:"date"`
Product string `json:"product"`
SKU string `json:"sku"`
Quantity float64 `json:"quantity"`
UnitType string `json:"unitType"`
PricePerUnit float64 `json:"pricePerUnit"`
GrossAmount float64 `json:"grossAmount"`
DiscountAmount float64 `json:"discountAmount"`
NetAmount float64 `json:"netAmount"`
RepositoryName *string `json:"repositoryName,omitempty"`
// Organization name is only used for organization-level reports.
OrganizationName *string `json:"organizationName,omitempty"`
}
// UsageReport represents the enhanced billing platform usage report response.
type UsageReport struct {
UsageItems []*UsageItem `json:"usageItems,omitempty"`
}
// PremiumRequestUsageItem represents a single usage line item in premium request usage reports.
type PremiumRequestUsageItem struct {
Product string `json:"product"`
SKU string `json:"sku"`
Model string `json:"model"`
UnitType string `json:"unitType"`
PricePerUnit float64 `json:"pricePerUnit"`
GrossQuantity float64 `json:"grossQuantity"`
GrossAmount float64 `json:"grossAmount"`
DiscountQuantity float64 `json:"discountQuantity"`
DiscountAmount float64 `json:"discountAmount"`
NetQuantity float64 `json:"netQuantity"`
NetAmount float64 `json:"netAmount"`
}
// PremiumRequestUsageTimePeriod represents a time period for premium request usage reports.
type PremiumRequestUsageTimePeriod struct {
Year int `json:"year"`
Month *int `json:"month,omitempty"`
Day *int `json:"day,omitempty"`
}
// PremiumRequestUsageReport represents the premium request usage report response.
type PremiumRequestUsageReport struct {
TimePeriod PremiumRequestUsageTimePeriod `json:"timePeriod"`
// Organization is only set for organization-level reports.
Organization *string `json:"organization,omitempty"`
// User is only set for user-level reports.
User *string `json:"user,omitempty"`
Product *string `json:"product,omitempty"`
Model *string `json:"model,omitempty"`
UsageItems []*PremiumRequestUsageItem `json:"usageItems"`
}
// GetOrganizationPackagesBilling returns the free and paid storage used for GitHub Packages in gigabytes for an Org.
//
// This endpoint appears to have disappeared from the official GitHub v3 API documentation website.
// See https://github.com/google/go-github/issues/3894 for details.
func (s *BillingService) GetOrganizationPackagesBilling(ctx context.Context, org string) (*PackagesBilling, *Response, error) {
u := fmt.Sprintf("orgs/%v/settings/billing/packages", org)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
result := new(PackagesBilling)
resp, err := s.client.Do(ctx, req, result)
if err != nil {
return nil, resp, err
}
return result, resp, nil
}
// GetOrganizationStorageBilling returns the estimated paid and estimated total storage used for GitHub Actions
// and GitHub Packages in gigabytes for an Org.
//
// This endpoint appears to have disappeared from the official GitHub v3 API documentation website.
// See https://github.com/google/go-github/issues/3894 for details.
func (s *BillingService) GetOrganizationStorageBilling(ctx context.Context, org string) (*StorageBilling, *Response, error) {
u := fmt.Sprintf("orgs/%v/settings/billing/shared-storage", org)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
result := new(StorageBilling)
resp, err := s.client.Do(ctx, req, result)
if err != nil {
return nil, resp, err
}
return result, resp, nil
}
// GetOrganizationAdvancedSecurityActiveCommitters returns the GitHub Advanced Security active committers for an organization per repository.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/billing/billing#get-github-advanced-security-active-committers-for-an-organization
//
//meta:operation GET /orgs/{org}/settings/billing/advanced-security
func (s *BillingService) GetOrganizationAdvancedSecurityActiveCommitters(ctx context.Context, org string, opts *ActiveCommittersListOptions) (*ActiveCommitters, *Response, error) {
u := fmt.Sprintf("orgs/%v/settings/billing/advanced-security", 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
}
result := new(ActiveCommitters)
resp, err := s.client.Do(ctx, req, result)
if err != nil {
return nil, resp, err
}
return result, resp, nil
}
// GetPackagesBilling returns the free and paid storage used for GitHub Packages in gigabytes for a user.
//
// This endpoint appears to have disappeared from the official GitHub v3 API documentation website.
// See https://github.com/google/go-github/issues/3894 for details.
func (s *BillingService) GetPackagesBilling(ctx context.Context, user string) (*PackagesBilling, *Response, error) {
u := fmt.Sprintf("users/%v/settings/billing/packages", user)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
packagesUserBilling := new(PackagesBilling)
resp, err := s.client.Do(ctx, req, packagesUserBilling)
if err != nil {
return nil, resp, err
}
return packagesUserBilling, resp, nil
}
// GetStorageBilling returns the estimated paid and estimated total storage used for GitHub Actions
// and GitHub Packages in gigabytes for a user.
//
// This endpoint appears to have disappeared from the official GitHub v3 API documentation website.
// See https://github.com/google/go-github/issues/3894 for details.
func (s *BillingService) GetStorageBilling(ctx context.Context, user string) (*StorageBilling, *Response, error) {
u := fmt.Sprintf("users/%v/settings/billing/shared-storage", user)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
storageUserBilling := new(StorageBilling)
resp, err := s.client.Do(ctx, req, storageUserBilling)
if err != nil {
return nil, resp, err
}
return storageUserBilling, resp, nil
}
// GetOrganizationUsageReport returns a report of the total usage for an organization using the enhanced billing platform.
//
// Note: This endpoint is only available to organizations with access to the enhanced billing platform.
//
// GitHub API docs: https://docs.github.com/rest/billing/usage#get-billing-usage-report-for-an-organization
//
//meta:operation GET /organizations/{org}/settings/billing/usage
func (s *BillingService) GetOrganizationUsageReport(ctx context.Context, org string, opts *UsageReportOptions) (*UsageReport, *Response, error) {
u := fmt.Sprintf("organizations/%v/settings/billing/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
}
usageReport := new(UsageReport)
resp, err := s.client.Do(ctx, req, usageReport)
if err != nil {
return nil, resp, err
}
return usageReport, resp, nil
}
// GetUsageReport returns a report of the total usage for a user using the enhanced billing platform.
//
// Note: This endpoint is only available to users with access to the enhanced billing platform.
//
// GitHub API docs: https://docs.github.com/rest/billing/usage#get-billing-usage-report-for-a-user
//
//meta:operation GET /users/{username}/settings/billing/usage
func (s *BillingService) GetUsageReport(ctx context.Context, user string, opts *UsageReportOptions) (*UsageReport, *Response, error) {
u := fmt.Sprintf("users/%v/settings/billing/usage", user)
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
}
usageReport := new(UsageReport)
resp, err := s.client.Do(ctx, req, usageReport)
if err != nil {
return nil, resp, err
}
return usageReport, resp, nil
}
// GetOrganizationPremiumRequestUsageReport returns a report of the premium request
// usage for an organization using the enhanced billing platform.
//
// Note: This endpoint is only available to organizations with access to the enhanced billing platform.
//
// GitHub API docs: https://docs.github.com/rest/billing/usage#get-billing-premium-request-usage-report-for-an-organization
//
//meta:operation GET /organizations/{org}/settings/billing/premium_request/usage
func (s *BillingService) GetOrganizationPremiumRequestUsageReport(ctx context.Context, org string, opts *PremiumRequestUsageReportOptions) (*PremiumRequestUsageReport, *Response, error) {
u := fmt.Sprintf("organizations/%v/settings/billing/premium_request/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
}
premiumRequestUsageReport := new(PremiumRequestUsageReport)
resp, err := s.client.Do(ctx, req, premiumRequestUsageReport)
if err != nil {
return nil, resp, err
}
return premiumRequestUsageReport, resp, nil
}
// GetPremiumRequestUsageReport returns a report of the premium request
// usage for a user using the enhanced billing platform.
//
// Note: This endpoint is only available to users with access to the enhanced billing platform.
//
// GitHub API docs: https://docs.github.com/rest/billing/usage#get-billing-premium-request-usage-report-for-a-user
//
//meta:operation GET /users/{username}/settings/billing/premium_request/usage
func (s *BillingService) GetPremiumRequestUsageReport(ctx context.Context, user string, opts *PremiumRequestUsageReportOptions) (*PremiumRequestUsageReport, *Response, error) {
u := fmt.Sprintf("users/%v/settings/billing/premium_request/usage", user)
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
}
premiumRequestUsageReport := new(PremiumRequestUsageReport)
resp, err := s.client.Do(ctx, req, premiumRequestUsageReport)
if err != nil {
return nil, resp, err
}
return premiumRequestUsageReport, resp, nil
}