From 46f1127c50024fd92c7bb10992e1baaf607ee5e7 Mon Sep 17 00:00:00 2001 From: Felix Luthman <34520175+felixlut@users.noreply.github.com> Date: Sat, 21 Sep 2024 21:25:21 +0200 Subject: [PATCH 1/3] chore: Change golangci-lint output format due to deprecation (#3279) --- script/lint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/lint.sh b/script/lint.sh index ccd0cf1150..1434cc6904 100755 --- a/script/lint.sh +++ b/script/lint.sh @@ -33,7 +33,7 @@ for dir in $MOD_DIRS; do cd "$dir" # github actions output when running in an action if [ -n "$GITHUB_ACTIONS" ]; then - "$BIN"/golangci-lint run --path-prefix "$dir" --out-format github-actions + "$BIN"/golangci-lint run --path-prefix "$dir" --out-format colored-line-number else "$BIN"/golangci-lint run --path-prefix "$dir" fi From 62bc855b9d7bf1aa28b40afacee1892e857a8d8c Mon Sep 17 00:00:00 2001 From: felixlut Date: Sun, 22 Sep 2024 16:00:36 +0200 Subject: [PATCH 2/3] support getting an organization role --- github/orgs_custom_roles.go | 23 ++++++++ github/orgs_custom_roles_test.go | 99 ++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/github/orgs_custom_roles.go b/github/orgs_custom_roles.go index ca0c6d7bcb..ed5f115a5a 100644 --- a/github/orgs_custom_roles.go +++ b/github/orgs_custom_roles.go @@ -87,6 +87,29 @@ func (s *OrganizationsService) ListRoles(ctx context.Context, org string) (*Orga return customRepoRoles, resp, nil } +// GetOrgRole gets an organization role in this organization. +// In order to get organization roles in an organization, the authenticated user must be an organization owner, or have access via an organization role. +// +// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-an-organization-role +// +//meta:operation GET /orgs/{org}/organization-roles/{role_id} +func (s *OrganizationsService) GetOrgRole(ctx context.Context, org string, roleID int64) (*CustomOrgRoles, *Response, error) { + u := fmt.Sprintf("orgs/%v/organization-roles/%v", org, roleID) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + resultingRole := new(CustomOrgRoles) + resp, err := s.client.Do(ctx, req, resultingRole) + if err != nil { + return nil, resp, err + } + + return resultingRole, resp, err +} + // CreateCustomOrgRole creates a custom role in this organization. // In order to create custom 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..70999de020 100644 --- a/github/orgs_custom_roles_test.go +++ b/github/orgs_custom_roles_test.go @@ -98,6 +98,105 @@ func TestOrganizationsService_ListRoles(t *testing.T) { }) } +func TestOrganizationsService_GetOrgRole(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + // Test built-in org role + mux.HandleFunc("/orgs/o/organization-roles/8132", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{ + "id": 8132, + "name": "all_repo_read", + "description": "Grants read access to all repositories in the organization.", + "permissions": [], + "created_at": ` + referenceTimeStr + `, + "updated_at": ` + referenceTimeStr + `, + "source": "Predefined", + "base_role": "read" + }`) + }) + + ctx := context.Background() + + gotBuiltInRole, _, err := client.Organizations.GetOrgRole(ctx, "o", 8132) + if err != nil { + t.Errorf("Organizations.GetOrgRole returned error: %v", err) + } + + wantBuiltInRole := &CustomOrgRoles{ + ID: Int64(8132), + Name: String("all_repo_read"), + Description: String("Grants read access to all repositories in the organization."), + Permissions: []string{}, + CreatedAt: &Timestamp{referenceTime}, + UpdatedAt: &Timestamp{referenceTime}, + Source: String("Predefined"), + BaseRole: String("read"), + } + + if !cmp.Equal(gotBuiltInRole, wantBuiltInRole) { + t.Errorf("Organizations.GetOrgRole returned %+v, want %+v", gotBuiltInRole, wantBuiltInRole) + } + + // Test custom org role + mux.HandleFunc("/orgs/o/organization-roles/123456", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{ + "id": 123456, + "name": "test-role", + "description": "test-role", + "permissions": [ + "read_organization_custom_org_role", + "read_organization_custom_repo_role", + "write_organization_custom_org_role" + ], + "created_at": ` + referenceTimeStr + `, + "updated_at": ` + referenceTimeStr + `, + "source": "Organization", + "base_role": null + }`) + }) + + gotCustomRole, _, err := client.Organizations.GetOrgRole(ctx, "o", 123456) + if err != nil { + t.Errorf("Organizations.GetOrgRole returned error: %v", err) + } + + wantCustomRole := &CustomOrgRoles{ + ID: Int64(123456), + Name: String("test-role"), + Description: String("test-role"), + Permissions: []string{ + "read_organization_custom_org_role", + "read_organization_custom_repo_role", + "write_organization_custom_org_role", + }, + CreatedAt: &Timestamp{referenceTime}, + UpdatedAt: &Timestamp{referenceTime}, + Source: String("Organization"), + BaseRole: nil, + } + + if !cmp.Equal(gotCustomRole, wantCustomRole) { + t.Errorf("Organizations.GetOrgRole returned %+v, want %+v", gotCustomRole, wantCustomRole) + } + + const methodName = "GetOrgRole" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Organizations.GetOrgRole(ctx, "\no", -8132) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Organizations.GetOrgRole(ctx, "o", 8132) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + func TestOrganizationsService_CreateCustomOrgRole(t *testing.T) { client, mux, _, teardown := setup() defer teardown() From 68caaadbf47c281059cef5fc6d95ea1ab621dd05 Mon Sep 17 00:00:00 2001 From: felixlut Date: Sun, 22 Sep 2024 16:16:25 +0200 Subject: [PATCH 3/3] formatting --- github/orgs_custom_roles_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/github/orgs_custom_roles_test.go b/github/orgs_custom_roles_test.go index 70999de020..a4268b28f8 100644 --- a/github/orgs_custom_roles_test.go +++ b/github/orgs_custom_roles_test.go @@ -110,8 +110,8 @@ func TestOrganizationsService_GetOrgRole(t *testing.T) { "name": "all_repo_read", "description": "Grants read access to all repositories in the organization.", "permissions": [], - "created_at": ` + referenceTimeStr + `, - "updated_at": ` + referenceTimeStr + `, + "created_at": `+referenceTimeStr+`, + "updated_at": `+referenceTimeStr+`, "source": "Predefined", "base_role": "read" }`) @@ -129,10 +129,10 @@ func TestOrganizationsService_GetOrgRole(t *testing.T) { Name: String("all_repo_read"), Description: String("Grants read access to all repositories in the organization."), Permissions: []string{}, - CreatedAt: &Timestamp{referenceTime}, - UpdatedAt: &Timestamp{referenceTime}, - Source: String("Predefined"), - BaseRole: String("read"), + CreatedAt: &Timestamp{referenceTime}, + UpdatedAt: &Timestamp{referenceTime}, + Source: String("Predefined"), + BaseRole: String("read"), } if !cmp.Equal(gotBuiltInRole, wantBuiltInRole) { @@ -151,8 +151,8 @@ func TestOrganizationsService_GetOrgRole(t *testing.T) { "read_organization_custom_repo_role", "write_organization_custom_org_role" ], - "created_at": ` + referenceTimeStr + `, - "updated_at": ` + referenceTimeStr + `, + "created_at": `+referenceTimeStr+`, + "updated_at": `+referenceTimeStr+`, "source": "Organization", "base_role": null }`) @@ -172,10 +172,10 @@ func TestOrganizationsService_GetOrgRole(t *testing.T) { "read_organization_custom_repo_role", "write_organization_custom_org_role", }, - CreatedAt: &Timestamp{referenceTime}, - UpdatedAt: &Timestamp{referenceTime}, - Source: String("Organization"), - BaseRole: nil, + CreatedAt: &Timestamp{referenceTime}, + UpdatedAt: &Timestamp{referenceTime}, + Source: String("Organization"), + BaseRole: nil, } if !cmp.Equal(gotCustomRole, wantCustomRole) {