github.com/google/go-github/v74@v74.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  // UsageReportOptions specifies optional parameters for the enhanced billing platform usage report.
    67  type UsageReportOptions struct {
    68  	// 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.
    69  	// Default value is the current year.
    70  	Year *int `url:"year,omitempty"`
    71  
    72  	// If specified, only return results for a single month. The value of month is an integer between 1 and 12.
    73  	// If no year is specified the default year is used.
    74  	Month *int `url:"month,omitempty"`
    75  
    76  	// If specified, only return results for a single day. The value of day is an integer between 1 and 31.
    77  	// If no year or month is specified, the default year and month are used.
    78  	Day *int `url:"day,omitempty"`
    79  
    80  	// If specified, only return results for a single hour. The value of hour is an integer between 0 and 23.
    81  	// If no year, month, or day is specified, the default year, month, and day are used.
    82  	Hour *int `url:"hour,omitempty"`
    83  }
    84  
    85  // UsageItem represents a single usage item in the enhanced billing platform report.
    86  type UsageItem struct {
    87  	Date           *string  `json:"date"`
    88  	Product        *string  `json:"product"`
    89  	SKU            *string  `json:"sku"`
    90  	Quantity       *float64 `json:"quantity"`
    91  	UnitType       *string  `json:"unitType"`
    92  	PricePerUnit   *float64 `json:"pricePerUnit"`
    93  	GrossAmount    *float64 `json:"grossAmount"`
    94  	DiscountAmount *float64 `json:"discountAmount"`
    95  	NetAmount      *float64 `json:"netAmount"`
    96  	RepositoryName *string  `json:"repositoryName,omitempty"`
    97  	// Organization name is only used for organization-level reports.
    98  	OrganizationName *string `json:"organizationName,omitempty"`
    99  }
   100  
   101  // UsageReport represents the enhanced billing platform usage report response.
   102  type UsageReport struct {
   103  	UsageItems []*UsageItem `json:"usageItems,omitempty"`
   104  }
   105  
   106  // GetActionsBillingOrg returns the summary of the free and paid GitHub Actions minutes used for an Org.
   107  //
   108  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-actions-billing-for-an-organization
   109  //
   110  //meta:operation GET /orgs/{org}/settings/billing/actions
   111  func (s *BillingService) GetActionsBillingOrg(ctx context.Context, org string) (*ActionBilling, *Response, error) {
   112  	u := fmt.Sprintf("orgs/%v/settings/billing/actions", org)
   113  	req, err := s.client.NewRequest("GET", u, nil)
   114  	if err != nil {
   115  		return nil, nil, err
   116  	}
   117  
   118  	actionsOrgBilling := new(ActionBilling)
   119  	resp, err := s.client.Do(ctx, req, actionsOrgBilling)
   120  	if err != nil {
   121  		return nil, resp, err
   122  	}
   123  
   124  	return actionsOrgBilling, resp, nil
   125  }
   126  
   127  // GetPackagesBillingOrg returns the free and paid storage used for GitHub Packages in gigabytes for an Org.
   128  //
   129  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-packages-billing-for-an-organization
   130  //
   131  //meta:operation GET /orgs/{org}/settings/billing/packages
   132  func (s *BillingService) GetPackagesBillingOrg(ctx context.Context, org string) (*PackageBilling, *Response, error) {
   133  	u := fmt.Sprintf("orgs/%v/settings/billing/packages", org)
   134  	req, err := s.client.NewRequest("GET", u, nil)
   135  	if err != nil {
   136  		return nil, nil, err
   137  	}
   138  
   139  	packagesOrgBilling := new(PackageBilling)
   140  	resp, err := s.client.Do(ctx, req, packagesOrgBilling)
   141  	if err != nil {
   142  		return nil, resp, err
   143  	}
   144  
   145  	return packagesOrgBilling, resp, nil
   146  }
   147  
   148  // GetStorageBillingOrg returns the estimated paid and estimated total storage used for GitHub Actions
   149  // and GitHub Packages in gigabytes for an Org.
   150  //
   151  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-shared-storage-billing-for-an-organization
   152  //
   153  //meta:operation GET /orgs/{org}/settings/billing/shared-storage
   154  func (s *BillingService) GetStorageBillingOrg(ctx context.Context, org string) (*StorageBilling, *Response, error) {
   155  	u := fmt.Sprintf("orgs/%v/settings/billing/shared-storage", org)
   156  	req, err := s.client.NewRequest("GET", u, nil)
   157  	if err != nil {
   158  		return nil, nil, err
   159  	}
   160  
   161  	storageOrgBilling := new(StorageBilling)
   162  	resp, err := s.client.Do(ctx, req, storageOrgBilling)
   163  	if err != nil {
   164  		return nil, resp, err
   165  	}
   166  
   167  	return storageOrgBilling, resp, nil
   168  }
   169  
   170  // GetAdvancedSecurityActiveCommittersOrg returns the GitHub Advanced Security active committers for an organization per repository.
   171  //
   172  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/billing/billing#get-github-advanced-security-active-committers-for-an-organization
   173  //
   174  //meta:operation GET /orgs/{org}/settings/billing/advanced-security
   175  func (s *BillingService) GetAdvancedSecurityActiveCommittersOrg(ctx context.Context, org string, opts *ListOptions) (*ActiveCommitters, *Response, error) {
   176  	u := fmt.Sprintf("orgs/%v/settings/billing/advanced-security", org)
   177  	u, err := addOptions(u, opts)
   178  	if err != nil {
   179  		return nil, nil, err
   180  	}
   181  
   182  	req, err := s.client.NewRequest("GET", u, nil)
   183  	if err != nil {
   184  		return nil, nil, err
   185  	}
   186  
   187  	activeOrgCommitters := new(ActiveCommitters)
   188  	resp, err := s.client.Do(ctx, req, activeOrgCommitters)
   189  	if err != nil {
   190  		return nil, resp, err
   191  	}
   192  
   193  	return activeOrgCommitters, resp, nil
   194  }
   195  
   196  // GetActionsBillingUser returns the summary of the free and paid GitHub Actions minutes used for a user.
   197  //
   198  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-actions-billing-for-a-user
   199  //
   200  //meta:operation GET /users/{username}/settings/billing/actions
   201  func (s *BillingService) GetActionsBillingUser(ctx context.Context, user string) (*ActionBilling, *Response, error) {
   202  	u := fmt.Sprintf("users/%v/settings/billing/actions", user)
   203  	req, err := s.client.NewRequest("GET", u, nil)
   204  	if err != nil {
   205  		return nil, nil, err
   206  	}
   207  
   208  	actionsUserBilling := new(ActionBilling)
   209  	resp, err := s.client.Do(ctx, req, actionsUserBilling)
   210  	if err != nil {
   211  		return nil, resp, err
   212  	}
   213  
   214  	return actionsUserBilling, resp, nil
   215  }
   216  
   217  // GetPackagesBillingUser returns the free and paid storage used for GitHub Packages in gigabytes for a user.
   218  //
   219  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-github-packages-billing-for-a-user
   220  //
   221  //meta:operation GET /users/{username}/settings/billing/packages
   222  func (s *BillingService) GetPackagesBillingUser(ctx context.Context, user string) (*PackageBilling, *Response, error) {
   223  	u := fmt.Sprintf("users/%v/settings/billing/packages", user)
   224  	req, err := s.client.NewRequest("GET", u, nil)
   225  	if err != nil {
   226  		return nil, nil, err
   227  	}
   228  
   229  	packagesUserBilling := new(PackageBilling)
   230  	resp, err := s.client.Do(ctx, req, packagesUserBilling)
   231  	if err != nil {
   232  		return nil, resp, err
   233  	}
   234  
   235  	return packagesUserBilling, resp, nil
   236  }
   237  
   238  // GetStorageBillingUser returns the estimated paid and estimated total storage used for GitHub Actions
   239  // and GitHub Packages in gigabytes for a user.
   240  //
   241  // GitHub API docs: https://docs.github.com/rest/billing/billing#get-shared-storage-billing-for-a-user
   242  //
   243  //meta:operation GET /users/{username}/settings/billing/shared-storage
   244  func (s *BillingService) GetStorageBillingUser(ctx context.Context, user string) (*StorageBilling, *Response, error) {
   245  	u := fmt.Sprintf("users/%v/settings/billing/shared-storage", user)
   246  	req, err := s.client.NewRequest("GET", u, nil)
   247  	if err != nil {
   248  		return nil, nil, err
   249  	}
   250  
   251  	storageUserBilling := new(StorageBilling)
   252  	resp, err := s.client.Do(ctx, req, storageUserBilling)
   253  	if err != nil {
   254  		return nil, resp, err
   255  	}
   256  
   257  	return storageUserBilling, resp, nil
   258  }
   259  
   260  // GetUsageReportOrg returns a report of the total usage for an organization using the enhanced billing platform.
   261  //
   262  // Note: This endpoint is only available to organizations with access to the enhanced billing platform.
   263  //
   264  // GitHub API docs: https://docs.github.com/rest/billing/enhanced-billing#get-billing-usage-report-for-an-organization
   265  //
   266  //meta:operation GET /organizations/{org}/settings/billing/usage
   267  func (s *BillingService) GetUsageReportOrg(ctx context.Context, org string, opts *UsageReportOptions) (*UsageReport, *Response, error) {
   268  	u := fmt.Sprintf("organizations/%v/settings/billing/usage", org)
   269  	u, err := addOptions(u, opts)
   270  	if err != nil {
   271  		return nil, nil, err
   272  	}
   273  
   274  	req, err := s.client.NewRequest("GET", u, nil)
   275  	if err != nil {
   276  		return nil, nil, err
   277  	}
   278  
   279  	usageReport := new(UsageReport)
   280  	resp, err := s.client.Do(ctx, req, usageReport)
   281  	if err != nil {
   282  		return nil, resp, err
   283  	}
   284  
   285  	return usageReport, resp, nil
   286  }
   287  
   288  // GetUsageReportUser returns a report of the total usage for a user using the enhanced billing platform.
   289  //
   290  // Note: This endpoint is only available to users with access to the enhanced billing platform.
   291  //
   292  // GitHub API docs: https://docs.github.com/rest/billing/enhanced-billing#get-billing-usage-report-for-a-user
   293  //
   294  //meta:operation GET /users/{username}/settings/billing/usage
   295  func (s *BillingService) GetUsageReportUser(ctx context.Context, user string, opts *UsageReportOptions) (*UsageReport, *Response, error) {
   296  	u := fmt.Sprintf("users/%v/settings/billing/usage", user)
   297  	u, err := addOptions(u, opts)
   298  	if err != nil {
   299  		return nil, nil, err
   300  	}
   301  
   302  	req, err := s.client.NewRequest("GET", u, nil)
   303  	if err != nil {
   304  		return nil, nil, err
   305  	}
   306  
   307  	usageReport := new(UsageReport)
   308  	resp, err := s.client.Do(ctx, req, usageReport)
   309  	if err != nil {
   310  		return nil, resp, err
   311  	}
   312  
   313  	return usageReport, resp, nil
   314  }