Skip to content

Commit 76e48a3

Browse files
committed
temp commit
1 parent 313aaa4 commit 76e48a3

File tree

14 files changed

+135
-23
lines changed

14 files changed

+135
-23
lines changed

backend/controllers/projects.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

backend/models/storage.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,39 +1840,23 @@ func (db *Database) GetRepoCache(orgId uint, repoFullName string) (*RepoCache, e
18401840
return &repoCache, nil
18411841
}
18421842

1843-
func (db *Database) GetDiggerJobsForPR(orgId uint, repoFullName string, prNumber int) ([]DiggerJob, error) {
1843+
func (db *Database) GetDiggerBatchesForPR(repoFullName string, prNumber int) ([]DiggerBatch, error) {
18441844
// Step 1: Get all batches for the PR
18451845
batches := make([]DiggerBatch, 0)
18461846
result := db.GormDB.Where("repo_full_name = ? AND pr_number = ?", repoFullName, prNumber).Find(&batches)
18471847
if result.Error != nil {
18481848
slog.Error("error fetching batches for PR",
18491849
"prNumber", prNumber,
18501850
"repoFullName", repoFullName,
1851-
"orgId", orgId,
18521851
"error", result.Error)
18531852
return nil, result.Error
18541853
}
1855-
1856-
// Step 2: Get all jobs for each batch
1857-
allJobs := make([]DiggerJob, 0)
1858-
for _, batch := range batches {
1859-
jobs, err := db.GetDiggerJobsForBatch(batch.ID)
1860-
if err != nil {
1861-
slog.Error("error fetching digger jobs for batch",
1862-
"batchId", batch.ID,
1863-
"prNumber", prNumber,
1864-
"error", err)
1865-
return nil, err
1866-
}
1867-
allJobs = append(allJobs, jobs...)
1868-
}
1869-
1870-
slog.Info("fetched all digger jobs for PR",
1854+
slog.Info("fetched all digger batches for PR",
18711855
"prNumber", prNumber,
18721856
"repoFullName", repoFullName,
1873-
"orgId", orgId,
18741857
"batchCount", len(batches),
1875-
"jobCount", len(allJobs))
1876-
1877-
return allJobs, nil
1858+
"jobCount", len(batches))
1859+
1860+
return batches, nil
1861+
18781862
}

cli/pkg/digger/digger_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ func (m *MockPRManager) EditComment(prNumber int, id string, comment string) err
155155
return nil
156156
}
157157

158+
func (m *MockPRManager) DeleteComment(id string) error {
159+
return nil
160+
}
161+
158162
func (m *MockPRManager) CreateCommentReaction(id string, reaction string) error {
159163
m.Commands = append(m.Commands, RunInfo{"EditComment", id + " " + reaction, time.Now()})
160164
return nil

libs/ci/azure/azure.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ func (a *AzureReposService) EditComment(prNumber int, id string, comment string)
347347
return err
348348
}
349349

350+
func (a *AzureReposService) DeleteComment(id string) error {
351+
return nil
352+
}
353+
350354
func (a *AzureReposService) CreateCommentReaction(id string, reaction string) error {
351355
// TODO implement me
352356
return nil

libs/ci/bitbucket/bitbucket.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ func (b BitbucketAPI) EditComment(prNumber int, id string, comment string) error
183183
return nil
184184
}
185185

186-
func (a BitbucketAPI) CreateCommentReaction(id string, reaction string) error {
186+
func (b BitbucketAPI) DeleteComment(id string) error {
187+
return nil
188+
}
189+
190+
func (b BitbucketAPI) CreateCommentReaction(id string, reaction string) error {
187191
// TODO implement me
188192
return nil
189193
}

libs/ci/ci.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type PullRequestService interface {
99
PublishIssue(title string, body string, labels *[]string) (int64, error)
1010
UpdateIssue(ID int64, title string, body string) (int64, error)
1111
EditComment(prNumber int, id string, comment string) error
12+
DeleteComment(id string) error
1213
CreateCommentReaction(id string, reaction string) error
1314
GetComments(prNumber int) ([]Comment, error)
1415
GetApprovals(prNumber int) ([]string, error)

libs/ci/github/mocks.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ func (t MockCiService) EditComment(prNumber int, id string, comment string) erro
9999
return nil
100100
}
101101

102+
func (t MockCiService) DeleteComment(id string) error {
103+
return nil
104+
}
105+
102106
func (t MockCiService) CreateCommentReaction(id string, reaction string) error {
103107
// TODO implement me
104108
return nil

libs/ci/gitlab/gitlab.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ func (gitlabService GitLabService) EditComment(prNumber int, id string, comment
329329
return err
330330
}
331331

332+
func (gitlabService GitLabService) DeleteComment(id string) error {
333+
return nil
334+
}
335+
332336
func (gitlabService GitLabService) CreateCommentReaction(id string, reaction string) error {
333337
// TODO implement me
334338
return nil

libs/ci/mocks.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (t MockPullRequestManager) EditComment(prNumber int, id string, comment str
7171
return nil
7272
}
7373

74+
func (t MockPullRequestManager) DeleteComment(id string) error {
75+
return nil
76+
}
77+
7478
func (t MockPullRequestManager) CreateCommentReaction(id string, reaction string) error {
7579
return nil
7680
}

libs/comment_utils/reporting/reporting_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ func (t MockCiService) EditComment(prNumber int, id string, comment string) erro
9999
return nil
100100
}
101101

102+
func (svc MockCiService) DeleteComment(id string) error {
103+
return nil
104+
}
105+
102106
func (svc MockCiService) CreateCommentReaction(id string, reaction string) error {
103107
// TODO implement me
104108
return nil

0 commit comments

Comments
 (0)