-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Add support for GitHub Budgets API #3931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
youneedgreg
wants to merge
7
commits into
google:master
Choose a base branch
from
youneedgreg:budget-api-update
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3af8d69
feat: add support for GitHub Budgets API
youneedgreg d18bb71
chore: address PR #3931 review comments
youneedgreg bf47de7
test: add error path tests for 100% coverage
youneedgreg 0e911d0
chore: update generated files
youneedgreg 57e424b
chore: address PR #3931 review comments
youneedgreg 1966b29
update billing return types
youneedgreg 9dc830b
fix: resolved the lint errors.
youneedgreg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright 2026 The go-github AUTHORS. All rights reserved. | ||
| // | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // Budget represents a GitHub budget. | ||
| type Budget struct { | ||
| ID *string `json:"id,omitempty"` | ||
| BudgetName *string `json:"budget_name,omitempty"` | ||
| TargetSubAccount *string `json:"target_sub_account,omitempty"` | ||
| TargetType *string `json:"target_type,omitempty"` | ||
| TargetID *int64 `json:"target_id,omitempty"` | ||
| TargetName *string `json:"target_name,omitempty"` | ||
| PricingModel *string `json:"pricing_model,omitempty"` | ||
| PricingModelID *string `json:"pricing_model_id,omitempty"` | ||
| PricingModelDisplayName *string `json:"pricing_model_display_name,omitempty"` | ||
| BudgetType *string `json:"budget_type,omitempty"` | ||
| LimitAmount *float64 `json:"limit_amount,omitempty"` | ||
| CurrentAmount *float64 `json:"current_amount,omitempty"` | ||
| Currency *string `json:"currency,omitempty"` | ||
| ExcludeCostCenterUsage *bool `json:"exclude_cost_center_usage,omitempty"` | ||
| BudgetAlerting *BudgetAlerting `json:"budget_alerting,omitempty"` | ||
| } | ||
|
|
||
| // BudgetAlerting represents the alerting configuration for a budget. | ||
| type BudgetAlerting struct { | ||
| WillAlert *bool `json:"will_alert,omitempty"` | ||
| AlertRecipients []string `json:"alert_recipients,omitempty"` | ||
| } | ||
|
|
||
| // BudgetList represents a list of budgets. | ||
| type BudgetList struct { | ||
| Budgets []*Budget `json:"budgets"` | ||
| HasNextPage *bool `json:"has_next_page,omitempty"` | ||
| } | ||
|
|
||
| // BudgetResponse represents the response when updating a budget. | ||
| type BudgetResponse struct { | ||
| Budget *Budget `json:"budget"` | ||
| Message *string `json:"message,omitempty"` | ||
| } | ||
|
|
||
| // ListOrganizationBudgets lists all budgets for an organization. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/rest/billing/budgets#get-all-budgets-for-an-organization | ||
| // | ||
| //meta:operation GET /organizations/{org}/settings/billing/budgets | ||
| func (s *BillingService) ListOrganizationBudgets(ctx context.Context, org string) (*BudgetList, *Response, error) { | ||
| u := fmt.Sprintf("organizations/%v/settings/billing/budgets", org) | ||
| req, err := s.client.NewRequest("GET", u, nil) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| budgets := new(BudgetList) | ||
| resp, err := s.client.Do(ctx, req, budgets) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
|
|
||
| return budgets, resp, nil | ||
| } | ||
|
|
||
| // GetOrganizationBudget gets a specific budget for an organization. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/rest/billing/budgets#get-a-budget-by-id-for-an-organization | ||
| // | ||
| //meta:operation GET /organizations/{org}/settings/billing/budgets/{budget_id} | ||
| func (s *BillingService) GetOrganizationBudget(ctx context.Context, org, budgetID string) (*Budget, *Response, error) { | ||
| u := fmt.Sprintf("organizations/%v/settings/billing/budgets/%v", org, budgetID) | ||
| req, err := s.client.NewRequest("GET", u, nil) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| budget := new(Budget) | ||
| resp, err := s.client.Do(ctx, req, budget) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
|
|
||
| return budget, resp, nil | ||
| } | ||
|
|
||
| // UpdateOrganizationBudget updates a specific budget for an organization. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/rest/billing/budgets#update-a-budget-for-an-organization | ||
| // | ||
| //meta:operation PATCH /organizations/{org}/settings/billing/budgets/{budget_id} | ||
| func (s *BillingService) UpdateOrganizationBudget(ctx context.Context, org, budgetID string, budget *Budget) (*BudgetResponse, *Response, error) { | ||
| u := fmt.Sprintf("organizations/%v/settings/billing/budgets/%v", org, budgetID) | ||
| req, err := s.client.NewRequest("PATCH", u, budget) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| updatedBudget := new(BudgetResponse) | ||
| resp, err := s.client.Do(ctx, req, updatedBudget) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
|
|
||
| return updatedBudget, resp, nil | ||
| } | ||
|
|
||
| // DeleteOrganizationBudget deletes a specific budget for an organization. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/rest/billing/budgets#delete-a-budget-for-an-organization | ||
| // | ||
| //meta:operation DELETE /organizations/{org}/settings/billing/budgets/{budget_id} | ||
| func (s *BillingService) DeleteOrganizationBudget(ctx context.Context, org, budgetID string) (*Response, error) { | ||
| u := fmt.Sprintf("organizations/%v/settings/billing/budgets/%v", org, budgetID) | ||
| req, err := s.client.NewRequest("DELETE", u, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return s.client.Do(ctx, req, nil) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| // Copyright 2026 The go-github AUTHORS. All rights reserved. | ||
| // | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package github | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| func TestBillingService_ListOrganizationBudgets(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| mux.HandleFunc("/organizations/o/settings/billing/budgets", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "GET") | ||
| fmt.Fprint(w, `{ | ||
| "budgets": [ | ||
| { | ||
| "id": "1", | ||
| "budget_name": "Budget 1", | ||
| "limit_amount": 100.5, | ||
| "budget_alerting": { | ||
| "will_alert": true, | ||
| "alert_recipients": ["user1"] | ||
| } | ||
| } | ||
| ] | ||
| }`) | ||
| }) | ||
|
|
||
| ctx := t.Context() | ||
| budgets, _, err := client.Billing.ListOrganizationBudgets(ctx, "o") | ||
| if err != nil { | ||
| t.Errorf("Billing.ListOrganizationBudgets returned error: %v", err) | ||
| } | ||
|
|
||
| want := &BudgetList{ | ||
| Budgets: []*Budget{ | ||
| { | ||
| ID: Ptr("1"), | ||
| BudgetName: Ptr("Budget 1"), | ||
| LimitAmount: Ptr(100.5), | ||
| BudgetAlerting: &BudgetAlerting{ | ||
| WillAlert: Ptr(true), | ||
| AlertRecipients: []string{"user1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| if !cmp.Equal(budgets, want) { | ||
| t.Errorf("Billing.ListOrganizationBudgets returned %+v, want %+v", budgets, want) | ||
| } | ||
| const methodName = "ListOrganizationBudgets" | ||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| got, resp, err := client.Billing.ListOrganizationBudgets(ctx, "o") | ||
| if got != nil { | ||
| t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
| } | ||
| return resp, err | ||
| }) | ||
| } | ||
|
|
||
| func TestBillingService_GetOrganizationBudget(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| mux.HandleFunc("/organizations/o/settings/billing/budgets/1", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "GET") | ||
| fmt.Fprint(w, `{ | ||
| "id": "1", | ||
| "budget_name": "Budget 1" | ||
| }`) | ||
| }) | ||
|
|
||
| ctx := t.Context() | ||
| budget, _, err := client.Billing.GetOrganizationBudget(ctx, "o", "1") | ||
| if err != nil { | ||
| t.Errorf("Billing.GetOrganizationBudget returned error: %v", err) | ||
| } | ||
|
|
||
| want := &Budget{ | ||
| ID: Ptr("1"), | ||
| BudgetName: Ptr("Budget 1"), | ||
| } | ||
| if !cmp.Equal(budget, want) { | ||
| t.Errorf("Billing.GetOrganizationBudget returned %+v, want %+v", budget, want) | ||
| } | ||
| const methodName = "GetOrganizationBudget" | ||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| got, resp, err := client.Billing.GetOrganizationBudget(ctx, "o", "1") | ||
| if got != nil { | ||
| t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
| } | ||
| return resp, err | ||
| }) | ||
| } | ||
|
|
||
| func TestBillingService_UpdateOrganizationBudget(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| input := &Budget{ | ||
| BudgetName: Ptr("Updated Budget"), | ||
| } | ||
|
|
||
| mux.HandleFunc("/organizations/o/settings/billing/budgets/1", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "PATCH") | ||
| testBody(t, r, `{"budget_name":"Updated Budget"}`+"\n") | ||
| fmt.Fprint(w, `{ | ||
| "budget": { | ||
| "id": "1", | ||
| "budget_name": "Updated Budget" | ||
| } | ||
| }`) | ||
| }) | ||
|
|
||
| ctx := t.Context() | ||
| budget, _, err := client.Billing.UpdateOrganizationBudget(ctx, "o", "1", input) | ||
| if err != nil { | ||
| t.Errorf("Billing.UpdateOrganizationBudget returned error: %v", err) | ||
| } | ||
|
|
||
| want := &BudgetResponse{ | ||
| Budget: &Budget{ | ||
| ID: Ptr("1"), | ||
| BudgetName: Ptr("Updated Budget"), | ||
| }, | ||
| } | ||
| if !cmp.Equal(budget, want) { | ||
| t.Errorf("Billing.UpdateOrganizationBudget returned %+v, want %+v", budget, want) | ||
| } | ||
| const methodName = "UpdateOrganizationBudget" | ||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| got, resp, err := client.Billing.UpdateOrganizationBudget(ctx, "o", "1", input) | ||
| if got != nil { | ||
| t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
| } | ||
| return resp, err | ||
| }) | ||
| } | ||
|
|
||
| func TestBillingService_DeleteOrganizationBudget(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| mux.HandleFunc("/organizations/o/settings/billing/budgets/1", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "DELETE") | ||
| w.WriteHeader(http.StatusNoContent) | ||
| }) | ||
|
|
||
| ctx := t.Context() | ||
| _, err := client.Billing.DeleteOrganizationBudget(ctx, "o", "1") | ||
| if err != nil { | ||
| t.Errorf("Billing.DeleteOrganizationBudget returned error: %v", err) | ||
| } | ||
| const methodName = "DeleteOrganizationBudget" | ||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| return client.Billing.DeleteOrganizationBudget(ctx, "o", "1") | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Response schema of Budget
{ "type": "object", "properties": { "id": { "type": "string", "description": "ID of the budget." }, "budget_scope": { "type": "string", "description": "The type of scope for the budget", "enum": [ "enterprise", "organization", "repository", "cost_center" ], "examples": [ "enterprise" ] }, "budget_entity_name": { "type": "string", "description": "The name of the entity to apply the budget to", "examples": [ "octocat/hello-world" ] }, "budget_amount": { "type": "integer", "description": "The budget amount in whole dollars. For license-based products, this represents the number of licenses." }, "prevent_further_usage": { "type": "boolean", "description": "Whether to prevent additional spending once the budget is exceeded", "examples": [ true ] }, "budget_product_sku": { "type": "string", "description": "A single product or sku to apply the budget to.", "examples": [ "actions_linux" ] }, "budget_type": { "type": "string", "description": "The type of pricing for the budget", "enum": [ "ProductPricing", "SkuPricing" ], "examples": [ "ProductPricing" ] }, "budget_alerting": { "type": "object", "properties": { "will_alert": { "type": "boolean", "description": "Whether alerts are enabled for this budget", "examples": [ true ] }, "alert_recipients": { "type": "array", "items": { "type": "string" }, "description": "Array of user login names who will receive alerts", "examples": [ "mona", "lisa" ] } } } }, "required": [ "id", "budget_amount", "prevent_further_usage", "budget_product_sku", "budget_type", "budget_alerting", "budget_scope", "budget_entity_name" ] }it has few missing items -
budget_scope,budget_entity_name,budget_amount,prevent_further_usage,budget_product_sku.