@@ -1453,3 +1453,90 @@ func AutomergePRforBatchIfEnabled(gh utils.GithubClientProvider, batch *models.D
14531453
14541454 return nil
14551455}
1456+
1457+ func DeleteOlderPRCommentsIfEnabled (gh utils.GithubClientProvider , batch * models.DiggerBatch ) error {
1458+ slog .Info ("Checking if PR should be auto-merged" ,
1459+ "batchId" , batch .ID ,
1460+ "prNumber" , batch .PrNumber ,
1461+ "batchStatus" , batch .Status ,
1462+ "batchType" , batch .BatchType ,
1463+ )
1464+
1465+ diggerYmlString := batch .DiggerConfig
1466+ diggerConfigYml , err := digger_config .LoadDiggerConfigYamlFromString (diggerYmlString )
1467+ if err != nil {
1468+ slog .Error ("Error loading Digger config from batch" ,
1469+ "batchId" , batch .ID ,
1470+ "error" , err ,
1471+ )
1472+ return fmt .Errorf ("error loading digger config from batch: %v" , err )
1473+ }
1474+
1475+ config , _ , err := digger_config .ConvertDiggerYamlToConfig (diggerConfigYml )
1476+ if err != nil {
1477+ slog .Error ("Error converting Digger YAML to config" ,
1478+ "batchId" , batch .ID ,
1479+ "error" , err ,
1480+ )
1481+ return fmt .Errorf ("error loading digger config from yaml: %v" , err )
1482+ }
1483+
1484+ deleteOlderComments := config .DeletePriorComments
1485+
1486+ slog .Debug ("Delete prior comments settings" ,
1487+ "enabled" , deleteOlderComments ,
1488+ "batchStatus" , batch .Status ,
1489+ "batchType" , batch .BatchType ,
1490+ )
1491+
1492+ if (batch .Status == orchestrator_scheduler .BatchJobSucceeded || batch .Status == orchestrator_scheduler .BatchJobFailed ) &&
1493+ batch .BatchType == orchestrator_scheduler .DiggerCommandPlan &&
1494+ batch .CoverAllImpactedProjects == true &&
1495+ deleteOlderComments == true {
1496+
1497+ slog .Info ("Conditions met for auto-merge, proceeding" ,
1498+ "batchId" , batch .ID ,
1499+ "prNumber" , batch .PrNumber ,
1500+ )
1501+
1502+ prService , err := GetPrServiceFromBatch (batch , gh )
1503+ if err != nil {
1504+ slog .Error ("Error getting PR service" ,
1505+ "batchId" , batch .ID ,
1506+ "error" , err ,
1507+ )
1508+ return fmt .Errorf ("error getting github service: %v" , err )
1509+ }
1510+
1511+ prBatches , err := models .DB .GetDiggerBatchesForPR (batch .RepoFullName , batch .PrNumber )
1512+ if err != nil {
1513+ slog .Error ("Error getting PR service" ,
1514+ "batchId" , batch .ID ,
1515+ "error" , err ,
1516+ )
1517+ return fmt .Errorf ("error getting github service: %v" , err )
1518+ }
1519+
1520+ for _ , prBatch := range prBatches {
1521+ jobs , err := models .DB .GetDiggerJobsForBatch (prBatch .ID )
1522+ if err != nil {
1523+
1524+ }
1525+ for _ , prJob := range jobs {
1526+ prService .DeleteComment (strconv .FormatInt (* prJob .PRCommentId , 10 ))
1527+ }
1528+ }
1529+
1530+ } else {
1531+ if batch .BatchType != orchestrator_scheduler .DiggerCommandPlan {
1532+ slog .Debug ("Skipping deletion of prior comments - not an plan command" ,
1533+ "batchId" , batch .ID ,
1534+ "batchType" , batch .BatchType ,
1535+ )
1536+ } else if ! deleteOlderComments {
1537+ slog .Debug ("Skipping deletion of prior comments - not enabled in config" , "batchId" , batch .ID )
1538+ }
1539+ }
1540+
1541+ return nil
1542+ }
0 commit comments