Skip to content

Commit f0f6761

Browse files
Add active committers API implementation (#2208)
1 parent c6b75c1 commit f0f6761

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed

github/billing.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ type StorageBilling struct {
4444
EstimatedStorageForMonth int `json:"estimated_storage_for_month"`
4545
}
4646

47+
// ActiveCommitters represents the total active committers across all repositories in an Organization.
48+
type ActiveCommitters struct {
49+
TotalAdvancedSecurityCommitters int `json:"total_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+
4766
// GetActionsBillingOrg returns the summary of the free and paid GitHub Actions minutes used for an Org.
4867
//
4968
// 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) (
5372
if err != nil {
5473
return nil, nil, err
5574
}
75+
5676
actionsOrgBilling := new(ActionBilling)
5777
resp, err := s.client.Do(ctx, req, actionsOrgBilling)
78+
if err != nil {
79+
return nil, resp, err
80+
}
81+
5882
return actionsOrgBilling, resp, err
5983
}
6084

@@ -67,8 +91,13 @@ func (s *BillingService) GetPackagesBillingOrg(ctx context.Context, org string)
6791
if err != nil {
6892
return nil, nil, err
6993
}
94+
7095
packagesOrgBilling := new(PackageBilling)
7196
resp, err := s.client.Do(ctx, req, packagesOrgBilling)
97+
if err != nil {
98+
return nil, resp, err
99+
}
100+
72101
return packagesOrgBilling, resp, err
73102
}
74103

@@ -82,11 +111,35 @@ func (s *BillingService) GetStorageBillingOrg(ctx context.Context, org string) (
82111
if err != nil {
83112
return nil, nil, err
84113
}
114+
85115
storageOrgBilling := new(StorageBilling)
86116
resp, err := s.client.Do(ctx, req, storageOrgBilling)
117+
if err != nil {
118+
return nil, resp, err
119+
}
120+
87121
return storageOrgBilling, resp, err
88122
}
89123

124+
// GetAdvancedSecurityActiveCommittersOrg returns the GitHub Advanced Security active committers for an organization per repository.
125+
//
126+
// GitHub API docs: https://docs.github.com/en/rest/reference/billing#get-github-advanced-security-active-committers-for-an-organization
127+
func (s *BillingService) GetAdvancedSecurityActiveCommittersOrg(ctx context.Context, org string) (*ActiveCommitters, *Response, error) {
128+
u := fmt.Sprintf("orgs/%v/settings/billing/advanced-security", org)
129+
req, err := s.client.NewRequest("GET", u, nil)
130+
if err != nil {
131+
return nil, nil, err
132+
}
133+
134+
activeOrgCommitters := new(ActiveCommitters)
135+
resp, err := s.client.Do(ctx, req, activeOrgCommitters)
136+
if err != nil {
137+
return nil, resp, err
138+
}
139+
140+
return activeOrgCommitters, resp, err
141+
}
142+
90143
// GetActionsBillingUser returns the summary of the free and paid GitHub Actions minutes used for a user.
91144
//
92145
// 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)
96149
if err != nil {
97150
return nil, nil, err
98151
}
152+
99153
actionsUserBilling := new(ActionBilling)
100154
resp, err := s.client.Do(ctx, req, actionsUserBilling)
155+
if err != nil {
156+
return nil, resp, err
157+
}
158+
101159
return actionsUserBilling, resp, err
102160
}
103161

@@ -110,8 +168,13 @@ func (s *BillingService) GetPackagesBillingUser(ctx context.Context, user string
110168
if err != nil {
111169
return nil, nil, err
112170
}
171+
113172
packagesUserBilling := new(PackageBilling)
114173
resp, err := s.client.Do(ctx, req, packagesUserBilling)
174+
if err != nil {
175+
return nil, resp, err
176+
}
177+
115178
return packagesUserBilling, resp, err
116179
}
117180

@@ -125,7 +188,12 @@ func (s *BillingService) GetStorageBillingUser(ctx context.Context, user string)
125188
if err != nil {
126189
return nil, nil, err
127190
}
191+
128192
storageUserBilling := new(StorageBilling)
129193
resp, err := s.client.Do(ctx, req, storageUserBilling)
194+
if err != nil {
195+
return nil, resp, err
196+
}
197+
130198
return storageUserBilling, resp, err
131199
}

github/billing_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func TestBillingService_GetActionsBillingOrg(t *testing.T) {
5757
_, _, err = client.Billing.GetActionsBillingOrg(ctx, "\n")
5858
return err
5959
})
60+
61+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
62+
got, resp, err := client.Billing.GetActionsBillingOrg(ctx, "o")
63+
if got != nil {
64+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
65+
}
66+
return resp, err
67+
})
6068
}
6169

6270
func TestBillingService_GetActionsBillingOrg_invalidOrg(t *testing.T) {
@@ -101,6 +109,14 @@ func TestBillingService_GetPackagesBillingOrg(t *testing.T) {
101109
_, _, err = client.Billing.GetPackagesBillingOrg(ctx, "\n")
102110
return err
103111
})
112+
113+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
114+
got, resp, err := client.Billing.GetPackagesBillingOrg(ctx, "o")
115+
if got != nil {
116+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
117+
}
118+
return resp, err
119+
})
104120
}
105121

106122
func TestBillingService_GetPackagesBillingOrg_invalidOrg(t *testing.T) {
@@ -145,6 +161,14 @@ func TestBillingService_GetStorageBillingOrg(t *testing.T) {
145161
_, _, err = client.Billing.GetStorageBillingOrg(ctx, "\n")
146162
return err
147163
})
164+
165+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
166+
got, resp, err := client.Billing.GetStorageBillingOrg(ctx, "o")
167+
if got != nil {
168+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
169+
}
170+
return resp, err
171+
})
148172
}
149173

150174
func TestBillingService_GetStorageBillingOrg_invalidOrg(t *testing.T) {
@@ -199,6 +223,14 @@ func TestBillingService_GetActionsBillingUser(t *testing.T) {
199223
_, _, err = client.Billing.GetActionsBillingOrg(ctx, "\n")
200224
return err
201225
})
226+
227+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
228+
got, resp, err := client.Billing.GetActionsBillingUser(ctx, "o")
229+
if got != nil {
230+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
231+
}
232+
return resp, err
233+
})
202234
}
203235

204236
func TestBillingService_GetActionsBillingUser_invalidUser(t *testing.T) {
@@ -243,6 +275,14 @@ func TestBillingService_GetPackagesBillingUser(t *testing.T) {
243275
_, _, err = client.Billing.GetPackagesBillingUser(ctx, "\n")
244276
return err
245277
})
278+
279+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
280+
got, resp, err := client.Billing.GetPackagesBillingUser(ctx, "o")
281+
if got != nil {
282+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
283+
}
284+
return resp, err
285+
})
246286
}
247287

248288
func TestBillingService_GetPackagesBillingUser_invalidUser(t *testing.T) {
@@ -287,6 +327,14 @@ func TestBillingService_GetStorageBillingUser(t *testing.T) {
287327
_, _, err = client.Billing.GetStorageBillingUser(ctx, "\n")
288328
return err
289329
})
330+
331+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
332+
got, resp, err := client.Billing.GetStorageBillingUser(ctx, "o")
333+
if got != nil {
334+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
335+
}
336+
return resp, err
337+
})
290338
}
291339

292340
func TestBillingService_GetStorageBillingUser_invalidUser(t *testing.T) {
@@ -379,3 +427,75 @@ func TestStorageBilling_Marshal(t *testing.T) {
379427

380428
testJSONMarshal(t, u, want)
381429
}
430+
431+
func TestBillingService_GetAdvancedSecurityActiveCommittersOrg(t *testing.T) {
432+
client, mux, _, teardown := setup()
433+
defer teardown()
434+
435+
mux.HandleFunc("/orgs/o/settings/billing/advanced-security", func(w http.ResponseWriter, r *http.Request) {
436+
testMethod(t, r, "GET")
437+
fmt.Fprint(w, `{
438+
"total_advanced_security_committers": 2,
439+
"repositories": [
440+
{
441+
"name": "octocat-org/Hello-World",
442+
"advanced_security_committers": 2,
443+
"advanced_security_committers_breakdown": [
444+
{
445+
"user_login": "octokitten",
446+
"last_pushed_date": "2021-10-25"
447+
}
448+
]
449+
}
450+
]
451+
}`)
452+
})
453+
454+
ctx := context.Background()
455+
hook, _, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "o")
456+
if err != nil {
457+
t.Errorf("Billing.GetAdvancedSecurityActiveCommittersOrg returned error: %v", err)
458+
}
459+
460+
want := &ActiveCommitters{
461+
TotalAdvancedSecurityCommitters: 2,
462+
Repositories: []*RepositoryActiveCommitters{
463+
{
464+
Name: String("octocat-org/Hello-World"),
465+
AdvancedSecurityCommitters: Int(2),
466+
AdvancedSecurityCommittersBreakdown: []*AdvancedSecurityCommittersBreakdown{
467+
{
468+
UserLogin: String("octokitten"),
469+
LastPushedDate: String("2021-10-25"),
470+
},
471+
},
472+
},
473+
},
474+
}
475+
if !cmp.Equal(hook, want) {
476+
t.Errorf("Billing.GetAdvancedSecurityActiveCommittersOrg returned %+v, want %+v", hook, want)
477+
}
478+
479+
const methodName = "GetAdvancedSecurityActiveCommittersOrg"
480+
testBadOptions(t, methodName, func() (err error) {
481+
_, _, err = client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "\n")
482+
return err
483+
})
484+
485+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
486+
got, resp, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "o")
487+
if got != nil {
488+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
489+
}
490+
return resp, err
491+
})
492+
}
493+
494+
func TestBillingService_GetAdvancedSecurityActiveCommittersOrg_invalidOrg(t *testing.T) {
495+
client, _, _, teardown := setup()
496+
defer teardown()
497+
498+
ctx := context.Background()
499+
_, _, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "%")
500+
testURLParseError(t, err)
501+
}

github/github-accessors.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)