From 760570bfd908115bdd7cbfd086e8093485050b70 Mon Sep 17 00:00:00 2001 From: Jay2006sawant Date: Mon, 15 Jun 2026 15:37:52 +0530 Subject: [PATCH] fix(robot): handle empty permissions in robot view Fixes #997 Signed-off-by: Jay2006sawant --- pkg/views/robot/view/view.go | 14 +++++++-- pkg/views/robot/view/view_test.go | 49 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 pkg/views/robot/view/view_test.go diff --git a/pkg/views/robot/view/view.go b/pkg/views/robot/view/view.go index 444c83587..1c1e6b1ab 100644 --- a/pkg/views/robot/view/view.go +++ b/pkg/views/robot/view/view.go @@ -87,7 +87,8 @@ func ViewRobot(robot *models.Robot) { enabledStatus = views.GreenStyle.Render("Enabled") } - TotalPermissions := strconv.FormatInt(int64(len(robot.Permissions[0].Access)), 10) + totalPermissions, namespace := robotPermissionSummary(robot) + TotalPermissions := strconv.FormatInt(int64(totalPermissions), 10) if robot.ExpiresAt == -1 { expires = "Never" @@ -127,7 +128,7 @@ func ViewRobot(robot *models.Robot) { permissionsColumns = projectPermissionsColumns resourceStrings = projectResourceStrings systemLevel = false - fmt.Printf("\n%s\n\n", views.BoldStyle.Render(fmt.Sprintf("System-level robot with access across projects: %s", robot.Permissions[0].Namespace))) + fmt.Printf("\n%s\n\n", views.BoldStyle.Render(fmt.Sprintf("System-level robot with access across projects: %s", namespace))) } var permissionRows []table.Row @@ -216,6 +217,15 @@ func ViewRobot(robot *models.Robot) { } } +func robotPermissionSummary(robot *models.Robot) (permissionCount int, namespace string) { + namespace = "--" + if len(robot.Permissions) == 0 || robot.Permissions[0] == nil { + return 0, namespace + } + + return len(robot.Permissions[0].Access), robot.Permissions[0].Namespace +} + func createProjectPermissionRows(perm *models.RobotPermission, availablePerms map[string][]string) []table.Row { var rows []table.Row resActs := map[string][]string{} diff --git a/pkg/views/robot/view/view_test.go b/pkg/views/robot/view/view_test.go new file mode 100644 index 000000000..dd767fa36 --- /dev/null +++ b/pkg/views/robot/view/view_test.go @@ -0,0 +1,49 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package view + +import ( + "testing" + + "github.com/goharbor/go-client/pkg/sdk/v2.0/models" + "github.com/stretchr/testify/assert" +) + +func TestRobotPermissionSummaryWithoutPermissions(t *testing.T) { + robot := &models.Robot{} + + permissionCount, namespace := robotPermissionSummary(robot) + + assert.Equal(t, 0, permissionCount) + assert.Equal(t, "--", namespace) +} + +func TestRobotPermissionSummaryWithPermissions(t *testing.T) { + robot := &models.Robot{ + Permissions: []*models.RobotPermission{ + { + Namespace: "demo", + Access: []*models.Access{ + {Resource: "repository", Action: "pull"}, + {Resource: "artifact", Action: "read"}, + }, + }, + }, + } + + permissionCount, namespace := robotPermissionSummary(robot) + + assert.Equal(t, 2, permissionCount) + assert.Equal(t, "demo", namespace) +}