-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1727 from diggerhq/feat/status-update
support handling job status update
- Loading branch information
Showing
12 changed files
with
406 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/diggerhq/digger/ee/drift/dbmodels" | ||
"github.com/diggerhq/digger/ee/drift/model" | ||
"github.com/diggerhq/digger/libs/terraform_utils" | ||
"github.com/gin-gonic/gin" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type SetJobStatusRequest struct { | ||
Status string `json:"status"` | ||
Timestamp time.Time `json:"timestamp"` | ||
JobSummary *terraform_utils.TerraformSummary `json:"job_summary"` | ||
Footprint *terraform_utils.TerraformPlanFootprint `json:"job_plan_footprint"` | ||
PrCommentUrl string `json:"pr_comment_url"` | ||
TerraformOutput string `json:"terraform_output""` | ||
} | ||
|
||
func (mc MainController) SetJobStatusForProject(c *gin.Context) { | ||
jobId := c.Param("jobId") | ||
//orgId, exists := c.Get(middleware.ORGANISATION_ID_KEY) | ||
|
||
//if !exists { | ||
// c.String(http.StatusForbidden, "Not allowed to access this resource") | ||
// return | ||
//} | ||
|
||
var request SetJobStatusRequest | ||
|
||
err := c.BindJSON(&request) | ||
|
||
if err != nil { | ||
log.Printf("Error binding JSON: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error binding JSON"}) | ||
return | ||
} | ||
|
||
log.Printf("settings tatus for job: %v, new status: %v, tfout: %v, job summary: %v", jobId, request.Status, request.TerraformOutput, request.JobSummary) | ||
|
||
job, err := dbmodels.DB.GetDiggerCiJob(jobId) | ||
if err != nil { | ||
log.Printf("could not get digger ci job, err: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting digger job"}) | ||
return | ||
} | ||
|
||
switch request.Status { | ||
case string(dbmodels.DiggerJobStarted): | ||
job.Status = string(dbmodels.DiggerJobStarted) | ||
err := dbmodels.DB.UpdateDiggerJob(job) | ||
if err != nil { | ||
log.Printf("Error updating job status: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error updating job status"}) | ||
return | ||
} | ||
case string(dbmodels.DiggerJobSucceeded): | ||
job.Status = string(dbmodels.DiggerJobSucceeded) | ||
job.TerraformOutput = request.TerraformOutput | ||
job.ResourcesCreated = int32(request.JobSummary.ResourcesCreated) | ||
job.ResourcesUpdated = int32(request.JobSummary.ResourcesUpdated) | ||
job.ResourcesDeleted = int32(request.JobSummary.ResourcesDeleted) | ||
err := dbmodels.DB.UpdateDiggerJob(job) | ||
if err != nil { | ||
log.Printf("Error updating job status: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error updating job status"}) | ||
return | ||
} | ||
|
||
project, err := dbmodels.DB.GetProjectById(job.ProjectID) | ||
if err != nil { | ||
log.Printf("Error retriving project: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving project"}) | ||
return | ||
|
||
} | ||
err = ProjectDriftStateMachineApply(*project, job.TerraformOutput, job.ResourcesCreated, job.ResourcesUpdated, job.ResourcesDeleted) | ||
if err != nil { | ||
log.Printf("error while checking drifted project") | ||
} | ||
|
||
case string(dbmodels.DiggerJobFailed): | ||
job.Status = string(dbmodels.DiggerJobFailed) | ||
job.TerraformOutput = request.TerraformOutput | ||
err := dbmodels.DB.UpdateDiggerJob(job) | ||
if err != nil { | ||
log.Printf("Error updating job status: %v", request.Status) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error saving job"}) | ||
return | ||
} | ||
|
||
default: | ||
log.Printf("Unexpected status %v", request.Status) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error saving job"}) | ||
return | ||
} | ||
|
||
job.UpdatedAt = request.Timestamp | ||
err = dbmodels.DB.GormDB.Save(job).Error | ||
if err != nil { | ||
log.Printf("Error saving update job: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error saving job"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{}) | ||
} | ||
|
||
func ProjectDriftStateMachineApply(project model.Project, tfplan string, resourcesCreated int32, resourcesUpdated int32, resourcesDeleted int32) error { | ||
isEmptyPlan := resourcesCreated == 0 && resourcesUpdated == 0 && resourcesDeleted == 0 | ||
wasEmptyPlan := project.ToCreate == 0 && project.ToUpdate == 0 && project.ToDelete == 0 | ||
if isEmptyPlan { | ||
project.DriftStatus = dbmodels.DriftStatusNoDrift | ||
} | ||
if !isEmptyPlan && wasEmptyPlan { | ||
project.DriftStatus = dbmodels.DriftStatusNewDrift | ||
} | ||
if !isEmptyPlan && !wasEmptyPlan { | ||
if project.DriftTerraformPlan != tfplan { | ||
if project.IsAcknowledged { | ||
project.DriftStatus = dbmodels.DriftStatusNewDrift | ||
} | ||
} | ||
} | ||
|
||
project.DriftTerraformPlan = tfplan | ||
project.ToCreate = resourcesCreated | ||
project.ToUpdate = resourcesUpdated | ||
project.ToDelete = resourcesDeleted | ||
result := dbmodels.DB.GormDB.Save(&project) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
log.Printf("project %v, (name: %v) has been updated successfully\n", project.ID, project.Name) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package dbmodels | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/diggerhq/digger/ee/drift/model" | ||
"github.com/diggerhq/digger/libs/spec" | ||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
"log" | ||
"time" | ||
) | ||
|
||
type DiggerJobStatus string | ||
|
||
const ( | ||
DiggerJobCreated DiggerJobStatus = "created" | ||
DiggerJobTriggered DiggerJobStatus = "triggered" | ||
DiggerJobFailed DiggerJobStatus = "failed" | ||
DiggerJobStarted DiggerJobStatus = "started" | ||
DiggerJobSucceeded DiggerJobStatus = "succeeded" | ||
DiggerJobQueuedForRun DiggerJobStatus = "queued" | ||
) | ||
|
||
func (db *Database) GetDiggerCiJob(diggerJobId string) (*model.DiggerCiJob, error) { | ||
log.Printf("GetDiggerCiJob, diggerJobId: %v", diggerJobId) | ||
var ciJob model.DiggerCiJob | ||
|
||
err := db.GormDB.Where("digger_job_id = ?", diggerJobId).First(&ciJob).Error | ||
|
||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, fmt.Errorf("ci job not found") | ||
} | ||
log.Printf("Unknown error occurred while fetching database, %v\n", err) | ||
return nil, err | ||
} | ||
|
||
return &ciJob, nil | ||
} | ||
|
||
func (db *Database) UpdateDiggerJob(job *model.DiggerCiJob) error { | ||
result := db.GormDB.Save(job) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
log.Printf("DiggerJob %v, (id: %v) has been updated successfully\n", job.DiggerJobID, job.ID) | ||
return nil | ||
} | ||
|
||
func (db *Database) CreateCiJobFromSpec(spec spec.Spec, runName string, projectId string) (*model.DiggerCiJob, error) { | ||
|
||
marshalledJobSpec, err := json.Marshal(spec.Job) | ||
if err != nil { | ||
log.Printf("failed to marshal job: %v", err) | ||
return nil, err | ||
} | ||
|
||
marshalledReporterSpec, err := json.Marshal(spec.Reporter) | ||
if err != nil { | ||
log.Printf("failed to marshal reporter: %v", err) | ||
return nil, err | ||
} | ||
|
||
marshalledCommentUpdaterSpec, err := json.Marshal(spec.CommentUpdater) | ||
if err != nil { | ||
log.Printf("failed to marshal comment updater: %v", err) | ||
return nil, err | ||
} | ||
|
||
marshalledLockSpec, err := json.Marshal(spec.Lock) | ||
if err != nil { | ||
log.Printf("failed to marshal lockspec: %v", err) | ||
return nil, err | ||
} | ||
|
||
marshalledBackendSpec, err := json.Marshal(spec.Backend) | ||
if err != nil { | ||
log.Printf("failed to marshal backend spec: %v", err) | ||
return nil, err | ||
|
||
} | ||
|
||
marshalledVcsSpec, err := json.Marshal(spec.VCS) | ||
if err != nil { | ||
log.Printf("failed to marshal vcs spec: %v", err) | ||
return nil, err | ||
} | ||
|
||
marshalledPolicySpec, err := json.Marshal(spec.Policy) | ||
if err != nil { | ||
log.Printf("failed to marshal policy spec: %v", err) | ||
return nil, err | ||
} | ||
|
||
marshalledVariablesSpec, err := json.Marshal(spec.Variables) | ||
if err != nil { | ||
log.Printf("failed to marshal variables spec: %v", err) | ||
return nil, err | ||
} | ||
|
||
job := &model.DiggerCiJob{ | ||
ID: uuid.NewString(), | ||
DiggerJobID: spec.JobId, | ||
Spectype: string(spec.SpecType), | ||
Commentid: spec.CommentId, | ||
Runname: runName, | ||
Jobspec: marshalledJobSpec, | ||
Reporterspec: marshalledReporterSpec, | ||
Commentupdaterspec: marshalledCommentUpdaterSpec, | ||
Lockspec: marshalledLockSpec, | ||
Backendspec: marshalledBackendSpec, | ||
Vcsspec: marshalledVcsSpec, | ||
Policyspec: marshalledPolicySpec, | ||
Variablesspec: marshalledVariablesSpec, | ||
CreatedAt: time.Time{}, | ||
UpdatedAt: time.Time{}, | ||
DeletedAt: gorm.DeletedAt{}, | ||
WorkflowFile: spec.VCS.WorkflowFile, | ||
WorkflowURL: "", | ||
Status: string(DiggerJobCreated), | ||
ResourcesCreated: 0, | ||
ResourcesUpdated: 0, | ||
ResourcesDeleted: 0, | ||
ProjectID: projectId, | ||
} | ||
|
||
err = db.GormDB.Create(job).Error | ||
if err != nil { | ||
log.Printf("failed to create job: %v", err) | ||
return nil, err | ||
} | ||
|
||
return job, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.