Skip to content

Commit 0e8bd72

Browse files
authored
Realtime backend summary updates (#2089)
* initial backend summary updates
1 parent 658c71d commit 0e8bd72

File tree

3 files changed

+358
-93
lines changed

3 files changed

+358
-93
lines changed

backend/controllers/projects.go

Lines changed: 56 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/diggerhq/digger/backend/models"
1717
"github.com/diggerhq/digger/backend/services"
1818
"github.com/diggerhq/digger/backend/utils"
19-
"github.com/diggerhq/digger/libs/ci"
2019
"github.com/diggerhq/digger/libs/comment_utils/reporting"
2120
"github.com/diggerhq/digger/libs/digger_config"
2221
"github.com/diggerhq/digger/libs/iac_utils"
@@ -624,6 +623,7 @@ type SetJobStatusRequest struct {
624623
WorkflowUrl string `json:"workflow_url,omitempty"`
625624
}
626625

626+
627627
func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
628628
jobId := c.Param("jobId")
629629
orgId, exists := c.Get(middleware.ORGANISATION_ID_KEY)
@@ -663,6 +663,42 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
663663
)
664664

665665
switch request.Status {
666+
case "created":
667+
job.Status = orchestrator_scheduler.DiggerJobCreated
668+
err := models.DB.UpdateDiggerJob(job)
669+
if err != nil {
670+
slog.Error("Error updating job status",
671+
"jobId", jobId,
672+
"status", request.Status,
673+
"error", err,
674+
)
675+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error updating job status"})
676+
return
677+
}
678+
679+
slog.Info("Job status updated to created", "jobId", jobId)
680+
681+
// Update PR comment with real-time status
682+
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "created")
683+
684+
case "triggered":
685+
job.Status = orchestrator_scheduler.DiggerJobTriggered
686+
err := models.DB.UpdateDiggerJob(job)
687+
if err != nil {
688+
slog.Error("Error updating job status",
689+
"jobId", jobId,
690+
"status", request.Status,
691+
"error", err,
692+
)
693+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error updating job status"})
694+
return
695+
}
696+
697+
slog.Info("Job status updated to triggered", "jobId", jobId)
698+
699+
// Update PR comment with real-time status
700+
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "triggered")
701+
666702
case "started":
667703
job.Status = orchestrator_scheduler.DiggerJobStarted
668704
if request.WorkflowUrl != "" {
@@ -682,6 +718,9 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
682718

683719
slog.Info("Job status updated to started", "jobId", jobId)
684720

721+
// Update PR comment with real-time status
722+
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "started")
723+
685724
case "succeeded":
686725
job.Status = orchestrator_scheduler.DiggerJobSucceeded
687726
job.TerraformOutput = request.TerraformOutput
@@ -839,6 +878,9 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
839878
models.DB.UpdateDiggerJobSummary(job.DiggerJobID, request.JobSummary.ResourcesCreated, request.JobSummary.ResourcesUpdated, request.JobSummary.ResourcesDeleted)
840879
}
841880

881+
// Update PR comment with real-time status for succeeded job
882+
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "succeeded")
883+
842884
case "failed":
843885
job.Status = orchestrator_scheduler.DiggerJobFailed
844886
job.TerraformOutput = request.TerraformOutput
@@ -858,12 +900,15 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
858900
"batchId", batchId,
859901
)
860902

903+
// Update PR comment with real-time status for failed job
904+
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "failed")
905+
861906
default:
862907
slog.Warn("Unexpected job status received",
863908
"jobId", jobId,
864909
"status", request.Status,
865910
)
866-
c.JSON(http.StatusBadRequest, gin.H{"error": "Unexpected job status"})
911+
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Unexpected job status: %s. Valid statuses are: created, triggered, started, succeeded, failed", request.Status)})
867912
return
868913
}
869914

@@ -965,6 +1010,12 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
9651010
c.JSON(http.StatusOK, res)
9661011
}
9671012

