From 00a67d21bbb27b5ca0437f1211ff29f2b3672756 Mon Sep 17 00:00:00 2001 From: felixlut Date: Sat, 21 Sep 2024 18:26:36 +0200 Subject: [PATCH 1/3] implement organization role assignments for both users and teams --- github/orgs_custom_roles.go | 88 +++++++++++++++++++++++ github/orgs_custom_roles_test.go | 116 +++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) diff --git a/github/orgs_custom_roles.go b/github/orgs_custom_roles.go index ca0c6d7bcb..9829c2b14a 100644 --- a/github/orgs_custom_roles.go +++ b/github/orgs_custom_roles.go @@ -156,6 +156,94 @@ func (s *OrganizationsService) DeleteCustomOrgRole(ctx context.Context, org stri return resp, nil } +// AssignOrgRoleToTeam assigns an existing organization role to a team in this organization. +// In order to assign organization roles in an organization, the authenticated user must be an organization owner. +// +// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#assign-an-organization-role-to-a-team +// +//meta:operation PUT /orgs/{org}/organization-roles/teams/{team_slug}/{role_id} +func (s *OrganizationsService) AssignOrgRoleToTeam(ctx context.Context, org string, teamSlug string, roleID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/organization-roles/teams/%v/%v", org, teamSlug, roleID) + + req, err := s.client.NewRequest("PUT", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// RemoveOrgRoleFromTeam removes an existing organization role assignment to a team in this organization. +// In order to remove organization role assignments in an organization, the authenticated user must be an organization owner. +// +// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#remove-an-organization-role-from-a-team +// +//meta:operation DELETE /orgs/{org}/organization-roles/teams/{team_slug}/{role_id} +func (s *OrganizationsService) RemoveOrgRoleFromTeam(ctx context.Context, org string, teamSlug string, roleID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/organization-roles/teams/%v/%v", org, teamSlug, roleID) + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// AssignOrgRoleToUser assigns an existing organization role to a user in this organization. +// In order to assign organization roles in an organization, the authenticated user must be an organization owner. +// +// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#assign-an-organization-role-to-a-user +// +//meta:operation PUT /orgs/{org}/organization-roles/users/{username}/{role_id} +func (s *OrganizationsService) AssignOrgRoleToUser(ctx context.Context, org string, username string, roleID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/organization-roles/users/%v/%v", org, username, roleID) + + req, err := s.client.NewRequest("PUT", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// RemoveOrgRoleFromUser removes an existing organization role assignment to a user in this organization. +// In order to remove organization role assignments in an organization, the authenticated user must be an organization owner. +// +// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#remove-an-organization-role-from-a-user +// +//meta:operation DELETE /orgs/{org}/organization-roles/users/{username}/{role_id} +func (s *OrganizationsService) RemoveOrgRoleFromUser(ctx context.Context, org string, username string, roleID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/organization-roles/users/%v/%v", org, username, roleID) + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + // ListCustomRepoRoles lists the custom repository roles available in this organization. // In order to see custom repository roles in an organization, the authenticated user must be an organization owner. // diff --git a/github/orgs_custom_roles_test.go b/github/orgs_custom_roles_test.go index 338eac19fc..d3c06bce8b 100644 --- a/github/orgs_custom_roles_test.go +++ b/github/orgs_custom_roles_test.go @@ -212,6 +212,122 @@ func TestOrganizationsService_DeleteCustomOrgRole(t *testing.T) { }) } +func TestOrganizationsService_AssignOrgRoleToTeam(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/organization-roles/teams/t/8030", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + resp, err := client.Organizations.AssignOrgRoleToTeam(ctx, "o", "t", 8030) + if err != nil { + t.Errorf("Organization.AssignOrgRoleToTeam return error: %v", err) + } + if !cmp.Equal(resp.StatusCode, http.StatusNoContent) { + t.Errorf("Organizations.AssignOrgRoleToTeam returned status code %+v, want %+v", resp.StatusCode, http.StatusNoContent) + } + + const methodName = "AssignOrgRoleToTeam" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Organizations.AssignOrgRoleToTeam(ctx, "\no", "\nt", -8030) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Organizations.AssignOrgRoleToTeam(ctx, "o", "t", 8030) + }) +} + +func TestOrganizationsService_RemoveOrgRoleFromTeam(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/organization-roles/teams/t/8030", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + resp, err := client.Organizations.RemoveOrgRoleFromTeam(ctx, "o", "t", 8030) + if err != nil { + t.Errorf("Organization.RemoveOrgRoleFromTeam return error: %v", err) + } + if !cmp.Equal(resp.StatusCode, http.StatusNoContent) { + t.Errorf("Organizations.RemoveOrgRoleFromTeam returned status code %+v, want %+v", resp.StatusCode, http.StatusNoContent) + } + + const methodName = "RemoveOrgRoleFromTeam" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Organizations.RemoveOrgRoleFromTeam(ctx, "\no", "\nt", -8030) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Organizations.RemoveOrgRoleFromTeam(ctx, "o", "t", 8030) + }) +} + +func TestOrganizationsService_AssignOrgRoleToUser(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/organization-roles/users/t/8030", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + resp, err := client.Organizations.AssignOrgRoleToUser(ctx, "o", "t", 8030) + if err != nil { + t.Errorf("Organization.AssignOrgRoleToUser return error: %v", err) + } + if !cmp.Equal(resp.StatusCode, http.StatusNoContent) { + t.Errorf("Organizations.AssignOrgRoleToUser returned status code %+v, want %+v", resp.StatusCode, http.StatusNoContent) + } + + const methodName = "AssignOrgRoleToUser" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Organizations.AssignOrgRoleToUser(ctx, "\no", "\nt", -8030) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Organizations.AssignOrgRoleToUser(ctx, "o", "t", 8030) + }) +} + +func TestOrganizationsService_RemoveOrgRoleFromUser(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/organization-roles/users/t/8030", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + resp, err := client.Organizations.RemoveOrgRoleFromUser(ctx, "o", "t", 8030) + if err != nil { + t.Errorf("Organization.RemoveOrgRoleFromUser return error: %v", err) + } + if !cmp.Equal(resp.StatusCode, http.StatusNoContent) { + t.Errorf("Organizations.RemoveOrgRoleFromUser returned status code %+v, want %+v", resp.StatusCode, http.StatusNoContent) + } + + const methodName = "RemoveOrgRoleFromUser" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Organizations.RemoveOrgRoleFromUser(ctx, "\no", "\nt", -8030) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Organizations.RemoveOrgRoleFromUser(ctx, "o", "t", 8030) + }) +} + func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) { client, mux, _, teardown := setup() defer teardown() From 62b08f862e4c65f175395d74dbe2328bb1e732b1 Mon Sep 17 00:00:00 2001 From: Felix Luthman <34520175+felixlut@users.noreply.github.com> Date: Sun, 22 Sep 2024 10:45:43 +0200 Subject: [PATCH 2/3] Update github/orgs_custom_roles.go Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/orgs_custom_roles.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/orgs_custom_roles.go b/github/orgs_custom_roles.go index 9829c2b14a..aa094c5d78 100644 --- a/github/orgs_custom_roles.go +++ b/github/orgs_custom_roles.go @@ -162,7 +162,7 @@ func (s *OrganizationsService) DeleteCustomOrgRole(ctx context.Context, org stri // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#assign-an-organization-role-to-a-team // //meta:operation PUT /orgs/{org}/organization-roles/teams/{team_slug}/{role_id} -func (s *OrganizationsService) AssignOrgRoleToTeam(ctx context.Context, org string, teamSlug string, roleID int64) (*Response, error) { +func (s *OrganizationsService) AssignOrgRoleToTeam(ctx context.Context, org, teamSlug string, roleID int64) (*Response, error) { u := fmt.Sprintf("orgs/%v/organization-roles/teams/%v/%v", org, teamSlug, roleID) req, err := s.client.NewRequest("PUT", u, nil) From 1583b9a7afa5301fb22575e9ead30c3ae5b69a08 Mon Sep 17 00:00:00 2001 From: Felix Luthman <34520175+felixlut@users.noreply.github.com> Date: Sun, 22 Sep 2024 10:46:20 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/orgs_custom_roles.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/github/orgs_custom_roles.go b/github/orgs_custom_roles.go index aa094c5d78..9dc519d338 100644 --- a/github/orgs_custom_roles.go +++ b/github/orgs_custom_roles.go @@ -178,13 +178,13 @@ func (s *OrganizationsService) AssignOrgRoleToTeam(ctx context.Context, org, tea return resp, nil } -// RemoveOrgRoleFromTeam removes an existing organization role assignment to a team in this organization. +// RemoveOrgRoleFromTeam removes an existing organization role assignment from a team in this organization. // In order to remove organization role assignments in an organization, the authenticated user must be an organization owner. // // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#remove-an-organization-role-from-a-team // //meta:operation DELETE /orgs/{org}/organization-roles/teams/{team_slug}/{role_id} -func (s *OrganizationsService) RemoveOrgRoleFromTeam(ctx context.Context, org string, teamSlug string, roleID int64) (*Response, error) { +func (s *OrganizationsService) RemoveOrgRoleFromTeam(ctx context.Context, org, teamSlug string, roleID int64) (*Response, error) { u := fmt.Sprintf("orgs/%v/organization-roles/teams/%v/%v", org, teamSlug, roleID) req, err := s.client.NewRequest("DELETE", u, nil) @@ -206,7 +206,7 @@ func (s *OrganizationsService) RemoveOrgRoleFromTeam(ctx context.Context, org st // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#assign-an-organization-role-to-a-user // //meta:operation PUT /orgs/{org}/organization-roles/users/{username}/{role_id} -func (s *OrganizationsService) AssignOrgRoleToUser(ctx context.Context, org string, username string, roleID int64) (*Response, error) { +func (s *OrganizationsService) AssignOrgRoleToUser(ctx context.Context, org, username string, roleID int64) (*Response, error) { u := fmt.Sprintf("orgs/%v/organization-roles/users/%v/%v", org, username, roleID) req, err := s.client.NewRequest("PUT", u, nil) @@ -222,13 +222,13 @@ func (s *OrganizationsService) AssignOrgRoleToUser(ctx context.Context, org stri return resp, nil } -// RemoveOrgRoleFromUser removes an existing organization role assignment to a user in this organization. +// RemoveOrgRoleFromUser removes an existing organization role assignment from a user in this organization. // In order to remove organization role assignments in an organization, the authenticated user must be an organization owner. // // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#remove-an-organization-role-from-a-user // //meta:operation DELETE /orgs/{org}/organization-roles/users/{username}/{role_id} -func (s *OrganizationsService) RemoveOrgRoleFromUser(ctx context.Context, org string, username string, roleID int64) (*Response, error) { +func (s *OrganizationsService) RemoveOrgRoleFromUser(ctx context.Context, org, username string, roleID int64) (*Response, error) { u := fmt.Sprintf("orgs/%v/organization-roles/users/%v/%v", org, username, roleID) req, err := s.client.NewRequest("DELETE", u, nil)