Skip to content

Commit

Permalink
Add active committers API implementation (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshkumarsv authored Nov 29, 2021
1 parent c6b75c1 commit f0f6761
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 0 deletions.
68 changes: 68 additions & 0 deletions github/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
120 changes: 120 additions & 0 deletions github/billing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
32 changes: 32 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f0f6761

Please sign in to comment.