Skip to content

Commit e340b53

Browse files
committed
support single comment summary
1 parent c9a0625 commit e340b53

File tree

10 files changed

+120
-46
lines changed

10 files changed

+120
-46
lines changed

backend/controllers/github.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,15 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
499499
log.Printf("strconv.ParseInt error: %v", err)
500500
commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not handle commentId: %v", err))
501501
}
502-
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGithub, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, installationId, branch, prNumber, repoOwner, repoName, repoFullName, commitSha, commentId, diggerYmlStr, 0)
502+
503+
placeholderComment, err := ghService.PublishComment(prNumber, "<digger report placehoder>")
504+
if err != nil {
505+
log.Printf("strconv.ParseInt error: %v", err)
506+
commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not create placeholder commentId for report: %v", err))
507+
return fmt.Errorf("comment reporter error: %v", err)
508+
}
509+
510+
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGithub, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, installationId, branch, prNumber, repoOwner, repoName, repoFullName, commitSha, commentId, &placeholderComment.Id, diggerYmlStr, 0)
503511
if err != nil {
504512
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
505513
commentReporterManager.UpdateComment(fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err))
@@ -874,7 +882,14 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
874882
return fmt.Errorf("comment reporter error: %v", err)
875883
}
876884

877-
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, "github", orgId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, *branch, issueNumber, repoOwner, repoName, repoFullName, *commitSha, reporterCommentId, diggerYmlStr, 0)
885+
placeholderComment, err := ghService.PublishComment(issueNumber, "<digger report placehoder>")
886+
if err != nil {
887+
log.Printf("strconv.ParseInt error: %v", err)
888+
commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not create placeholder commentId for report: %v", err))
889+
return fmt.Errorf("comment reporter error: %v", err)
890+
}
891+
892+
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, "github", orgId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, *branch, issueNumber, repoOwner, repoName, repoFullName, *commitSha, reporterCommentId, &placeholderComment.Id, diggerYmlStr, 0)
878893
if err != nil {
879894
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
880895
commentReporterManager.UpdateComment(fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err))

backend/models/scheduler.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ const DiggerVCSGithub DiggerVCSType = "github"
2222
const DiggerVCSGitlab DiggerVCSType = "gitlab"
2323

2424
type DiggerBatch struct {
25-
ID uuid.UUID `gorm:"primary_key"`
26-
VCS DiggerVCSType
27-
PrNumber int
28-
CommentId *int64
29-
Status orchestrator_scheduler.DiggerBatchStatus
30-
BranchName string
31-
DiggerConfig string
32-
GithubInstallationId int64
33-
GitlabProjectId int
34-
RepoFullName string
35-
RepoOwner string
36-
RepoName string
37-
BatchType orchestrator_scheduler.DiggerCommand
25+
ID uuid.UUID `gorm:"primary_key"`
26+
VCS DiggerVCSType
27+
PrNumber int
28+
CommentId *int64
29+
PlaceholderCommentIdForReport *string
30+
Status orchestrator_scheduler.DiggerBatchStatus
31+
BranchName string
32+
DiggerConfig string
33+
GithubInstallationId int64
34+
GitlabProjectId int
35+
RepoFullName string
36+
RepoOwner string
37+
RepoName string
38+
BatchType orchestrator_scheduler.DiggerCommand
3839
// used for module source grouping comments
3940
SourceDetails []byte
4041
}

backend/models/storage.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -617,22 +617,23 @@ func (db *Database) GetDiggerBatch(batchId *uuid.UUID) (*DiggerBatch, error) {
617617
return batch, nil
618618
}
619619

