-
Notifications
You must be signed in to change notification settings - Fork 90
refactor(robot): unify config loading and permission building functions #531
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
base: main
Are you sure you want to change the base?
Conversation
- Consolidate loadFromConfigFileForCreate and loadFromConfigFileForUpdate into single loadRobotConfigFromFile function with isUpdate flag - Merge buildMergedPermissions and buildMergedPermissionsForUpdate into unified buildMergedPermissions function using shared rmodel.RobotPermission type - Add backward compatibility wrapper functions for existing callers - Reduce code duplication and improve maintainability Signed-off-by: Patrick Eschenbach <[email protected]>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #531 +/- ##
=========================================
- Coverage 10.99% 6.75% -4.24%
=========================================
Files 173 264 +91
Lines 8671 15265 +6594
=========================================
+ Hits 953 1031 +78
- Misses 7612 14127 +6515
- Partials 106 107 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Pull Request Overview
This PR consolidates duplicate code in the Harbor CLI robot account functionality by unifying config loading and permission building functions. The refactor reduces code duplication while maintaining backward compatibility.
Key changes:
- Consolidates separate config loading functions into a unified
loadRobotConfigFromFile
function with anisUpdate
flag - Merges duplicate permission building logic into a single
buildMergedPermissions
function using shared types - Creates shared
RobotPermission
model to eliminate type duplication across create/update views
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.
Show a summary per file
File | Description |
---|---|
pkg/models/robot/permission.go | Introduces shared RobotPermission model to replace duplicate type definitions |
pkg/views/robot/create/view.go | Removes duplicate type definitions, uses shared model |
pkg/views/robot/update/view.go | Removes duplicate type definitions, uses shared model |
cmd/harbor/root/robot/common.go | Consolidates config loading and permission building logic into unified functions |
cmd/harbor/root/robot/create.go | Removes duplicate functions, calls unified implementations |
cmd/harbor/root/robot/update.go | Removes duplicate functions, calls unified implementations |
pkg/views/base/multiselect/model.go | Refactors multiselect UI component with improved list-based implementation |
pkg/views/project/select/view.go | Adds ProjectsList function using the refactored multiselect component |
pkg/prompt/prompt.go | Adds GetProjectNamesFromUser function for multi-project selection |
pkg/config/robot/robot.go | Updates to use shared RobotPermission model |
cmd/harbor/root/project/robot/create.go | Updates to use shared RobotPermission model |
cmd/harbor/root/project/robot/update.go | Updates to use shared RobotPermission model |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func loadRobotConfigFromFile(configFile string, permissions *[]models.Permission, projectPermissionsMap map[string][]models.Permission, isUpdate bool, createOpts *create.CreateView, updateOpts *update.UpdateView) error { |
Copilot
AI
Aug 18, 2025
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.
[nitpick] This function has too many parameters (6). Consider using a struct to group related parameters or splitting into separate functions for create and update operations to improve readability and maintainability.
Copilot uses AI. Check for mistakes.
cmd/harbor/root/robot/common.go
Outdated
Namespace: "/", | ||
Access: accessesSystem, | ||
Kind: "system", | ||
}) |
Copilot
AI
Aug 18, 2025
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.
System permissions are always added regardless of whether accessesSystem
is empty. This could create empty system permission entries. Consider checking if len(accessesSystem) > 0
before adding system permissions, similar to how it was handled in the original buildMergedPermissionsForUpdate
function.
}) | |
if len(accessesSystem) > 0 { | |
mergedPermissions = append(mergedPermissions, &rmodel.RobotPermission{ | |
Namespace: "/", | |
Access: accessesSystem, | |
Kind: "system", | |
}) | |
} |
Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <[email protected]> Signed-off-by: Patrick Eschenbach <[email protected]>
Co-authored-by: Copilot <[email protected]> Signed-off-by: Patrick Eschenbach <[email protected]>
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.
▼ Container.withExec(
┆ args: ["go", "build", "-ldflags", "-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.Version=dev\n\t\t\t\t\t\t -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GoVersion=1.24.4\n\t\t\t\t\t\t -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.BuildTime=2025-08-19T11:42:40Z\n\t\t\t\t\t\t -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GitCommit=63a3335\n\n\t\t\t\t", "-o", "/bin/harbor-cli", "/src/cmd/harbor/main.go"]
): Container! 3.3s ERROR
go: downloading golang.org/x/term v0.34.0
go: downloading golang.org/x/text v0.28.0
go: downloading golang.org/x/sys v0.35.0
# github.com/goharbor/harbor-cli/pkg/views/base/multiselect
pkg/views/base/multiselect/model.go:72:6: syntax error: unexpected name NewModel, expected (
pkg/views/base/multiselect/model.go:93:23: syntax error: unexpected name tea at end of statement
pkg/views/base/multiselect/model.go:139:23: syntax error: unexpected name string at end of statement
! process "go build -ldflags -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.Version=dev\n\t\t\t\t\t\t -X github.com/goharbor/harbor-
cli/cmd/harbor/internal/version.GoVersion=1.24.4\n\t\t\t\t\t\t -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.BuildTime=2025-08-19T11:42:40Z\n\t\t\t\t\t\t -X
github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GitCommit=63a3335\n\n\t\t\t\t -o /bin/harbor-cli /src/cmd/harbor/main.go" did not complete successfully: exit code: 1
please fix the above error. and fix lint issues
Signed-off-by: patrick.eschenbach <[email protected]>
@bupd Sorry should have checked Copilots changes more carefully before comitting them |
…ssion flows - Extract robot prompt helpers to pkg/prompt/robot (ChooseProjectPermissionMode, AskMoreProjects, ConfirmReplaceExisting, ChooseModifyMode, SelectProjects, AskUpdateSystemPerms) - Refactor cmd/harbor/root/robot/robot_permissions_project.go: split create/update flows; add small helpers (assignCommonPermissionsToSelectedProjects, assignPerProjectPermissionsInteractive, modifyPerProjectPermissionsInteractive, addProjectsInteractively, clearProjectPermissions, selectExistingProjectsForModify); remove duplication without behavior changes - Refactor cmd/harbor/root/robot/robot_permissions_system.go to use shared prompt helpers - Unify config loading and permission builders; keep public behavior stable - Remove unused splitPermissionsFromRobot and minor dead code Signed-off-by: Patrick Eschenbach <[email protected]>
…ckage Signed-off-by: Patrick Eschenbach <[email protected]>
…rsion error Signed-off-by: Patrick Eschenbach <[email protected]>
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.
Pull Request Overview
Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
pkg/robot/builders.go
Outdated
mergedPermissions = append(mergedPermissions, &rmodel.RobotPermission{ | ||
Namespace: "/", | ||
Access: accessesSystem, | ||
Kind: "system", | ||
}) |
Copilot
AI
Sep 30, 2025
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.
The system permissions are being added unconditionally, but this could append empty system permissions when accessesSystem
is nil or empty. Consider adding a length check: if len(accessesSystem) > 0 { ... }
mergedPermissions = append(mergedPermissions, &rmodel.RobotPermission{ | |
Namespace: "/", | |
Access: accessesSystem, | |
Kind: "system", | |
}) | |
if len(accessesSystem) > 0 { | |
mergedPermissions = append(mergedPermissions, &rmodel.RobotPermission{ | |
Namespace: "/", | |
Access: accessesSystem, | |
Kind: "system", | |
}) | |
} |
Copilot uses AI. Check for mistakes.
Signed-off-by: Prasanth Baskar <[email protected]>
…e central functions in project robot Signed-off-by: Patrick Eschenbach <[email protected]>
Signed-off-by: Patrick Eschenbach <[email protected]>
…to robot_refactor
Signed-off-by: Patrick Eschenbach <[email protected]>
Signed-off-by: Patrick Eschenbach <[email protected]>
…roject robot create Signed-off-by: Patrick Eschenbach <[email protected]>
Overview
This is the first of several pull requests that will remove code duplication and enhance maintainability of the robot account functionality of the Harbor CLI. Those PRs will be kept small for easier reviewing purposes.
Goal
SImplify existing code
Non Goal
Add new Features
Current changes