UpdateProject updates a project.
(ctx context.Context, req *connect.Request[v1pb.UpdateProjectRequest])
| 257 | |
| 258 | // UpdateProject updates a project. |
| 259 | func (s *ProjectService) UpdateProject(ctx context.Context, req *connect.Request[v1pb.UpdateProjectRequest]) (*connect.Response[v1pb.Project], error) { |
| 260 | if req.Msg.Project == nil { |
| 261 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("project must be set")) |
| 262 | } |
| 263 | if req.Msg.UpdateMask == nil { |
| 264 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("update_mask must be set")) |
| 265 | } |
| 266 | |
| 267 | project, err := s.getProjectMessage(ctx, req.Msg.Project.Name) |
| 268 | if err != nil { |
| 269 | if connect.CodeOf(err) == connect.CodeNotFound && req.Msg.AllowMissing { |
| 270 | // When allow_missing is true and project doesn't exist, create a new one |
| 271 | projectID, perr := common.GetProjectID(req.Msg.Project.Name) |
| 272 | if perr != nil { |
| 273 | return nil, connect.NewError(connect.CodeInvalidArgument, perr) |
| 274 | } |
| 275 | |
| 276 | return s.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{ |
| 277 | Project: req.Msg.Project, |
| 278 | ProjectId: projectID, |
| 279 | })) |
| 280 | } |
| 281 | return nil, err |
| 282 | } |
| 283 | if project.Deleted { |
| 284 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project %q has been deleted", req.Msg.Project.Name)) |
| 285 | } |
| 286 | if common.IsDefaultProject(project.Workspace, project.ResourceID) { |
| 287 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("default project cannot be updated")) |
| 288 | } |
| 289 | |
| 290 | patch := &store.UpdateProjectMessage{ |
| 291 | ResourceID: project.ResourceID, |
| 292 | Workspace: project.Workspace, |
| 293 | } |
| 294 | |
| 295 | projectSettings := proto.CloneOf(project.Setting) |
| 296 | for _, path := range req.Msg.UpdateMask.Paths { |
| 297 | switch path { |
| 298 | case "title": |
| 299 | patch.Title = &req.Msg.Project.Title |
| 300 | case "data_classification_config_id": |
| 301 | setting, err := s.store.GetDataClassificationSetting(ctx, common.GetWorkspaceIDFromContext(ctx)) |
| 302 | if err != nil { |
| 303 | return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to get data classification setting")) |
| 304 | } |
| 305 | existConfig := false |
| 306 | for _, config := range setting.Configs { |
| 307 | if config.Id == req.Msg.Project.DataClassificationConfigId { |
| 308 | existConfig = true |
| 309 | break |
| 310 | } |
| 311 | } |
| 312 | if !existConfig { |
| 313 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("data classification %s not exists", req.Msg.Project.DataClassificationConfigId)) |
| 314 | } |
| 315 | projectSettings.DataClassificationConfigId = req.Msg.Project.DataClassificationConfigId |
| 316 | patch.Setting = projectSettings |
nothing calls this directly
no test coverage detected