620-
func (db *Database) CreateDiggerBatch(vcsType DiggerVCSType, githubInstallationId int64, repoOwner string, repoName string, repoFullname string, PRNumber int, diggerConfig string, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int) (*DiggerBatch, error) {
620+
func (db *Database) CreateDiggerBatch(vcsType DiggerVCSType, githubInstallationId int64, repoOwner string, repoName string, repoFullname string, PRNumber int, diggerConfig string, branchName string, batchType scheduler.DiggerCommand, commentId *int64, placeholderCommentIdForReport *string, gitlabProjectId int) (*DiggerBatch, error) {
621621
uid := uuid.New()
622622
batch := &DiggerBatch{
623-
ID: uid,
624-
VCS: vcsType,
625-
GithubInstallationId: githubInstallationId,
626-
RepoOwner: repoOwner,
627-
RepoName: repoName,
628-
RepoFullName: repoFullname,
629-
PrNumber: PRNumber,
630-
CommentId: commentId,
631-
Status: scheduler.BatchJobCreated,
632-
BranchName: branchName,
633-
DiggerConfig: diggerConfig,
634-
BatchType: batchType,
635-
GitlabProjectId: gitlabProjectId,
623+
ID: uid,
624+
VCS: vcsType,
625+
GithubInstallationId: githubInstallationId,
626+
RepoOwner: repoOwner,
627+
RepoName: repoName,
628+
RepoFullName: repoFullname,
629+
PrNumber: PRNumber,
630+
CommentId: commentId,
631+
PlaceholderCommentIdForReport: placeholderCommentIdForReport,
632+
Status: scheduler.BatchJobCreated,
633+
BranchName: branchName,
634+
DiggerConfig: diggerConfig,
635+
BatchType: batchType,
636+
GitlabProjectId: gitlabProjectId,
636637
}
637638
result := db.GormDB.Save(batch)
638639
if result.Error != nil {

backend/services/spec.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ func GetSpecFromJob(job models.DiggerJob) (*spec.Spec, error) {
116116
CommentId: strconv.FormatInt(*batch.CommentId, 10),
117117
Job: jobSpec,
118118
Reporter: spec.ReporterSpec{
119-
ReportingStrategy: "comments_per_run",
119+
//ReportingStrategy: "comments_per_run",
120+
ReportingStrategy: "always_same_comment",
120121
ReporterType: "lazy",
122+
ReportCommentId: job.Batch.PlaceholderCommentIdForReport,
121123
},
122124
Lock: spec.LockSpec{
123125
LockType: "noop",

backend/utils/graphs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// ConvertJobsToDiggerJobs jobs is map with project name as a key and a Job as a value
17-
func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.DiggerVCSType, organisationId uint, jobsMap map[string]scheduler.Job, projectMap map[string]configuration.Project, projectsGraph graph.Graph[string, configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner string, repoName string, repoFullName string, commitSha string, commentId int64, diggerConfigStr string, gitlabProjectId int) (*uuid.UUID, map[string]*models.DiggerJob, error) {
17+
func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.DiggerVCSType, organisationId uint, jobsMap map[string]scheduler.Job, projectMap map[string]configuration.Project, projectsGraph graph.Graph[string, configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner string, repoName string, repoFullName string, commitSha string, commentId int64, placeholderCommentIdForReport *string, diggerConfigStr string, gitlabProjectId int) (*uuid.UUID, map[string]*models.DiggerJob, error) {
1818
result := make(map[string]*models.DiggerJob)
1919
organisation, err := models.DB.GetOrganisationById(organisationId)
2020
if err != nil {
@@ -43,7 +43,7 @@ func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.Dig
4343

4444
log.Printf("marshalledJobsMap: %v\n", marshalledJobsMap)
4545

46-
batch, err := models.DB.CreateDiggerBatch(vcsType, githubInstallationId, repoOwner, repoName, repoFullName, prNumber, diggerConfigStr, branch, jobType, &commentId, gitlabProjectId)
46+
batch, err := models.DB.CreateDiggerBatch(vcsType, githubInstallationId, repoOwner, repoName, repoFullName, prNumber, diggerConfigStr, branch, jobType, &commentId, placeholderCommentIdForReport, gitlabProjectId)
4747
if err != nil {
4848
return nil, nil, fmt.Errorf("failed to create batch: %v", err)
4949
}

ee/backend/controllers/gitlab.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab
333333
log.Printf("strconv.ParseInt error: %v", err)
334334
utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: could not handle commentId: %v", err))
335335
}
336-
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGitlab, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, 0, branch, prNumber, repoOwner, repoName, repoFullName, commitSha, commentId, diggeryamlStr, projectId)
336+
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGitlab, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, 0, branch, prNumber, repoOwner, repoName, repoFullName, commitSha, commentId, nil, diggeryamlStr, projectId)
337337
if err != nil {
338338
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
339339
utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err))
@@ -524,7 +524,7 @@ func handleIssueCommentEvent(gitlabProvider utils.GitlabProvider, payload *gitla
524524
log.Printf("ParseInt err: %v", err)
525525
return fmt.Errorf("parseint error: %v", err)
526526
}
527-
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGitlab, organisationId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, 0, branch, issueNumber, repoOwner, repoName, repoFullName, commitSha, commentId64, diggerYmlStr, projectId)
527+
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGitlab, organisationId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, 0, branch, issueNumber, repoOwner, repoName, repoFullName, commitSha, commentId64, nil, diggerYmlStr, projectId)
528528
if err != nil {
529529
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
530530
utils.InitCommentReporter(glService, issueNumber, fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err))

ee/backend/hooks/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ var DriftReconcilliationHook ce_controllers.IssueCommentHook = func(gh utils.Git
151151
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: could not handle commentId: %v", err))
152152
}
153153

154-
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, "github", orgId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, defaultBranch, issueNumber, repoOwner, repoName, repoFullName, "", reporterCommentId, diggerYmlStr, 0)
154+
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, "github", orgId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, defaultBranch, issueNumber, repoOwner, repoName, repoFullName, "", reporterCommentId, nil, diggerYmlStr, 0)
155155
if err != nil {
156156
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
157157
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err))