1013+
1014+
1015+
1016+
1017+
1018+
9681019
func updateWorkflowUrlForJob(githubClientProvider utils.GithubClientProvider, job *models.DiggerJob) error {
9691020
if job == nil {
9701021
return fmt.Errorf("job is nil")
@@ -1153,80 +1204,6 @@ func UpdateCommentsForBatchGroup(gh utils.GithubClientProvider, batch *models.Di
11531204
return nil
11541205
}
11551206

1156-
func GetPrServiceFromBatch(batch *models.DiggerBatch, gh utils.GithubClientProvider) (ci.PullRequestService, error) {
1157-
slog.Debug("Getting PR service for batch",
1158-
"batchId", batch.ID,
1159-
"vcs", batch.VCS,
1160-
"prNumber", batch.PrNumber,
1161-
)
1162-
1163-
switch batch.VCS {
1164-
case "github":
1165-
slog.Debug("Using GitHub service for batch",
1166-
"batchId", batch.ID,
1167-
"installationId", batch.GithubInstallationId,
1168-
"repoFullName", batch.RepoFullName,
1169-
)
1170-
1171-
service, _, err := utils.GetGithubService(
1172-
gh,
1173-
batch.GithubInstallationId,
1174-
batch.RepoFullName,
1175-
batch.RepoOwner,
1176-
batch.RepoName,
1177-
)
1178-
1179-
if err != nil {
1180-
slog.Error("Error getting GitHub service",
1181-
"batchId", batch.ID,
1182-
"repoFullName", batch.RepoFullName,
1183-
"error", err,
1184-
)
1185-
} else {
1186-
slog.Debug("Successfully got GitHub service",
1187-
"batchId", batch.ID,
1188-
"repoFullName", batch.RepoFullName,
1189-
)
1190-
}
1191-
1192-
return service, err
1193-
1194-
case "gitlab":
1195-
slog.Debug("Using GitLab service for batch",
1196-
"batchId", batch.ID,
1197-
"projectId", batch.GitlabProjectId,
1198-
"repoFullName", batch.RepoFullName,
1199-
)
1200-
1201-
service, err := utils.GetGitlabService(
1202-
utils.GitlabClientProvider{},
1203-
batch.GitlabProjectId,
1204-
batch.RepoName,
1205-
batch.RepoFullName,
1206-
batch.PrNumber,
1207-
"",
1208-
)
1209-
1210-
if err != nil {
1211-
slog.Error("Error getting GitLab service",
1212-
"batchId", batch.ID,
1213-
"projectId", batch.GitlabProjectId,
1214-
"error", err,
1215-
)
1216-
} else {
1217-
slog.Debug("Successfully got GitLab service",
1218-
"batchId", batch.ID,
1219-
"projectId", batch.GitlabProjectId,
1220-
)
1221-
}
1222-
1223-
return service, err
1224-
}
1225-
1226-
slog.Error("Unsupported VCS type", "vcs", batch.VCS, "batchId", batch.ID)
1227-
return nil, fmt.Errorf("could not retrieve a service for %v", batch.VCS)
1228-
}
1229-
12301207
func CreateTerraformOutputsSummary(gh utils.GithubClientProvider, batch *models.DiggerBatch) error {
12311208
slog.Info("Creating Terraform outputs summary",
12321209
"batchId", batch.ID,
@@ -1259,7 +1236,7 @@ func CreateTerraformOutputsSummary(gh utils.GithubClientProvider, batch *models.
12591236
"prNumber", batch.PrNumber,
12601237
)
12611238

1262-
prService, err := GetPrServiceFromBatch(batch, gh)
1239+
prService, err := utils.GetPrServiceFromBatch(batch, gh)
12631240
if err != nil {
12641241
slog.Error("Error getting PR service",
12651242
"batchId", batch.ID,
@@ -1462,7 +1439,7 @@ func AutomergePRforBatchIfEnabled(gh utils.GithubClientProvider, batch *models.D
14621439
"prNumber", batch.PrNumber,
14631440
)
14641441

1465-
prService, err := GetPrServiceFromBatch(batch, gh)
1442+
prService, err := utils.GetPrServiceFromBatch(batch, gh)
14661443
if err != nil {
14671444
slog.Error("Error getting PR service",
14681445
"batchId", batch.ID,
@@ -1554,7 +1531,7 @@ func DeleteOlderPRCommentsIfEnabled(gh utils.GithubClientProvider, batch *models
15541531
"prNumber", batch.PrNumber,
15551532
)
15561533

1557-
prService, err := GetPrServiceFromBatch(batch, gh)
1534+
prService, err := utils.GetPrServiceFromBatch(batch, gh)
15581535
if err != nil {
15591536
slog.Error("Error getting PR service",
15601537
"batchId", batch.ID,

0 commit comments

Comments
 (0)