github.com/google/go-github/v53@v53.2.0/github/billing.go (about) 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/en/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/en/rest/billing#get-github-actions-billing-for-an-organization 66 func (s *BillingService) GetActionsBillingOrg(ctx context.Context, org string) (*ActionBilling, *Response, error) { 67 u := fmt.Sprintf("orgs/%v/settings/billing/actions", org) 68 req, err := s.client.NewRequest("GET", u, nil) 69 if err != nil { 70 return nil, nil, err 71 } 72 73 actionsOrgBilling := new(ActionBilling) 74 resp, err := s.client.Do(ctx, req, actionsOrgBilling) 75 if err != nil { 76 return nil, resp, err 77 } 78 79 return actionsOrgBilling, resp, nil 80 } 81 82 // GetPackagesBillingOrg returns the free and paid storage used for GitHub Packages in gigabytes for an Org. 83 // 84 // GitHub API docs: https://docs.github.com/en/rest/billing#get-github-packages-billing-for-an-organization 85 func (s *BillingService) GetPackagesBillingOrg(ctx context.Context, org string) (*PackageBilling, *Response, error) { 86 u := fmt.Sprintf("orgs/%v/settings/billing/packages", org) 87 req, err := s.client.NewRequest("GET", u, nil) 88 if err != nil { 89 return nil, nil, err 90 } 91 92 packagesOrgBilling := new(PackageBilling) 93 resp, err := s.client.Do(ctx, req, packagesOrgBilling) 94 if err != nil { 95 return nil, resp, err 96 } 97 98 return packagesOrgBilling, resp, nil 99 } 100 101 // GetStorageBillingOrg returns the estimated paid and estimated total storage used for GitHub Actions 102 // and GitHub Packages in gigabytes for an Org. 103 // 104 // GitHub API docs: https://docs.github.com/en/rest/billing#get-shared-storage-billing-for-an-organization 105 func (s *BillingService) GetStorageBillingOrg(ctx context.Context, org string) (*StorageBilling, *Response, error) { 106 u := fmt.Sprintf("orgs/%v/settings/billing/shared-storage", org) 107 req, err := s.client.NewRequest("GET", u, nil) 108 if err != nil { 109 return nil, nil, err 110 } 111 112 storageOrgBilling := new(StorageBilling) 113 resp, err := s.client.Do(ctx, req, storageOrgBilling) 114 if err != nil { 115 return nil, resp, err 116 } 117 118 return storageOrgBilling, resp, nil 119 } 120 121 // GetAdvancedSecurityActiveCommittersOrg returns the GitHub Advanced Security active committers for an organization per repository. 122 // 123 // GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/billing?apiVersion=2022-11-28#get-github-advanced-security-active-committers-for-an-organization 124 func (s *BillingService) GetAdvancedSecurityActiveCommittersOrg(ctx context.Context, org string, opts *ListOptions) (*ActiveCommitters, *Response, error) { 125 u := fmt.Sprintf("orgs/%v/settings/billing/advanced-security", org) 126 u, err := addOptions(u, opts) 127 if err != nil { 128 return nil, nil, err 129 } 130 131 req, err := s.client.NewRequest("GET", u, nil) 132 if err != nil { 133 return nil, nil, err 134 } 135 136 activeOrgCommitters := new(ActiveCommitters) 137 resp, err := s.client.Do(ctx, req, activeOrgCommitters) 138 if err != nil { 139 return nil, resp, err 140 } 141 142 return activeOrgCommitters, resp, nil 143 } 144 145 // GetActionsBillingUser returns the summary of the free and paid GitHub Actions minutes used for a user. 146 // 147 // GitHub API docs: https://docs.github.com/en/rest/billing#get-github-actions-billing-for-a-user 148 func (s *BillingService) GetActionsBillingUser(ctx context.Context, user string) (*ActionBilling, *Response, error) { 149 u := fmt.Sprintf("users/%v/settings/billing/actions", user) 150 req, err := s.client.NewRequest("GET", u, nil) 151 if err != nil { 152 return nil, nil, err 153 } 154 155 actionsUserBilling := new(ActionBilling) 156 resp, err := s.client.Do(ctx, req, actionsUserBilling) 157 if err != nil { 158 return nil, resp, err 159 } 160 161 return actionsUserBilling, resp, nil 162 } 163 164 // GetPackagesBillingUser returns the free and paid storage used for GitHub Packages in gigabytes for a user. 165 // 166 // GitHub API docs: https://docs.github.com/en/rest/billing#get-github-packages-billing-for-a-user 167 func (s *BillingService) GetPackagesBillingUser(ctx context.Context, user string) (*PackageBilling, *Response, error) { 168 u := fmt.Sprintf("users/%v/settings/billing/packages", user) 169 req, err := s.client.NewRequest("GET", u, nil) 170 if err != nil { 171 return nil, nil, err 172 } 173 174 packagesUserBilling := new(PackageBilling) 175 resp, err := s.client.Do(ctx, req, packagesUserBilling) 176 if err != nil { 177 return nil, resp, err 178 } 179 180 return packagesUserBilling, resp, nil 181 } 182 183 // GetStorageBillingUser returns the estimated paid and estimated total storage used for GitHub Actions 184 // and GitHub Packages in gigabytes for a user. 185 // 186 // GitHub API docs: https://docs.github.com/en/rest/billing#get-shared-storage-billing-for-a-user 187 func (s *BillingService) GetStorageBillingUser(ctx context.Context, user string) (*StorageBilling, *Response, error) { 188 u := fmt.Sprintf("users/%v/settings/billing/shared-storage", user) 189 req, err := s.client.NewRequest("GET", u, nil) 190 if err != nil { 191 return nil, nil, err 192 } 193 194 storageUserBilling := new(StorageBilling) 195 resp, err := s.client.Do(ctx, req, storageUserBilling) 196 if err != nil { 197 return nil, resp, err 198 } 199 200 return storageUserBilling, resp, nil 201 }