libs/comment_utils/reporting/reporting.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,42 @@ type ReportStrategy interface {
107107
Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error)
108108
}
109109

110+
type AlwaysSameCommentStrategy struct {
111+
Title string
112+
CommentId string
113+
TimeOfRun time.Time
114+
}
115+
116+
func (strategy AlwaysSameCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
117+
comments, err := ciService.GetComments(PrNumber)
118+
if err != nil {
119+
return "", "", fmt.Errorf("error getting comments: %v", err)
120+
}
121+
122+
var commentBody *string
123+
var commentUrl string
124+
for _, comment := range comments {
125+
if comment.Id == strategy.CommentId {
126+
commentBody = comment.Body
127+
commentUrl = comment.Url
128+
}
129+
}
130+
131+
var reportTitle string
132+
if strategy.Title != "" {
133+
reportTitle = strategy.Title + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
134+
} else {
135+
reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
136+
}
137+
138+
err = appendToExistingComment(ciService, PrNumber, *commentBody, report, reportTitle, strategy.CommentId, supportsCollapsibleComment)
139+
if err != nil {
140+
return "", "", fmt.Errorf("error while adding to existing comment: %v", err)
141+
}
142+
143+
return strategy.CommentId, commentUrl, err
144+
}
145+
110146
type CommentPerRunStrategy struct {
111147
Title string
112148
TimeOfRun time.Time
@@ -156,26 +192,37 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string,
156192
return fmt.Sprintf("%v", comment.Id), comment.Url, nil
157193
}
158194

195+
err := appendToExistingComment(ciService, PrNumber, commentBody, report, reportTitle, commentIdForThisRun, supportsCollapsible)
196+
if err != nil {
197+
return "", "", err
198+
}
199+
200+
return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil
201+
202+
}
203+
204+
func appendToExistingComment(ciService ci.PullRequestService, prNumber int, existingCommentBody string, reportToAppend string, reportTitle string, commentId string, supportsCollapsible bool) error {
205+
159206
// strip first and last lines
160-
lines := strings.Split(commentBody, "\n")
207+
lines := strings.Split(existingCommentBody, "\n")
161208
lines = lines[1 : len(lines)-1]
162-
commentBody = strings.Join(lines, "\n")
209+
existingCommentBody = strings.Join(lines, "\n")
163210

164-
commentBody = commentBody + "\n\n" + report + "\n"
211+
existingCommentBody = existingCommentBody + "\n\n" + reportToAppend + "\n"
165212

166213
var completeComment string
167214
if !supportsCollapsible {
168-
completeComment = utils.AsComment(reportTitle)(commentBody)
215+
completeComment = utils.AsComment(reportTitle)(existingCommentBody)
169216
} else {
170-
completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody)
217+
completeComment = utils.AsCollapsibleComment(reportTitle, false)(existingCommentBody)
171218
}
172219

173-
err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment)
220+
err := ciService.EditComment(prNumber, commentId, completeComment)
174221

175222
if err != nil {
176-
return "", "", fmt.Errorf("error editing comment: %v", err)
223+
return fmt.Errorf("error editing comment: %v", err)
177224
}
178-
return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil
225+
return nil
179226
}
180227

181228
type LatestRunCommentStrategy struct {

libs/spec/models.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
)
66

77
type ReporterSpec struct {
8-
ReporterType string `json:"reporter_type"`
9-
ReportingStrategy string `json:"reporting_strategy"`
10-
ReportTerraformOutput bool `json:"report_terraform_output"`
8+
ReporterType string `json:"reporter_type"`
9+
ReportingStrategy string `json:"reporting_strategy"`
10+
ReportTerraformOutput bool `json:"report_terraform_output"`
11+
ReportCommentId *string `json:"report_comment_id,omitempty"`
1112
}
1213

1314
type CommentUpdaterSpec struct {

libs/spec/providers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c
125125
return reporting.LatestRunCommentStrategy{
126126
TimeOfRun: time.Now(),
127127
}
128+
case "always_same_comment":
129+
return reporting.AlwaysSameCommentStrategy{
130+
Title: title,
131+
TimeOfRun: time.Now(),
132+
CommentId: *reporterSpec.ReportCommentId,
133+
}
134+
128135
default:
129136
return reporting.MultipleCommentsStrategy{}
130137
}

0 commit comments

Comments
 (0)