diff --git a/github/billing.go b/github/billing.go index 190140a4cdb..12a79fa60ae 100644 --- a/github/billing.go +++ b/github/billing.go @@ -44,6 +44,25 @@ type StorageBilling struct { EstimatedStorageForMonth int `json:"estimated_storage_for_month"` } +// ActiveCommitters represents the total active committers across all repositories in an Organization. +type ActiveCommitters struct { + TotalAdvancedSecurityCommitters int `json:"total_advanced_security_committers"` + Repositories []*RepositoryActiveCommitters `json:"repositories,omitempty"` +} + +// RepositoryActiveCommitters represents active committers on each repository. +type RepositoryActiveCommitters struct { + Name *string `json:"name,omitempty"` + AdvancedSecurityCommitters *int `json:"advanced_security_committers,omitempty"` + AdvancedSecurityCommittersBreakdown []*AdvancedSecurityCommittersBreakdown `json:"advanced_security_committers_breakdown,omitempty"` +} + +// AdvancedSecurityCommittersBreakdown represents the user activity breakdown for ActiveCommitters. +type AdvancedSecurityCommittersBreakdown struct { + UserLogin *string `json:"user_login,omitempty"` + LastPushedDate *string `json:"last_pushed_date,omitempty"` +} + // GetActionsBillingOrg returns the summary of the free and paid GitHub Actions minutes used for an Org. // // GitHub API docs: https://docs.github.com/en/rest/reference/billing#get-github-actions-billing-for-an-organization @@ -53,8 +72,13 @@ func (s *BillingService) GetActionsBillingOrg(ctx context.Context, org string) ( if err != nil { return nil, nil, err } + actionsOrgBilling := new(ActionBilling) resp, err := s.client.Do(ctx, req, actionsOrgBilling) + if err != nil { + return nil, resp, err + } + return actionsOrgBilling, resp, err } @@ -67,8 +91,13 @@ func (s *BillingService) GetPackagesBillingOrg(ctx context.Context, org string) if err != nil { return nil, nil, err } + packagesOrgBilling := new(PackageBilling) resp, err := s.client.Do(ctx, req, packagesOrgBilling) + if err != nil { + return nil, resp, err + } + return packagesOrgBilling, resp, err } @@ -82,11 +111,35 @@ func (s *BillingService) GetStorageBillingOrg(ctx context.Context, org string) ( if err != nil { return nil, nil, err } + storageOrgBilling := new(StorageBilling) resp, err := s.client.Do(ctx, req, storageOrgBilling) + if err != nil { + return nil, resp, err + } + return storageOrgBilling, resp, err } +// GetAdvancedSecurityActiveCommittersOrg returns the GitHub Advanced Security active committers for an organization per repository. +// +// GitHub API docs: https://docs.github.com/en/rest/reference/billing#get-github-advanced-security-active-committers-for-an-organization +func (s *BillingService) GetAdvancedSecurityActiveCommittersOrg(ctx context.Context, org string) (*ActiveCommitters, *Response, error) { + u := fmt.Sprintf("orgs/%v/settings/billing/advanced-security", org) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + activeOrgCommitters := new(ActiveCommitters) + resp, err := s.client.Do(ctx, req, activeOrgCommitters) + if err != nil { + return nil, resp, err + } + + return activeOrgCommitters, resp, err +} + // GetActionsBillingUser returns the summary of the free and paid GitHub Actions minutes used for a user. // // GitHub API docs: https://docs.github.com/en/rest/reference/billing#get-github-actions-billing-for-a-user @@ -96,8 +149,13 @@ func (s *BillingService) GetActionsBillingUser(ctx context.Context, user string) if err != nil { return nil, nil, err } + actionsUserBilling := new(ActionBilling) resp, err := s.client.Do(ctx, req, actionsUserBilling) + if err != nil { + return nil, resp, err + } + return actionsUserBilling, resp, err } @@ -110,8 +168,13 @@ func (s *BillingService) GetPackagesBillingUser(ctx context.Context, user string if err != nil { return nil, nil, err } + packagesUserBilling := new(PackageBilling) resp, err := s.client.Do(ctx, req, packagesUserBilling) + if err != nil { + return nil, resp, err + } + return packagesUserBilling, resp, err } @@ -125,7 +188,12 @@ func (s *BillingService) GetStorageBillingUser(ctx context.Context, user string) if err != nil { return nil, nil, err } + storageUserBilling := new(StorageBilling) resp, err := s.client.Do(ctx, req, storageUserBilling) + if err != nil { + return nil, resp, err + } + return storageUserBilling, resp, err } diff --git a/github/billing_test.go b/github/billing_test.go index 2693e68a769..11acd623091 100644 --- a/github/billing_test.go +++ b/github/billing_test.go @@ -57,6 +57,14 @@ func TestBillingService_GetActionsBillingOrg(t *testing.T) { _, _, err = client.Billing.GetActionsBillingOrg(ctx, "\n") return err }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Billing.GetActionsBillingOrg(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) } func TestBillingService_GetActionsBillingOrg_invalidOrg(t *testing.T) { @@ -101,6 +109,14 @@ func TestBillingService_GetPackagesBillingOrg(t *testing.T) { _, _, err = client.Billing.GetPackagesBillingOrg(ctx, "\n") return err }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Billing.GetPackagesBillingOrg(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) } func TestBillingService_GetPackagesBillingOrg_invalidOrg(t *testing.T) { @@ -145,6 +161,14 @@ func TestBillingService_GetStorageBillingOrg(t *testing.T) { _, _, err = client.Billing.GetStorageBillingOrg(ctx, "\n") return err }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Billing.GetStorageBillingOrg(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) } func TestBillingService_GetStorageBillingOrg_invalidOrg(t *testing.T) { @@ -199,6 +223,14 @@ func TestBillingService_GetActionsBillingUser(t *testing.T) { _, _, err = client.Billing.GetActionsBillingOrg(ctx, "\n") return err }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Billing.GetActionsBillingUser(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) } func TestBillingService_GetActionsBillingUser_invalidUser(t *testing.T) { @@ -243,6 +275,14 @@ func TestBillingService_GetPackagesBillingUser(t *testing.T) { _, _, err = client.Billing.GetPackagesBillingUser(ctx, "\n") return err }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Billing.GetPackagesBillingUser(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) } func TestBillingService_GetPackagesBillingUser_invalidUser(t *testing.T) { @@ -287,6 +327,14 @@ func TestBillingService_GetStorageBillingUser(t *testing.T) { _, _, err = client.Billing.GetStorageBillingUser(ctx, "\n") return err }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Billing.GetStorageBillingUser(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) } func TestBillingService_GetStorageBillingUser_invalidUser(t *testing.T) { @@ -379,3 +427,75 @@ func TestStorageBilling_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } + +func TestBillingService_GetAdvancedSecurityActiveCommittersOrg(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/settings/billing/advanced-security", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{ + "total_advanced_security_committers": 2, + "repositories": [ + { + "name": "octocat-org/Hello-World", + "advanced_security_committers": 2, + "advanced_security_committers_breakdown": [ + { + "user_login": "octokitten", + "last_pushed_date": "2021-10-25" + } + ] + } + ] +}`) + }) + + ctx := context.Background() + hook, _, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "o") + if err != nil { + t.Errorf("Billing.GetAdvancedSecurityActiveCommittersOrg returned error: %v", err) + } + + want := &ActiveCommitters{ + TotalAdvancedSecurityCommitters: 2, + Repositories: []*RepositoryActiveCommitters{ + { + Name: String("octocat-org/Hello-World"), + AdvancedSecurityCommitters: Int(2), + AdvancedSecurityCommittersBreakdown: []*AdvancedSecurityCommittersBreakdown{ + { + UserLogin: String("octokitten"), + LastPushedDate: String("2021-10-25"), + }, + }, + }, + }, + } + if !cmp.Equal(hook, want) { + t.Errorf("Billing.GetAdvancedSecurityActiveCommittersOrg returned %+v, want %+v", hook, want) + } + + const methodName = "GetAdvancedSecurityActiveCommittersOrg" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestBillingService_GetAdvancedSecurityActiveCommittersOrg_invalidOrg(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "%") + testURLParseError(t, err) +} diff --git a/github/github-accessors.go b/github/github-accessors.go index 7bd870167e6..b6a846eff38 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -156,6 +156,22 @@ func (a *AdvancedSecurity) GetStatus() string { return *a.Status } +// GetLastPushedDate returns the LastPushedDate field if it's non-nil, zero value otherwise. +func (a *AdvancedSecurityCommittersBreakdown) GetLastPushedDate() string { + if a == nil || a.LastPushedDate == nil { + return "" + } + return *a.LastPushedDate +} + +// GetUserLogin returns the UserLogin field if it's non-nil, zero value otherwise. +func (a *AdvancedSecurityCommittersBreakdown) GetUserLogin() string { + if a == nil || a.UserLogin == nil { + return "" + } + return *a.UserLogin +} + // GetClosedAt returns the ClosedAt field if it's non-nil, zero value otherwise. func (a *Alert) GetClosedAt() Timestamp { if a == nil || a.ClosedAt == nil { @@ -13852,6 +13868,22 @@ func (r *Repository) GetWatchersCount() int { return *r.WatchersCount } +// GetAdvancedSecurityCommitters returns the AdvancedSecurityCommitters field if it's non-nil, zero value otherwise. +func (r *RepositoryActiveCommitters) GetAdvancedSecurityCommitters() int { + if r == nil || r.AdvancedSecurityCommitters == nil { + return 0 + } + return *r.AdvancedSecurityCommitters +} + +// GetName returns the Name field if it's non-nil, zero value otherwise. +func (r *RepositoryActiveCommitters) GetName() string { + if r == nil || r.Name == nil { + return "" + } + return *r.Name +} + // GetBody returns the Body field if it's non-nil, zero value otherwise. func (r *RepositoryComment) GetBody() string { if r == nil || r.Body == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index bbdff0ad3f5..6cc5dfbfaab 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -163,6 +163,26 @@ func TestAdvancedSecurity_GetStatus(tt *testing.T) { a.GetStatus() } +func TestAdvancedSecurityCommittersBreakdown_GetLastPushedDate(tt *testing.T) { + var zeroValue string + a := &AdvancedSecurityCommittersBreakdown{LastPushedDate: &zeroValue} + a.GetLastPushedDate() + a = &AdvancedSecurityCommittersBreakdown{} + a.GetLastPushedDate() + a = nil + a.GetLastPushedDate() +} + +func TestAdvancedSecurityCommittersBreakdown_GetUserLogin(tt *testing.T) { + var zeroValue string + a := &AdvancedSecurityCommittersBreakdown{UserLogin: &zeroValue} + a.GetUserLogin() + a = &AdvancedSecurityCommittersBreakdown{} + a.GetUserLogin() + a = nil + a.GetUserLogin() +} + func TestAlert_GetClosedAt(tt *testing.T) { var zeroValue Timestamp a := &Alert{ClosedAt: &zeroValue} @@ -16176,6 +16196,26 @@ func TestRepository_GetWatchersCount(tt *testing.T) { r.GetWatchersCount() } +func TestRepositoryActiveCommitters_GetAdvancedSecurityCommitters(tt *testing.T) { + var zeroValue int + r := &RepositoryActiveCommitters{AdvancedSecurityCommitters: &zeroValue} + r.GetAdvancedSecurityCommitters() + r = &RepositoryActiveCommitters{} + r.GetAdvancedSecurityCommitters() + r = nil + r.GetAdvancedSecurityCommitters() +} + +func TestRepositoryActiveCommitters_GetName(tt *testing.T) { + var zeroValue string + r := &RepositoryActiveCommitters{Name: &zeroValue} + r.GetName() + r = &RepositoryActiveCommitters{} + r.GetName() + r = nil + r.GetName() +} + func TestRepositoryComment_GetBody(tt *testing.T) { var zeroValue string r := &RepositoryComment{Body: &zeroValue}