@@ -492,6 +492,7 @@ type SetJobStatusRequest struct {
492492 JobSummary * iac_utils.IacSummary `json:"job_summary"`
493493 Footprint * iac_utils.IacPlanFootprint `json:"job_plan_footprint"`
494494 PrCommentUrl string `json:"pr_comment_url"`
495+ PrCommentId string `json:"pr_comment_id"`
495496 TerraformOutput string `json:"terraform_output"`
496497}
497498
@@ -527,6 +528,7 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
527528 "jobId" , jobId ,
528529 "currentStatus" , job .Status ,
529530 "newStatus" , request .Status ,
531+ "prCommentId" , request .PrCommentId ,
530532 "batchId" , job .BatchID ,
531533 )
532534
@@ -566,8 +568,20 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
566568 )
567569 }
568570
571+ var prCommentId * int64
572+ num , err := strconv .ParseInt (request .PrCommentId , 10 , 64 )
573+ if err != nil {
574+ slog .Debug ("could not parse commentID" , "prCommentId" , prCommentId , "error" , err )
575+ slog .Warn ("setting prCommentId to nil since could not parse" )
576+ prCommentId = nil
577+ } else {
578+ prCommentId = & num
579+ }
580+
569581 job .PRCommentUrl = request .PrCommentUrl
570- err := models .DB .UpdateDiggerJob (job )
582+ job .PRCommentId = prCommentId
583+
584+ err = models .DB .UpdateDiggerJob (job )
571585 if err != nil {
572586 slog .Error ("Error updating job" ,
573587 "jobId" , jobId ,
@@ -820,6 +834,11 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
820834 }
821835 }
822836
837+ err = DeleteOlderPRCommentsIfEnabled (d .GithubClientProvider , batch )
838+ if err != nil {
839+ slog .Error ("failed to delete older comments" , "repoFullName" , batch .RepoFullName , "prNumber" , batch .PrNumber , "batchID" , batch .ID , "error" , err )
840+ }
841+
823842 // return batch summary to client
824843 res , err := batch .MapToJsonStruct ()
825844 if err != nil {
@@ -1439,3 +1458,121 @@ func AutomergePRforBatchIfEnabled(gh utils.GithubClientProvider, batch *models.D
14391458
14401459 return nil
14411460}
1461+
1462+ func DeleteOlderPRCommentsIfEnabled (gh utils.GithubClientProvider , batch * models.DiggerBatch ) error {
1463+ slog .Info ("Checking if PR should have prior comments deleted" ,
1464+ "batchId" , batch .ID ,
1465+ "prNumber" , batch .PrNumber ,
1466+ "batchStatus" , batch .Status ,
1467+ "batchType" , batch .BatchType ,
1468+ )
1469+
1470+ diggerYmlString := batch .DiggerConfig
1471+ diggerConfigYml , err := digger_config .LoadDiggerConfigYamlFromString (diggerYmlString )
1472+ if err != nil {
1473+ slog .Error ("Error loading Digger config from batch" ,
1474+ "batchId" , batch .ID ,
1475+ "error" , err ,
1476+ )
1477+ return fmt .Errorf ("error loading digger config from batch: %v" , err )
1478+ }
1479+
1480+ config , _ , err := digger_config .ConvertDiggerYamlToConfig (diggerConfigYml )
1481+ if err != nil {
1482+ slog .Error ("Error converting Digger YAML to config" ,
1483+ "batchId" , batch .ID ,
1484+ "error" , err ,
1485+ )
1486+ return fmt .Errorf ("error loading digger config from yaml: %v" , err )
1487+ }
1488+
1489+ deleteOlderComments := config .DeletePriorComments
1490+
1491+ slog .Debug ("Delete prior comments settings" ,
1492+ "enabled" , deleteOlderComments ,
1493+ "batchStatus" , batch .Status ,
1494+ "batchType" , batch .BatchType ,
1495+ )
1496+
1497+ if (batch .Status == orchestrator_scheduler .BatchJobSucceeded || batch .Status == orchestrator_scheduler .BatchJobFailed ) &&
1498+ batch .BatchType == orchestrator_scheduler .DiggerCommandPlan &&
1499+ batch .CoverAllImpactedProjects == true &&
1500+ deleteOlderComments == true {
1501+
1502+ slog .Info ("Conditions met for deleting prior comments, proceeding" ,
1503+ "batchId" , batch .ID ,
1504+ "prNumber" , batch .PrNumber ,
1505+ )
1506+
1507+ prService , err := GetPrServiceFromBatch (batch , gh )
1508+ if err != nil {
1509+ slog .Error ("Error getting PR service" ,
1510+ "batchId" , batch .ID ,
1511+ "error" , err ,
1512+ )
1513+ return fmt .Errorf ("error getting github service: %v" , err )
1514+ }
1515+
1516+ prBatches , err := models .DB .GetDiggerBatchesForPR (batch .RepoFullName , batch .PrNumber )
1517+ if err != nil {
1518+ slog .Error ("Error getting PR service" ,
1519+ "batchId" , batch .ID ,
1520+ "error" , err ,
1521+ )
1522+ return fmt .Errorf ("error getting github service: %v" , err )
1523+ }
1524+
1525+ for _ , prBatch := range prBatches {
1526+ if prBatch .BatchType == orchestrator_scheduler .DiggerCommandApply {
1527+ slog .Info ("found previous apply job for PR therefore not deleting earlier comments" )
1528+ return nil
1529+ }
1530+ }
1531+
1532+ allDeletesSuccessful := true
1533+ for _ , prBatch := range prBatches {
1534+ if prBatch .ID == batch .ID {
1535+ // don't delete the current batch comments
1536+ continue
1537+ }
1538+ jobs , err := models .DB .GetDiggerJobsForBatch (prBatch .ID )
1539+ if err != nil {
1540+ slog .Error ("could not get jobs for batch" , "batchId" , prBatch .ID , "error" , err )
1541+ // won't return error here since can still continue deleting rest of batches
1542+ continue
1543+ }
1544+ for _ , prJob := range jobs {
1545+ if prJob .PRCommentId == nil {
1546+ slog .Debug ("PR comment not found for job, ignoring deletion" , "JobID" , prJob .ID )
1547+ continue
1548+ }
1549+ // TODO: this delete will fail with 404 for all previous batches that already have been deleted
1550+ // for now its okay but maybe better approach is only considering the most recent or have a marker on each batch
1551+ // on whether or not its comments were deleted yet
1552+ err = prService .DeleteComment (strconv .FormatInt (* prJob .PRCommentId , 10 ))
1553+ if err != nil {
1554+ slog .Error ("Could not delete comment for job" , "jobID" , prJob .ID , "commentID" , prJob .PRCommentId , "error" , err )
1555+ allDeletesSuccessful = false
1556+ }
1557+ }
1558+ }
1559+
1560+ if ! allDeletesSuccessful {
1561+ slog .Warn ("some of the previous comments failed to delete" )
1562+ }
1563+
1564+ return nil
1565+
1566+ } else {
1567+ if batch .BatchType != orchestrator_scheduler .DiggerCommandPlan {
1568+ slog .Debug ("Skipping deletion of prior comments - not an plan command" ,
1569+ "batchId" , batch .ID ,
1570+ "batchType" , batch .BatchType ,
1571+ )
1572+ } else if ! deleteOlderComments {
1573+ slog .Debug ("Skipping deletion of prior comments - not enabled in config" , "batchId" , batch .ID )
1574+ }
1575+ }
1576+
1577+ return nil
1578+ }
0 commit comments