github.com/google/go-github/v49@v49.1.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     int                  `json:"total_minutes_used"`
    22  	TotalPaidMinutesUsed float64              `json:"total_paid_minutes_used"`
    23  	IncludedMinutes      int                  `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      int `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     int     `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/rest/billing#get-github-advanced-security-active-committers-for-an-organization
   124  func (s *BillingService) GetAdvancedSecurityActiveCommittersOrg(ctx context.Context, org string) (*ActiveCommitters, *Response, error) {
   125  	u := fmt.Sprintf("orgs/%v/settings/billing/advanced-security", org)
   126  	req, err := s.client.NewRequest("GET", u, nil)
   127  	if err != nil {
   128  		return nil, nil, err
   129  	}
   130  
   131  	activeOrgCommitters := new(ActiveCommitters)
   132  	resp, err := s.client.Do(ctx, req, activeOrgCommitters)
   133  	if err != nil {
   134  		return nil, resp, err
   135  	}
   136  
   137  	return activeOrgCommitters, resp, nil
   138  }
   139  
   140  // GetActionsBillingUser returns the summary of the free and paid GitHub Actions minutes used for a user.
   141  //
   142  // GitHub API docs: https://docs.github.com/en/rest/billing#get-github-actions-billing-for-a-user
   143  func (s *BillingService) GetActionsBillingUser(ctx context.Context, user string) (*ActionBilling, *Response, error) {
   144  	u := fmt.Sprintf("users/%v/settings/billing/actions", user)
   145  	req, err := s.client.NewRequest("GET", u, nil)
   146  	if err != nil {
   147  		return nil, nil, err
   148  	}
   149  
   150  	actionsUserBilling := new(ActionBilling)
   151  	resp, err := s.client.Do(ctx, req, actionsUserBilling)
   152  	if err != nil {
   153  		return nil, resp, err
   154  	}
   155  
   156  	return actionsUserBilling, resp, nil
   157  }
   158  
   159  // GetPackagesBillingUser returns the free and paid storage used for GitHub Packages in gigabytes for a user.
   160  //
   161  // GitHub API docs: https://docs.github.com/en/rest/billing#get-github-packages-billing-for-a-user
   162  func (s *BillingService) GetPackagesBillingUser(ctx context.Context, user string) (*PackageBilling, *Response, error) {
   163  	u := fmt.Sprintf("users/%v/settings/billing/packages", user)
   164  	req, err := s.client.NewRequest("GET", u, nil)
   165  	if err != nil {
   166  		return nil, nil, err
   167  	}
   168  
   169  	packagesUserBilling := new(PackageBilling)
   170  	resp, err := s.client.Do(ctx, req, packagesUserBilling)
   171  	if err != nil {
   172  		return nil, resp, err
   173  	}
   174  
   175  	return packagesUserBilling, resp, nil
   176  }
   177  
   178  // GetStorageBillingUser returns the estimated paid and estimated total storage used for GitHub Actions
   179  // and GitHub Packages in gigabytes for a user.
   180  //
   181  // GitHub API docs: https://docs.github.com/en/rest/billing#get-shared-storage-billing-for-a-user
   182  func (s *BillingService) GetStorageBillingUser(ctx context.Context, user string) (*StorageBilling, *Response, error) {
   183  	u := fmt.Sprintf("users/%v/settings/billing/shared-storage", user)
   184  	req, err := s.client.NewRequest("GET", u, nil)
   185  	if err != nil {
   186  		return nil, nil, err
   187  	}
   188  
   189  	storageUserBilling := new(StorageBilling)
   190  	resp, err := s.client.Do(ctx, req, storageUserBilling)
   191  	if err != nil {
   192  		return nil, resp, err
   193  	}
   194  
   195  	return storageUserBilling, resp, nil
   196  }