| 1 |
// Copyright 2021 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
) |
| 12 |
|
| 13 |
// BillingService provides access to the billing related functions |
| 14 |
// in the GitHub API. |
| 15 |
// |
| 16 |
// GitHub API docs: https://docs.github.com/rest/billing |
| 17 |
type BillingService service |
| 18 |
|
| 19 |
// ActionBilling represents a GitHub Action billing. |
| 20 |
type ActionBilling struct { |
| 21 |
TotalMinutesUsed float64 `json:"total_minutes_used"` |
| 22 |
TotalPaidMinutesUsed float64 `json:"total_paid_minutes_used"` |
| 23 |
IncludedMinutes float64 `json:"included_minutes"` |
| 24 |
MinutesUsedBreakdown MinutesUsedBreakdown `json:"minutes_used_breakdown"` |
| 25 |
} |
| 26 |
|
| 27 |
// MinutesUsedBreakdown counts the actions minutes used by machine type (e.g. UBUNTU, WINDOWS, MACOS). |
| 28 |
type MinutesUsedBreakdown = map[string]int |
| 29 |
|
| 30 |
// PackageBilling represents a GitHub Package billing. |
| 31 |
type PackageBilling struct { |
| 32 |
TotalGigabytesBandwidthUsed int `json:"total_gigabytes_bandwidth_used"` |
| 33 |
TotalPaidGigabytesBandwidthUsed int `json:"total_paid_gigabytes_bandwidth_used"` |
| 34 |
IncludedGigabytesBandwidth float64 `json:"included_gigabytes_bandwidth"` |
| 35 |
} |
| 36 |
|
| 37 |
// StorageBilling represents a GitHub Storage billing. |
| 38 |
type StorageBilling struct { |
| 39 |
DaysLeftInBillingCycle int `json:"days_left_in_billing_cycle"` |
| 40 |
EstimatedPaidStorageForMonth float64 `json:"estimated_paid_storage_for_month"` |
| 41 |
EstimatedStorageForMonth float64 `json:"estimated_storage_for_month"` |
| 42 |
} |
| 43 |
|
| 44 |
// ActiveCommitters represents the total active committers across all repositories in an Organization. |
| 45 |
type ActiveCommitters struct { |
| 46 |
TotalAdvancedSecurityCommitters int `json:"total_advanced_security_committers"` |
| 47 |
Repositories []*RepositoryActiveCommitters `json:"repositories,omitempty"` |
| 48 |
} |
| 49 |
|
| 50 |
// RepositoryActiveCommitters represents active committers on each repository. |
| 51 |
type RepositoryActiveCommitters struct { |
| 52 |
Name *string `json:"name,omitempty"` |
| 53 |
AdvancedSecurityCommitters *int `json:"advanced_security_committers,omitempty"` |
| 54 |
AdvancedSecurityCommittersBreakdown []*AdvancedSecurityCommittersBreakdown `json:"advanced_security_committers_breakdown,omitempty"` |
| 55 |
} |
| 56 |
|
| 57 |
// AdvancedSecurityCommittersBreakdown represents the user activity breakdown for ActiveCommitters. |
| 58 |
type AdvancedSecurityCommittersBreakdown struct { |
| 59 |
UserLogin *string `json:"user_login,omitempty"` |
| 60 |
LastPushedDate *string `json:"last_pushed_date,omitempty"` |
| 61 |
} |
| 62 |
|
| 63 |
// GetActionsBillingOrg returns the summary of the free and paid GitHub Actions minutes used for an Org. |
| 64 |
// |
| 65 |
// GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-actions-billing-for-an-organization |
| 66 |
// |
| 67 |
//meta:operation GET /orgs/{org}/settings/billing/actions |
| 68 |
func (s *BillingService) GetActionsBillingOrg(ctx context.Context, org string) (*ActionBilling, *Response, error) { |
| 69 |
u := fmt.Sprintf("orgs/%v/settings/billing/actions", org) |
| 70 |
req, err := s.client.NewRequest("GET", u, nil) |
| 71 |
if err != nil { |
| 72 |
return nil, nil, err |
| 73 |
} |
| 74 |
|
| 75 |
actionsOrgBilling := new(ActionBilling) |
| 76 |
resp, err := s.client.Do(ctx, req, actionsOrgBilling) |
| 77 |
if err != nil { |
| 78 |
return nil, resp, err |
| 79 |
} |
| 80 |
|
| 81 |
return actionsOrgBilling, resp, nil |
| 82 |
} |
| 83 |
|
| 84 |
// GetPackagesBillingOrg returns the free and paid storage used for GitHub Packages in gigabytes for an Org. |
| 85 |
// |
| 86 |
// GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-packages-billing-for-an-organization |
| 87 |
// |
| 88 |
//meta:operation GET /orgs/{org}/settings/billing/packages |
| 89 |
func (s *BillingService) GetPackagesBillingOrg(ctx context.Context, org string) (*PackageBilling, *Response, error) { |
| 90 |
u := fmt.Sprintf("orgs/%v/settings/billing/packages", org) |
| 91 |
req, err := s.client.NewRequest("GET", u, nil) |
| 92 |
if err != nil { |
| 93 |
return nil, nil, err |
| 94 |
} |
| 95 |
|
| 96 |
packagesOrgBilling := new(PackageBilling) |
| 97 |
resp, err := s.client.Do(ctx, req, packagesOrgBilling) |
| 98 |
if err != nil { |
| 99 |
return nil, resp, err |
| 100 |
} |
| 101 |
|
| 102 |
return packagesOrgBilling, resp, nil |
| 103 |
} |
| 104 |
|
| 105 |
// GetStorageBillingOrg returns the estimated paid and estimated total storage used for GitHub Actions |
| 106 |
// and GitHub Packages in gigabytes for an Org. |
| 107 |
// |
| 108 |
// GitHub API docs: https://docs.github.com/rest/billing/billing#get-shared-storage-billing-for-an-organization |
| 109 |
// |
| 110 |
//meta:operation GET /orgs/{org}/settings/billing/shared-storage |
| 111 |
func (s *BillingService) GetStorageBillingOrg(ctx context.Context, org string) (*StorageBilling, *Response, error) { |
| 112 |
u := fmt.Sprintf("orgs/%v/settings/billing/shared-storage", org) |
| 113 |
req, err := s.client.NewRequest("GET", u, nil) |
| 114 |
if err != nil { |
| 115 |
return nil, nil, err |
| 116 |
} |
| 117 |
|
| 118 |
storageOrgBilling := new(StorageBilling) |
| 119 |
resp, err := s.client.Do(ctx, req, storageOrgBilling) |
| 120 |
if err != nil { |
| 121 |
return nil, resp, err |
| 122 |
} |
| 123 |
|
| 124 |
return storageOrgBilling, resp, nil |
| 125 |
} |
| 126 |
|
| 127 |
// GetAdvancedSecurityActiveCommittersOrg returns the GitHub Advanced Security active committers for an organization per repository. |
| 128 |
// |
| 129 |
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/billing/billing#get-github-advanced-security-active-committers-for-an-organization |
| 130 |
// |
| 131 |
//meta:operation GET /orgs/{org}/settings/billing/advanced-security |
| 132 |
func (s *BillingService) GetAdvancedSecurityActiveCommittersOrg(ctx context.Context, org string, opts *ListOptions) (*ActiveCommitters, *Response, error) { |
| 133 |
u := fmt.Sprintf("orgs/%v/settings/billing/advanced-security", org) |
| 134 |
u, err := addOptions(u, opts) |
| 135 |
if err != nil { |
| 136 |
return nil, nil, err |
| 137 |
} |
| 138 |
|
| 139 |
req, err := s.client.NewRequest("GET", u, nil) |
| 140 |
if err != nil { |
| 141 |
return nil, nil, err |
| 142 |
} |
| 143 |
|
| 144 |
activeOrgCommitters := new(ActiveCommitters) |
| 145 |
resp, err := s.client.Do(ctx, req, activeOrgCommitters) |
| 146 |
if err != nil { |
| 147 |
return nil, resp, err |
| 148 |
} |
| 149 |
|
| 150 |
return activeOrgCommitters, resp, nil |
| 151 |
} |
| 152 |
|
| 153 |
// GetActionsBillingUser returns the summary of the free and paid GitHub Actions minutes used for a user. |
| 154 |
// |
| 155 |
// GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-actions-billing-for-a-user |
| 156 |
// |
| 157 |
//meta:operation GET /users/{username}/settings/billing/actions |
| 158 |
func (s *BillingService) GetActionsBillingUser(ctx context.Context, user string) (*ActionBilling, *Response, error) { |
| 159 |
u := fmt.Sprintf("users/%v/settings/billing/actions", user) |
| 160 |
req, err := s.client.NewRequest("GET", u, nil) |
| 161 |
if err != nil { |
| 162 |
return nil, nil, err |
| 163 |
} |
| 164 |
|
| 165 |
actionsUserBilling := new(ActionBilling) |
| 166 |
resp, err := s.client.Do(ctx, req, actionsUserBilling) |
| 167 |
if err != nil { |
| 168 |
return nil, resp, err |
| 169 |
} |
| 170 |
|
| 171 |
return actionsUserBilling, resp, nil |
| 172 |
} |
| 173 |
|
| 174 |
// GetPackagesBillingUser returns the free and paid storage used for GitHub Packages in gigabytes for a user. |
| 175 |
// |
| 176 |
// GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-packages-billing-for-a-user |
| 177 |
// |
| 178 |
//meta:operation GET /users/{username}/settings/billing/packages |
| 179 |
func (s *BillingService) GetPackagesBillingUser(ctx context.Context, user string) (*PackageBilling, *Response, error) { |
| 180 |
u := fmt.Sprintf("users/%v/settings/billing/packages", user) |
| 181 |
req, err := s.client.NewRequest("GET", u, nil) |
| 182 |
if err != nil { |
| 183 |
return nil, nil, err |
| 184 |
} |
| 185 |
|
| 186 |
packagesUserBilling := new(PackageBilling) |
| 187 |
resp, err := s.client.Do(ctx, req, packagesUserBilling) |
| 188 |
if err != nil { |
| 189 |
return nil, resp, err |
| 190 |
} |
| 191 |
|
| 192 |
return packagesUserBilling, resp, nil |
| 193 |
} |
| 194 |
|
| 195 |
// GetStorageBillingUser returns the estimated paid and estimated total storage used for GitHub Actions |
| 196 |
// and GitHub Packages in gigabytes for a user. |
| 197 |
// |
| 198 |
// GitHub API docs: https://docs.github.com/rest/billing/billing#get-shared-storage-billing-for-a-user |
| 199 |
// |
| 200 |
//meta:operation GET /users/{username}/settings/billing/shared-storage |
| 201 |
func (s *BillingService) GetStorageBillingUser(ctx context.Context, user string) (*StorageBilling, *Response, error) { |
| 202 |
u := fmt.Sprintf("users/%v/settings/billing/shared-storage", user) |
| 203 |
req, err := s.client.NewRequest("GET", u, nil) |
| 204 |
if err != nil { |
| 205 |
return nil, nil, err |
| 206 |
} |
| 207 |
|
| 208 |
storageUserBilling := new(StorageBilling) |
| 209 |
resp, err := s.client.Do(ctx, req, storageUserBilling) |
| 210 |
if err != nil { |
| 211 |
return nil, resp, err |
| 212 |
} |
| 213 |
|
| 214 |
return storageUserBilling, resp, nil |
| 215 |
} |