Skip to content

fix: handle GetRobot error in robot create and update output path to prevent nil panic#928

Open
AdeshDeshmukh wants to merge 1 commit into
goharbor:mainfrom
AdeshDeshmukh:fix/getrobot-error-discard-nil-panic
Open

fix: handle GetRobot error in robot create and update output path to prevent nil panic#928
AdeshDeshmukh wants to merge 1 commit into
goharbor:mainfrom
AdeshDeshmukh:fix/getrobot-error-discard-nil-panic

Conversation

@AdeshDeshmukh
Copy link
Copy Markdown

Summary

Fixes three instances where api.GetRobot() error was silently
discarded with _, causing a nil pointer panic when the call
fails in the -o json or -o yaml output path.

Fixes #927

Background

After a robot is created or updated, the code calls GetRobot()
to fetch the full *models.Robot representation for JSON/YAML
output. This is intentional — CreateRobot only returns
*models.RobotCreated (a subset without permissions), so a
second call is needed.

However in all 3 affected files the error was discarded:

res, _ := api.GetRobot(response.Payload.ID) // error discarded
utils.SavePayloadJSON(name, res.Payload) // PANIC if res is nil

If GetRobot fails (network error, auth timeout, server error),
res is nil and res.Payload causes a nil pointer panic.
The CLI crashes instead of returning a clean error message.

Files Changed

File Line Change
cmd/harbor/root/project/robot/create.go ~205 Handle GetRobot error
cmd/harbor/root/robot/create.go ~334 Handle GetRobot error
cmd/harbor/root/robot/update.go ~590 Handle GetRobot error

Total: ~15 lines across 3 files.

Fix Applied (same pattern for all 3 locations)

// Before (broken):
res, _ := api.GetRobot(response.Payload.ID)
utils.SavePayloadJSON(name, res.Payload)

// After (fixed):
res, err := api.GetRobot(response.Payload.ID)
if err != nil {
    return fmt.Errorf("failed to get robot details: %v",
        utils.ParseHarborErrorMsg(err))
}
utils.SavePayloadJSON(name, res.Payload)

Pattern Alignment

This fix follows the exact pattern already used in the codebase
for the same api.GetRobot() call:

cmd/harbor/root/project/robot/view.go:

robot, err = api.GetRobot(robotID)
if err != nil {
    return fmt.Errorf("failed to get robot: %v", err)
}

Uses utils.ParseHarborErrorMsg consistent with all other
API error handling in the same files.

Checklist

…prevent nil panic

Signed-off-by: AdeshDeshmukh <adeshkd123@gmail.com>
Copilot AI review requested due to automatic review settings May 15, 2026 03:51
Copy link
Copy Markdown
Contributor

Copilot AI left a 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 fixes nil pointer panics in robot create/update commands by handling errors from api.GetRobot() before saving formatted robot output.

Changes:

  • Adds error handling for api.GetRobot() in system robot create output path.
  • Adds error handling for api.GetRobot() in system robot update output path.
  • Adds error handling for api.GetRobot() in project robot create output path.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
cmd/harbor/root/robot/update.go Handles GetRobot errors before saving updated robot output.
cmd/harbor/root/robot/create.go Handles GetRobot errors before saving created robot output.
cmd/harbor/root/project/robot/create.go Handles GetRobot errors before saving created project robot output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: GetRobot error discarded in robot create and update output path causing nil pointer panic

2 participants