Skip to content

Commit 3bfe921

Browse files
authored
Add support for security manager endpoints (#2530)
Fixes: #2529.
1 parent 66a053a commit 3bfe921

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed

github/orgs_security_managers.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2022 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+
// ListSecurityManagerTeams lists all security manager teams for an organization.
14+
//
15+
// GitHub API docs: https://docs.github.com/en/rest/orgs/security-managers#list-security-manager-teams
16+
func (s *OrganizationsService) ListSecurityManagerTeams(ctx context.Context, org string) ([]*Team, *Response, error) {
17+
u := fmt.Sprintf("orgs/%v/security-managers", org)
18+
19+
req, err := s.client.NewRequest("GET", u, nil)
20+
if err != nil {
21+
return nil, nil, err
22+
}
23+
24+
var teams []*Team
25+
resp, err := s.client.Do(ctx, req, &teams)
26+
if err != nil {
27+
return nil, resp, err
28+
}
29+
30+
return teams, resp, nil
31+
}
32+
33+
// AddSecurityManagerTeam adds a team to the list of security managers for an organization.
34+
//
35+
// GitHub API docs: https://docs.github.com/en/rest/orgs/security-managers#add-a-security-manager-team
36+
func (s *OrganizationsService) AddSecurityManagerTeam(ctx context.Context, org, team string) (*Response, error) {
37+
u := fmt.Sprintf("orgs/%v/security-managers/teams/%v", org, team)
38+
req, err := s.client.NewRequest("PUT", u, nil)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
return s.client.Do(ctx, req, nil)
44+
}
45+
46+
// RemoveSecurityManagerTeam removes a team from the list of security managers for an organization.
47+
//
48+
// GitHub API docs: https://docs.github.com/en/rest/orgs/security-managers#remove-a-security-manager-team
49+
func (s *OrganizationsService) RemoveSecurityManagerTeam(ctx context.Context, org, team string) (*Response, error) {
50+
u := fmt.Sprintf("orgs/%v/security-managers/teams/%v", org, team)
51+
req, err := s.client.NewRequest("DELETE", u, nil)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
return s.client.Do(ctx, req, nil)
57+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright 2022 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+
"net/http"
12+
"testing"
13+
14+
"github.com/google/go-cmp/cmp"
15+
)
16+
17+
func TestOrganizationsService_ListSecurityManagerTeams(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/orgs/o/security-managers", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
fmt.Fprint(w, `[{"id":1}]`)
24+
})
25+
26+
ctx := context.Background()
27+
teams, _, err := client.Organizations.ListSecurityManagerTeams(ctx, "o")
28+
if err != nil {
29+
t.Errorf("Organizations.ListSecurityManagerTeams returned error: %v", err)
30+
}
31+
32+
want := []*Team{{ID: Int64(1)}}
33+
if !cmp.Equal(teams, want) {
34+
t.Errorf("Organizations.ListSecurityManagerTeams returned %+v, want %+v", teams, want)
35+
}
36+
37+
const methodName = "ListSecurityManagerTeams"
38+
testBadOptions(t, methodName, func() (err error) {
39+
_, _, err = client.Organizations.ListSecurityManagerTeams(ctx, "\n")
40+
return err
41+
})
42+
43+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
44+
got, resp, err := client.Organizations.ListSecurityManagerTeams(ctx, "o")
45+
if got != nil {
46+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
47+
}
48+
return resp, err
49+
})
50+
}
51+
52+
func TestOrganizationsService_ListSecurityManagerTeams_invalidOrg(t *testing.T) {
53+
client, _, _, teardown := setup()
54+
defer teardown()
55+
56+
ctx := context.Background()
57+
_, _, err := client.Organizations.ListSecurityManagerTeams(ctx, "%")
58+
testURLParseError(t, err)
59+
}
60+
61+
func TestOrganizationsService_AddSecurityManagerTeam(t *testing.T) {
62+
client, mux, _, teardown := setup()
63+
defer teardown()
64+
65+
mux.HandleFunc("/orgs/o/security-managers/teams/t", func(w http.ResponseWriter, r *http.Request) {
66+
testMethod(t, r, "PUT")
67+
})
68+
69+
ctx := context.Background()
70+
_, err := client.Organizations.AddSecurityManagerTeam(ctx, "o", "t")
71+
if err != nil {
72+
t.Errorf("Organizations.AddSecurityManagerTeam returned error: %v", err)
73+
}
74+
75+
const methodName = "AddSecurityManagerTeam"
76+
testBadOptions(t, methodName, func() (err error) {
77+
_, err = client.Organizations.AddSecurityManagerTeam(ctx, "\n", "\n")
78+
return err
79+
})
80+
81+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
82+
return client.Organizations.AddSecurityManagerTeam(ctx, "o", "t")
83+
})
84+
}
85+
86+
func TestOrganizationsService_AddSecurityManagerTeam_invalidOrg(t *testing.T) {
87+
client, _, _, teardown := setup()
88+
defer teardown()
89+
90+
ctx := context.Background()
91+
_, err := client.Organizations.AddSecurityManagerTeam(ctx, "%", "t")
92+
testURLParseError(t, err)
93+
}
94+
95+
func TestOrganizationsService_AddSecurityManagerTeam_invalidTeam(t *testing.T) {
96+
client, _, _, teardown := setup()
97+
defer teardown()
98+
99+
ctx := context.Background()
100+
_, err := client.Organizations.AddSecurityManagerTeam(ctx, "%", "t")
101+
testURLParseError(t, err)
102+
}
103+
104+
func TestOrganizationsService_RemoveSecurityManagerTeam(t *testing.T) {
105+
client, mux, _, teardown := setup()
106+
defer teardown()
107+
108+
mux.HandleFunc("/orgs/o/security-managers/teams/t", func(w http.ResponseWriter, r *http.Request) {
109+
testMethod(t, r, "DELETE")
110+
})
111+
112+
ctx := context.Background()
113+
_, err := client.Organizations.RemoveSecurityManagerTeam(ctx, "o", "t")
114+
if err != nil {
115+
t.Errorf("Organizations.RemoveSecurityManagerTeam returned error: %v", err)
116+
}
117+
118+
const methodName = "RemoveSecurityManagerTeam"
119+
testBadOptions(t, methodName, func() (err error) {
120+
_, err = client.Organizations.RemoveSecurityManagerTeam(ctx, "\n", "\n")
121+
return err
122+
})
123+
124+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
125+
return client.Organizations.RemoveSecurityManagerTeam(ctx, "o", "t")
126+
})
127+
}
128+
129+
func TestOrganizationsService_RemoveSecurityManagerTeam_invalidOrg(t *testing.T) {
130+
client, _, _, teardown := setup()
131+
defer teardown()
132+
133+
ctx := context.Background()
134+
_, err := client.Organizations.RemoveSecurityManagerTeam(ctx, "%", "t")
135+
testURLParseError(t, err)
136+
}
137+
138+
func TestOrganizationsService_RemoveSecurityManagerTeam_invalidTeam(t *testing.T) {
139+
client, _, _, teardown := setup()
140+
defer teardown()
141+
142+
ctx := context.Background()
143+
_, err := client.Organizations.RemoveSecurityManagerTeam(ctx, "%", "t")
144+
testURLParseError(t, err)
145+
}

0 commit comments

Comments
 (0)