From 9b321980f18c221c08a2720700e15c9e492c0e72 Mon Sep 17 00:00:00 2001 From: motatoes Date: Fri, 6 Sep 2024 17:43:55 +0100 Subject: [PATCH] improvements to the triggered by btn --- next/controllers/github_after_merge.go | 2 +- next/controllers/runs.go | 3 ++- next/dbgen/dbgen.go | 5 ++++- next/dbmodels/storage.go | 3 ++- next/model/digger_runs.gen.go | 1 + next/models_generated/digger_runs.gen.go | 6 +++++- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/next/controllers/github_after_merge.go b/next/controllers/github_after_merge.go index aa13e58f5..06a7b9138 100644 --- a/next/controllers/github_after_merge.go +++ b/next/controllers/github_after_merge.go @@ -208,7 +208,7 @@ func handlePushEventApplyAfterMerge(gh nextutils.GithubClientProvider, payload * return fmt.Errorf("error creating digger run stage") } - diggerRun, err := dbmodels.DB.CreateDiggerRun("push", 0, dbmodels.RunQueued, commitId, "", installationId, repo.ID, project.ID, projectName, dbmodels.PlanAndApply, planStage.ID, applyStage.ID) + diggerRun, err := dbmodels.DB.CreateDiggerRun("push", 0, dbmodels.RunQueued, commitId, "", installationId, repo.ID, project.ID, projectName, dbmodels.PlanAndApply, planStage.ID, applyStage.ID, nil) if err != nil { log.Printf("Error creating digger run: %v", err) return fmt.Errorf("error creating digger run") diff --git a/next/controllers/runs.go b/next/controllers/runs.go index edbeeb175..f7e90032f 100644 --- a/next/controllers/runs.go +++ b/next/controllers/runs.go @@ -74,6 +74,7 @@ func (d DiggerController) TriggerRunForProjectAssumingUser(c *gin.Context) { return } projectId := request.ProjectId + userId := request.UserId p := dbmodels.DB.Query.Project project, err := dbmodels.DB.Query.Project.Where(p.ID.Eq(projectId)).First() @@ -136,7 +137,7 @@ func (d DiggerController) TriggerRunForProjectAssumingUser(c *gin.Context) { return } - diggerRun, err := dbmodels.DB.CreateDiggerRun("push", 0, dbmodels.RunQueued, *commitSha, "", installationId, repoId, projectId, projectName, dbmodels.PlanAndApply, planStage.ID, applyStage.ID) + diggerRun, err := dbmodels.DB.CreateDiggerRun("user", 0, dbmodels.RunQueued, *commitSha, "", installationId, repoId, projectId, projectName, dbmodels.PlanAndApply, planStage.ID, applyStage.ID, &userId) if err != nil { log.Printf("Error creating digger run: %v", err) c.JSON(http.StatusBadRequest, gin.H{"error": "error creating digger run"}) diff --git a/next/dbgen/dbgen.go b/next/dbgen/dbgen.go index 89e54505e..d4e8805fd 100644 --- a/next/dbgen/dbgen.go +++ b/next/dbgen/dbgen.go @@ -45,7 +45,10 @@ func main() { // need to make approver_user_id nullable and gorm gen does not generate it as a string pointer, for some reason g.ApplyBasic( - g.GenerateModel("digger_runs", gen.FieldType("approver_user_id", "*string")), + g.GenerateModel("digger_runs", + gen.FieldType("approver_user_id", "*string"), + gen.FieldType("triggered_by_user_id", "*string"), + ), ) g.ApplyBasic( diff --git a/next/dbmodels/storage.go b/next/dbmodels/storage.go index 3479b2383..9ef75f75c 100644 --- a/next/dbmodels/storage.go +++ b/next/dbmodels/storage.go @@ -756,7 +756,7 @@ func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([ return runs, nil } -func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId string, DiggerConfig string, GithubInstallationId int64, RepoId int64, projectId string, ProjectName string, RunType RunType, planStageId string, applyStageId string) (*model.DiggerRun, error) { +func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId string, DiggerConfig string, GithubInstallationId int64, RepoId int64, projectId string, ProjectName string, RunType RunType, planStageId string, applyStageId string, triggeredByUserId *string) (*model.DiggerRun, error) { dr := &model.DiggerRun{ ID: uuid.NewString(), Triggertype: Triggertype, @@ -774,6 +774,7 @@ func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status Dig IsApproved: false, ApprovalAuthor: "", ApplyLogs: "", + TriggeredByUserID: triggeredByUserId, } result := db.GormDB.Create(dr) if result.Error != nil { diff --git a/next/model/digger_runs.gen.go b/next/model/digger_runs.gen.go index f9f186c51..e87d35e8e 100644 --- a/next/model/digger_runs.gen.go +++ b/next/model/digger_runs.gen.go @@ -36,6 +36,7 @@ type DiggerRun struct { TerraformOutput string `gorm:"column:terraform_output" json:"terraform_output"` ApplyLogs string `gorm:"column:apply_logs" json:"apply_logs"` ApproverUserID *string `gorm:"column:approver_user_id" json:"approver_user_id"` + TriggeredByUserID *string `gorm:"column:triggered_by_user_id" json:"triggered_by_user_id"` } // TableName DiggerRun's table name diff --git a/next/models_generated/digger_runs.gen.go b/next/models_generated/digger_runs.gen.go index 8aeb6392f..52af5527c 100644 --- a/next/models_generated/digger_runs.gen.go +++ b/next/models_generated/digger_runs.gen.go @@ -49,6 +49,7 @@ func newDiggerRun(db *gorm.DB, opts ...gen.DOOption) diggerRun { _diggerRun.TerraformOutput = field.NewString(tableName, "terraform_output") _diggerRun.ApplyLogs = field.NewString(tableName, "apply_logs") _diggerRun.ApproverUserID = field.NewString(tableName, "approver_user_id") + _diggerRun.TriggeredByUserID = field.NewString(tableName, "triggered_by_user_id") _diggerRun.fillFieldMap() @@ -81,6 +82,7 @@ type diggerRun struct { TerraformOutput field.String ApplyLogs field.String ApproverUserID field.String + TriggeredByUserID field.String fieldMap map[string]field.Expr } @@ -119,6 +121,7 @@ func (d *diggerRun) updateTableName(table string) *diggerRun { d.TerraformOutput = field.NewString(table, "terraform_output") d.ApplyLogs = field.NewString(table, "apply_logs") d.ApproverUserID = field.NewString(table, "approver_user_id") + d.TriggeredByUserID = field.NewString(table, "triggered_by_user_id") d.fillFieldMap() @@ -135,7 +138,7 @@ func (d *diggerRun) GetFieldByName(fieldName string) (field.OrderExpr, bool) { } func (d *diggerRun) fillFieldMap() { - d.fieldMap = make(map[string]field.Expr, 22) + d.fieldMap = make(map[string]field.Expr, 23) d.fieldMap["id"] = d.ID d.fieldMap["created_at"] = d.CreatedAt d.fieldMap["updated_at"] = d.UpdatedAt @@ -158,6 +161,7 @@ func (d *diggerRun) fillFieldMap() { d.fieldMap["terraform_output"] = d.TerraformOutput d.fieldMap["apply_logs"] = d.ApplyLogs d.fieldMap["approver_user_id"] = d.ApproverUserID + d.fieldMap["triggered_by_user_id"] = d.TriggeredByUserID } func (d diggerRun) clone(db *gorm.DB) diggerRun {