Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pkg/views/robot/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{}
Expand Down
49 changes: 49 additions & 0 deletions pkg/views/robot/view/view_test.go
Original file line number Diff line number Diff line change
@@ -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)
}