Skip to content
Open
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
37 changes: 37 additions & 0 deletions plugins/codeamp/graphql/project_mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,43 @@ func (r *ProjectResolverMutation) UpdateProjectEnvironments(ctx context.Context,
EnvironmentID: environment.Model.ID,
ProjectID: project.Model.ID,
}

// Save branch if this is a new environment
if r.DB.Where("environment_id = ? and project_id = ?", environment.Model.ID, project.Model.ID).Find(&projectEnvironment).RecordNotFound() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use gorm.IsRecordNotFoundError (i think that's the right function)
to determine if it is a RecordNotFound error or not? If it's not a simple record not found, like there being a DB constraint or some sort of other error, we should log it.

You would do if err := r.DB.Where(query).Find(&model).Error; err != nil and then if gorm.IsRecordNotFoundError(err)

// default project
projectID := project.ID.String()
gitBranch := "master"
bookmarked := false
continuousDeploy := false
environmentID := environment.Model.ID.String()

var projectSettings model.ProjectSettings
if err := r.DB.Where("environment_id = ? and project_id = ?", environment.Model.ID, project.Model.ID).Find(&projectSettings).Error; err != nil {
// if not a simple record not found, log the error
if !gorm.IsRecordNotFoundError(err) {
log.Error(err.Error())
return nil, err
}
} else {
gitBranch = projectSettings.GitBranch
continuousDeploy = projectSettings.ContinuousDeploy
}

r.UpdateProject(ctx, &struct {
Project *model.ProjectInput
}{
&model.ProjectInput{
ID: &projectID,
GitProtocol: project.GitProtocol,
GitUrl: project.GitUrl,
GitBranch: &gitBranch,
Bookmarked: &bookmarked,
ContinuousDeploy: &continuousDeploy,
EnvironmentID: &environmentID,
},
})
}

r.DB.Where("environment_id = ? and project_id = ?", environment.Model.ID, project.Model.ID).FirstOrCreate(&projectEnvironment)
results = append(results, &EnvironmentResolver{DBEnvironmentResolver: &db_resolver.EnvironmentResolver{DB: r.DB, Environment: environment}})
} else {
Expand Down