-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Update security manager operations to use organization roles REST APIs #3421
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
Changes from all commits
21855f4
6e962d9
c59f0d2
d980320
1b33bfc
a524128
faf36ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,57 +7,87 @@ | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"errors" | ||
) | ||
|
||
// ListSecurityManagerTeams lists all security manager teams for an organization. | ||
// GetSecurityManagerRole retrieves the security manager role for an organization. | ||
// | ||
// GitHub API docs: https://docs.github.com/rest/orgs/security-managers#list-security-manager-teams | ||
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-all-organization-roles-for-an-organization | ||
// | ||
//meta:operation GET /orgs/{org}/security-managers | ||
func (s *OrganizationsService) ListSecurityManagerTeams(ctx context.Context, org string) ([]*Team, *Response, error) { | ||
u := fmt.Sprintf("orgs/%v/security-managers", org) | ||
|
||
req, err := s.client.NewRequest("GET", u, nil) | ||
//meta:operation GET /orgs/{org}/organization-roles | ||
func (s *OrganizationsService) GetSecurityManagerRole(ctx context.Context, org string) (*CustomOrgRoles, *Response, error) { | ||
roles, resp, err := s.ListRoles(ctx, org) | ||
if err != nil { | ||
return nil, nil, err | ||
return nil, resp, err | ||
} | ||
|
||
var teams []*Team | ||
resp, err := s.client.Do(ctx, req, &teams) | ||
for _, role := range roles.CustomRepoRoles { | ||
if *role.Name == "security_manager" { | ||
return role, resp, nil | ||
} | ||
} | ||
|
||
return nil, resp, errors.New("security manager role not found") | ||
} | ||
|
||
// ListSecurityManagerTeams lists all security manager teams for an organization. | ||
// | ||
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-all-organization-roles-for-an-organization | ||
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#list-teams-that-are-assigned-to-an-organization-role | ||
// | ||
//meta:operation GET /orgs/{org}/organization-roles | ||
//meta:operation GET /orgs/{org}/organization-roles/{role_id}/teams | ||
func (s *OrganizationsService) ListSecurityManagerTeams(ctx context.Context, org string) ([]*Team, *Response, error) { | ||
securityManagerRole, resp, err := s.GetSecurityManagerRole(ctx, org) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return teams, resp, nil | ||
options := &ListOptions{PerPage: 100} | ||
securityManagerTeams := make([]*Team, 0) | ||
for { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this change, this will be the first endpoint that handles pagination within the call itself, whereas all other endpoints in this repo expect the caller to handle pagination (which also typically involves checking for rate limit errors). Honestly, I'm not seeing the need here to introduce pagination in this endpoint. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @gmlewis! The reason I did that is because the previous API did not support pagination, so I didn't want to change the contract here. My goal was for these operations to continue to work the same way they did previously so consumers of the library would not face any interruptions from these when updating to newer versions of the library. But based on other feedback here as well, it sounds like it might be more desirable to keep the library closer to a 1:1 with the GitHub APIs, so I will mark them as deprecated instead. |
||
teams, resp, err := s.ListTeamsAssignedToOrgRole(ctx, org, securityManagerRole.GetID(), options) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
securityManagerTeams = append(securityManagerTeams, teams...) | ||
if resp.NextPage == 0 { | ||
return securityManagerTeams, resp, nil | ||
} | ||
|
||
options.Page = resp.NextPage | ||
} | ||
} | ||
|
||
// AddSecurityManagerTeam adds a team to the list of security managers for an organization. | ||
// | ||
// GitHub API docs: https://docs.github.com/rest/orgs/security-managers#add-a-security-manager-team | ||
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#assign-an-organization-role-to-a-team | ||
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-all-organization-roles-for-an-organization | ||
// | ||
//meta:operation PUT /orgs/{org}/security-managers/teams/{team_slug} | ||
//meta:operation GET /orgs/{org}/organization-roles | ||
//meta:operation PUT /orgs/{org}/organization-roles/teams/{team_slug}/{role_id} | ||
func (s *OrganizationsService) AddSecurityManagerTeam(ctx context.Context, org, team string) (*Response, error) { | ||
u := fmt.Sprintf("orgs/%v/security-managers/teams/%v", org, team) | ||
req, err := s.client.NewRequest("PUT", u, nil) | ||
securityManagerRole, resp, err := s.GetSecurityManagerRole(ctx, org) | ||
if err != nil { | ||
return nil, err | ||
return resp, err | ||
} | ||
|
||
return s.client.Do(ctx, req, nil) | ||
return s.AssignOrgRoleToTeam(ctx, org, team, securityManagerRole.GetID()) | ||
} | ||
|
||
// RemoveSecurityManagerTeam removes a team from the list of security managers for an organization. | ||
// | ||
// GitHub API docs: https://docs.github.com/rest/orgs/security-managers#remove-a-security-manager-team | ||
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-all-organization-roles-for-an-organization | ||
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#remove-an-organization-role-from-a-team | ||
// | ||
//meta:operation DELETE /orgs/{org}/security-managers/teams/{team_slug} | ||
//meta:operation GET /orgs/{org}/organization-roles | ||
//meta:operation DELETE /orgs/{org}/organization-roles/teams/{team_slug}/{role_id} | ||
func (s *OrganizationsService) RemoveSecurityManagerTeam(ctx context.Context, org, team string) (*Response, error) { | ||
u := fmt.Sprintf("orgs/%v/security-managers/teams/%v", org, team) | ||
req, err := s.client.NewRequest("DELETE", u, nil) | ||
securityManagerRole, resp, err := s.GetSecurityManagerRole(ctx, org) | ||
if err != nil { | ||
return nil, err | ||
return resp, err | ||
} | ||
|
||
return s.client.Do(ctx, req, nil) | ||
return s.RemoveOrgRoleFromTeam(ctx, org, team, securityManagerRole.GetID()) | ||
} |
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.
Please add a unit test that covers this error.