diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index da1e9b25f4b5..c995f705e8d2 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -7846,7 +7846,7 @@ definitions: type: array items: $ref: '#/definitions/RobotPermission' - Creator: + creator: type: string description: The creator of the robot creation_time: diff --git a/make/migrations/postgresql/0150_2.12.0_schema.up.sql b/make/migrations/postgresql/0150_2.12.0_schema.up.sql index 82c36bc9cbc5..82f1061722d3 100644 --- a/make/migrations/postgresql/0150_2.12.0_schema.up.sql +++ b/make/migrations/postgresql/0150_2.12.0_schema.up.sql @@ -1,5 +1,5 @@ /* -Add new column robot for artifact table to add a new column to record the creator of the robot +Add new column creator for robot table to add a new column to record the creator of the robot */ ALTER TABLE robot ADD COLUMN IF NOT EXISTS creator varchar(255); UPDATE robot SET creator = 'unknown' WHERE creator IS NULL; diff --git a/src/controller/robot/controller.go b/src/controller/robot/controller.go index b2dc81dbbfa2..cd6469c31e51 100644 --- a/src/controller/robot/controller.go +++ b/src/controller/robot/controller.go @@ -121,6 +121,7 @@ func (d *controller) Create(ctx context.Context, r *Robot) (int64, string, error if r.Level == LEVELPROJECT { name = fmt.Sprintf("%s+%s", r.ProjectName, r.Name) } + robotID, err := d.robotMgr.Create(ctx, &model.Robot{ Name: name, Description: r.Description, diff --git a/src/controller/robot/controller_test.go b/src/controller/robot/controller_test.go index 529c4b4bb465..148e4cc1636f 100644 --- a/src/controller/robot/controller_test.go +++ b/src/controller/robot/controller_test.go @@ -2,6 +2,8 @@ package robot import ( "context" + "github.com/goharbor/harbor/src/common/security" + testsec "github.com/goharbor/harbor/src/testing/common/security" "os" "testing" @@ -102,7 +104,9 @@ func (suite *ControllerTestSuite) TestCreate() { robotMgr := &robot.Manager{} c := controller{robotMgr: robotMgr, rbacMgr: rbacMgr, proMgr: projectMgr} - ctx := context.TODO() + secCtx := &testsec.Context{} + secCtx.On("GetUsername").Return("security-context-user") + ctx := security.NewContext(context.Background(), secCtx) projectMgr.On("Get", mock.Anything, mock.Anything).Return(&proModels.Project{ProjectID: 1, Name: "library"}, nil) robotMgr.On("Create", mock.Anything, mock.Anything).Return(int64(1), nil) rbacMgr.On("CreateRbacPolicy", mock.Anything, mock.Anything, mock.Anything).Return(int64(1), nil) @@ -113,7 +117,6 @@ func (suite *ControllerTestSuite) TestCreate() { Name: "testcreate", Description: "testcreate", Duration: 0, - Creator: "tester", }, ProjectName: "library", Level: LEVELPROJECT, diff --git a/src/server/v2.0/handler/robot.go b/src/server/v2.0/handler/robot.go index 6e910cdc1ddb..316db7cd5f02 100644 --- a/src/server/v2.0/handler/robot.go +++ b/src/server/v2.0/handler/robot.go @@ -62,7 +62,7 @@ func (rAPI *robotAPI) CreateRobot(ctx context.Context, params operation.CreateRo return rAPI.SendError(ctx, err) } - secCtx, err := rAPI.GetSecurityContext(ctx) + sc, err := rAPI.GetSecurityContext(ctx) if err != nil { return rAPI.SendError(ctx, err) } @@ -73,7 +73,7 @@ func (rAPI *robotAPI) CreateRobot(ctx context.Context, params operation.CreateRo Description: params.Robot.Description, Duration: params.Robot.Duration, Visible: true, - Creator: secCtx.GetUsername(), + Creator: sc.GetUsername(), }, Level: params.Robot.Level, }