github.com/google/go-github/v71@v71.0.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/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  	TotalCount                          int                           `json:"total_count"`
    48  	MaximumAdvancedSecurityCommitters   int                           `json:"maximum_advanced_security_committers"`
    49  	PurchasedAdvancedSecurityCommitters int                           `json:"purchased_advanced_security_committers"`
    50  	Repositories                        []*RepositoryActiveCommitters `json:"repositories,omitempty"`
    51  }
    52  
    53  // RepositoryActiveCommitters represents active committers on each repository.
    54  type RepositoryActiveCommitters struct {
    55  	Name                                *string                                `json:"name,omitempty"`
    56  	AdvancedSecurityCommitters          *int                                   `json:"advanced_security_committers,omitempty"`
    57  	AdvancedSecurityCommittersBreakdown []*AdvancedSecurityCommittersBreakdown `json:"advanced_security_committers_breakdown,omitempty"`
    58  }
    59  
    60  // AdvancedSecurityCommittersBreakdown represents the user activity breakdown for ActiveCommitters.
    61  type AdvancedSecurityCommittersBreakdown struct {
    62  	UserLogin      *string `json:"user_login,omitempty"`
    63  	LastPushedDate *string `json:"last_pushed_date,omitempty"`
    64  }
    65  
    66  // GetActionsBillingOrg returns the summary of the free and paid GitHub Actions minutes used for an Org.
    67  //
    68  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-actions-billing-for-an-organization
    69  //
    70  //meta:operation GET /orgs/{org}/settings/billing/actions
    71  func (s *BillingService) GetActionsBillingOrg(ctx context.Context, org string) (*ActionBilling, *Response, error) {
    72  	u := fmt.Sprintf("orgs/%v/settings/billing/actions", org)
    73  	req, err := s.client.NewRequest("GET", u, nil)
    74  	if err != nil {
    75  		return nil, nil, err
    76  	}
    77  
    78  	actionsOrgBilling := new(ActionBilling)
    79  	resp, err := s.client.Do(ctx, req, actionsOrgBilling)
    80  	if err != nil {
    81  		return nil, resp, err
    82  	}
    83  
    84  	return actionsOrgBilling, resp, nil
    85  }
    86  
    87  // GetPackagesBillingOrg returns the free and paid storage used for GitHub Packages in gigabytes for an Org.
    88  //
    89  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-packages-billing-for-an-organization
    90  //
    91  //meta:operation GET /orgs/{org}/settings/billing/packages
    92  func (s *BillingService) GetPackagesBillingOrg(ctx context.Context, org string) (*PackageBilling, *Response, error) {
    93  	u := fmt.Sprintf("orgs/%v/settings/billing/packages", org)
    94  	req, err := s.client.NewRequest("GET", u, nil)
    95  	if err != nil {
    96  		return nil, nil, err
    97  	}
    98  
    99  	packagesOrgBilling := new(PackageBilling)
   100  	resp, err := s.client.Do(ctx, req, packagesOrgBilling)
   101  	if err != nil {
   102  		return nil, resp, err
   103  	}
   104  
   105  	return packagesOrgBilling, resp, nil
   106  }
   107  
   108  // GetStorageBillingOrg returns the estimated paid and estimated total storage used for GitHub Actions
   109  // and GitHub Packages in gigabytes for an Org.
   110  //
   111  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-shared-storage-billing-for-an-organization
   112  //
   113  //meta:operation GET /orgs/{org}/settings/billing/shared-storage
   114  func (s *BillingService) GetStorageBillingOrg(ctx context.Context, org string) (*StorageBilling, *Response, error) {
   115  	u := fmt.Sprintf("orgs/%v/settings/billing/shared-storage", org)
   116  	req, err := s.client.NewRequest("GET", u, nil)
   117  	if err != nil {
   118  		return nil, nil, err
   119  	}
   120  
   121  	storageOrgBilling := new(StorageBilling)
   122  	resp, err := s.client.Do(ctx, req, storageOrgBilling)
   123  	if err != nil {
   124  		return nil, resp, err
   125  	}
   126  
   127  	return storageOrgBilling, resp, nil
   128  }
   129  
   130  // GetAdvancedSecurityActiveCommittersOrg returns the GitHub Advanced Security active committers for an organization per repository.
   131  //
   132  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/billing/billing#get-github-advanced-security-active-committers-for-an-organization
   133  //
   134  //meta:operation GET /orgs/{org}/settings/billing/advanced-security
   135  func (s *BillingService) GetAdvancedSecurityActiveCommittersOrg(ctx context.Context, org string, opts *ListOptions) (*ActiveCommitters, *Response, error) {
   136  	u := fmt.Sprintf("orgs/%v/settings/billing/advanced-security", org)
   137  	u, err := addOptions(u, opts)
   138  	if err != nil {
   139  		return nil, nil, err
   140  	}
   141  
   142  	req, err := s.client.NewRequest("GET", u, nil)
   143  	if err != nil {
   144  		return nil, nil, err
   145  	}
   146  
   147  	activeOrgCommitters := new(ActiveCommitters)
   148  	resp, err := s.client.Do(ctx, req, activeOrgCommitters)
   149  	if err != nil {
   150  		return nil, resp, err
   151  	}
   152  
   153  	return activeOrgCommitters, resp, nil
   154  }
   155  
   156  // GetActionsBillingUser returns the summary of the free and paid GitHub Actions minutes used for a user.
   157  //
   158  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-actions-billing-for-a-user
   159  //
   160  //meta:operation GET /users/{username}/settings/billing/actions
   161  func (s *BillingService) GetActionsBillingUser(ctx context.Context, user string) (*ActionBilling, *Response, error) {
   162  	u := fmt.Sprintf("users/%v/settings/billing/actions", user)
   163  	req, err := s.client.NewRequest("GET", u, nil)
   164  	if err != nil {
   165  		return nil, nil, err
   166  	}
   167  
   168  	actionsUserBilling := new(ActionBilling)
   169  	resp, err := s.client.Do(ctx, req, actionsUserBilling)
   170  	if err != nil {
   171  		return nil, resp, err
   172  	}
   173  
   174  	return actionsUserBilling, resp, nil
   175  }
   176  
   177  // GetPackagesBillingUser returns the free and paid storage used for GitHub Packages in gigabytes for a user.
   178  //
   179  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-packages-billing-for-a-user
   180  //
   181  //meta:operation GET /users/{username}/settings/billing/packages
   182  func (s *BillingService) GetPackagesBillingUser(ctx context.Context, user string) (*PackageBilling, *Response, error) {
   183  	u := fmt.Sprintf("users/%v/settings/billing/packages", user)
   184  	req, err := s.client.NewRequest("GET", u, nil)
   185  	if err != nil {
   186  		return nil, nil, err
   187  	}
   188  
   189  	packagesUserBilling := new(PackageBilling)
   190  	resp, err := s.client.Do(ctx, req, packagesUserBilling)
   191  	if err != nil {
   192  		return nil, resp, err
   193  	}
   194  
   195  	return packagesUserBilling, resp, nil
   196  }
   197  
   198  // GetStorageBillingUser returns the estimated paid and estimated total storage used for GitHub Actions
   199  // and GitHub Packages in gigabytes for a user.
   200  //
   201  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-shared-storage-billing-for-a-user
   202  //
   203  //meta:operation GET /users/{username}/settings/billing/shared-storage
   204  func (s *BillingService) GetStorageBillingUser(ctx context.Context, user string) (*StorageBilling, *Response, error) {
   205  	u := fmt.Sprintf("users/%v/settings/billing/shared-storage", user)
   206  	req, err := s.client.NewRequest("GET", u, nil)
   207  	if err != nil {
   208  		return nil, nil, err
   209  	}
   210  
   211  	storageUserBilling := new(StorageBilling)
   212  	resp, err := s.client.Do(ctx, req, storageUserBilling)
   213  	if err != nil {
   214  		return nil, resp, err
   215  	}
   216  
   217  	return storageUserBilling, resp, nil
   218  }