From e340b53efb5ece012e360b8fb94cc4a0b108cd9b Mon Sep 17 00:00:00 2001 From: motatoes Date: Wed, 4 Dec 2024 19:22:01 +0000 Subject: [PATCH 01/13] support single comment summary --- backend/controllers/github.go | 19 ++++++- backend/models/scheduler.go | 27 +++++----- backend/models/storage.go | 29 ++++++----- backend/services/spec.go | 4 +- backend/utils/graphs.go | 4 +- ee/backend/controllers/gitlab.go | 4 +- ee/backend/hooks/github.go | 2 +- libs/comment_utils/reporting/reporting.go | 63 ++++++++++++++++++++--- libs/spec/models.go | 7 +-- libs/spec/providers.go | 7 +++ 10 files changed, 120 insertions(+), 46 deletions(-) diff --git a/backend/controllers/github.go b/backend/controllers/github.go index 5c012cfe7..51edca65e 100644 --- a/backend/controllers/github.go +++ b/backend/controllers/github.go @@ -499,7 +499,15 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR log.Printf("strconv.ParseInt error: %v", err) commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not handle commentId: %v", err)) } - batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGithub, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, installationId, branch, prNumber, repoOwner, repoName, repoFullName, commitSha, commentId, diggerYmlStr, 0) + + placeholderComment, err := ghService.PublishComment(prNumber, "") + if err != nil { + log.Printf("strconv.ParseInt error: %v", err) + commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not create placeholder commentId for report: %v", err)) + return fmt.Errorf("comment reporter error: %v", err) + } + + batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGithub, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, installationId, branch, prNumber, repoOwner, repoName, repoFullName, commitSha, commentId, &placeholderComment.Id, diggerYmlStr, 0) if err != nil { log.Printf("ConvertJobsToDiggerJobs error: %v", err) commentReporterManager.UpdateComment(fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err)) @@ -874,7 +882,14 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu return fmt.Errorf("comment reporter error: %v", err) } - batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, "github", orgId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, *branch, issueNumber, repoOwner, repoName, repoFullName, *commitSha, reporterCommentId, diggerYmlStr, 0) + placeholderComment, err := ghService.PublishComment(issueNumber, "") + if err != nil { + log.Printf("strconv.ParseInt error: %v", err) + commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not create placeholder commentId for report: %v", err)) + return fmt.Errorf("comment reporter error: %v", err) + } + + batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, "github", orgId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, *branch, issueNumber, repoOwner, repoName, repoFullName, *commitSha, reporterCommentId, &placeholderComment.Id, diggerYmlStr, 0) if err != nil { log.Printf("ConvertJobsToDiggerJobs error: %v", err) commentReporterManager.UpdateComment(fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err)) diff --git a/backend/models/scheduler.go b/backend/models/scheduler.go index 0d0943b01..2affa0b64 100644 --- a/backend/models/scheduler.go +++ b/backend/models/scheduler.go @@ -22,19 +22,20 @@ const DiggerVCSGithub DiggerVCSType = "github" const DiggerVCSGitlab DiggerVCSType = "gitlab" type DiggerBatch struct { - ID uuid.UUID `gorm:"primary_key"` - VCS DiggerVCSType - PrNumber int - CommentId *int64 - Status orchestrator_scheduler.DiggerBatchStatus - BranchName string - DiggerConfig string - GithubInstallationId int64 - GitlabProjectId int - RepoFullName string - RepoOwner string - RepoName string - BatchType orchestrator_scheduler.DiggerCommand + ID uuid.UUID `gorm:"primary_key"` + VCS DiggerVCSType + PrNumber int + CommentId *int64 + PlaceholderCommentIdForReport *string + Status orchestrator_scheduler.DiggerBatchStatus + BranchName string + DiggerConfig string + GithubInstallationId int64 + GitlabProjectId int + RepoFullName string + RepoOwner string + RepoName string + BatchType orchestrator_scheduler.DiggerCommand // used for module source grouping comments SourceDetails []byte } diff --git a/backend/models/storage.go b/backend/models/storage.go index 6c5397d10..1668f4d2a 100644 --- a/backend/models/storage.go +++ b/backend/models/storage.go @@ -617,22 +617,23 @@ func (db *Database) GetDiggerBatch(batchId *uuid.UUID) (*DiggerBatch, error) { return batch, nil } -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) { +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) { uid := uuid.New() batch := &DiggerBatch{ - ID: uid, - VCS: vcsType, - GithubInstallationId: githubInstallationId, - RepoOwner: repoOwner, - RepoName: repoName, - RepoFullName: repoFullname, - PrNumber: PRNumber, - CommentId: commentId, - Status: scheduler.BatchJobCreated, - BranchName: branchName, - DiggerConfig: diggerConfig, - BatchType: batchType, - GitlabProjectId: gitlabProjectId, + ID: uid, + VCS: vcsType, + GithubInstallationId: githubInstallationId, + RepoOwner: repoOwner, + RepoName: repoName, + RepoFullName: repoFullname, + PrNumber: PRNumber, + CommentId: commentId, + PlaceholderCommentIdForReport: placeholderCommentIdForReport, + Status: scheduler.BatchJobCreated, + BranchName: branchName, + DiggerConfig: diggerConfig, + BatchType: batchType, + GitlabProjectId: gitlabProjectId, } result := db.GormDB.Save(batch) if result.Error != nil { diff --git a/backend/services/spec.go b/backend/services/spec.go index d363e74aa..8152f6f42 100644 --- a/backend/services/spec.go +++ b/backend/services/spec.go @@ -116,8 +116,10 @@ func GetSpecFromJob(job models.DiggerJob) (*spec.Spec, error) { CommentId: strconv.FormatInt(*batch.CommentId, 10), Job: jobSpec, Reporter: spec.ReporterSpec{ - ReportingStrategy: "comments_per_run", + //ReportingStrategy: "comments_per_run", + ReportingStrategy: "always_same_comment", ReporterType: "lazy", + ReportCommentId: job.Batch.PlaceholderCommentIdForReport, }, Lock: spec.LockSpec{ LockType: "noop", diff --git a/backend/utils/graphs.go b/backend/utils/graphs.go index 7f5c33950..66cc38abe 100644 --- a/backend/utils/graphs.go +++ b/backend/utils/graphs.go @@ -14,7 +14,7 @@ import ( ) // ConvertJobsToDiggerJobs jobs is map with project name as a key and a Job as a value -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) { +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) { result := make(map[string]*models.DiggerJob) organisation, err := models.DB.GetOrganisationById(organisationId) if err != nil { @@ -43,7 +43,7 @@ func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.Dig log.Printf("marshalledJobsMap: %v\n", marshalledJobsMap) - batch, err := models.DB.CreateDiggerBatch(vcsType, githubInstallationId, repoOwner, repoName, repoFullName, prNumber, diggerConfigStr, branch, jobType, &commentId, gitlabProjectId) + batch, err := models.DB.CreateDiggerBatch(vcsType, githubInstallationId, repoOwner, repoName, repoFullName, prNumber, diggerConfigStr, branch, jobType, &commentId, placeholderCommentIdForReport, gitlabProjectId) if err != nil { return nil, nil, fmt.Errorf("failed to create batch: %v", err) } diff --git a/ee/backend/controllers/gitlab.go b/ee/backend/controllers/gitlab.go index dbce399d5..7705b8a98 100644 --- a/ee/backend/controllers/gitlab.go +++ b/ee/backend/controllers/gitlab.go @@ -333,7 +333,7 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab log.Printf("strconv.ParseInt error: %v", err) utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: could not handle commentId: %v", err)) } - batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGitlab, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, 0, branch, prNumber, repoOwner, repoName, repoFullName, commitSha, commentId, diggeryamlStr, projectId) + batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGitlab, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, 0, branch, prNumber, repoOwner, repoName, repoFullName, commitSha, commentId, nil, diggeryamlStr, projectId) if err != nil { log.Printf("ConvertJobsToDiggerJobs error: %v", err) utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err)) @@ -524,7 +524,7 @@ func handleIssueCommentEvent(gitlabProvider utils.GitlabProvider, payload *gitla log.Printf("ParseInt err: %v", err) return fmt.Errorf("parseint error: %v", err) } - batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGitlab, organisationId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, 0, branch, issueNumber, repoOwner, repoName, repoFullName, commitSha, commentId64, diggerYmlStr, projectId) + batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, models.DiggerVCSGitlab, organisationId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, 0, branch, issueNumber, repoOwner, repoName, repoFullName, commitSha, commentId64, nil, diggerYmlStr, projectId) if err != nil { log.Printf("ConvertJobsToDiggerJobs error: %v", err) utils.InitCommentReporter(glService, issueNumber, fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err)) diff --git a/ee/backend/hooks/github.go b/ee/backend/hooks/github.go index dbf8e92b0..2936995b5 100644 --- a/ee/backend/hooks/github.go +++ b/ee/backend/hooks/github.go @@ -151,7 +151,7 @@ var DriftReconcilliationHook ce_controllers.IssueCommentHook = func(gh utils.Git utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: could not handle commentId: %v", err)) } - batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, "github", orgId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, defaultBranch, issueNumber, repoOwner, repoName, repoFullName, "", reporterCommentId, diggerYmlStr, 0) + batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, "github", orgId, impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, defaultBranch, issueNumber, repoOwner, repoName, repoFullName, "", reporterCommentId, nil, diggerYmlStr, 0) if err != nil { log.Printf("ConvertJobsToDiggerJobs error: %v", err) utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err)) diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index 927bb44d6..b0d488694 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -107,6 +107,42 @@ type ReportStrategy interface { Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error) } +type AlwaysSameCommentStrategy struct { + Title string + CommentId string + TimeOfRun time.Time +} + +func (strategy AlwaysSameCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { + comments, err := ciService.GetComments(PrNumber) + if err != nil { + return "", "", fmt.Errorf("error getting comments: %v", err) + } + + var commentBody *string + var commentUrl string + for _, comment := range comments { + if comment.Id == strategy.CommentId { + commentBody = comment.Body + commentUrl = comment.Url + } + } + + var reportTitle string + if strategy.Title != "" { + reportTitle = strategy.Title + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") + } else { + reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") + } + + err = appendToExistingComment(ciService, PrNumber, *commentBody, report, reportTitle, strategy.CommentId, supportsCollapsibleComment) + if err != nil { + return "", "", fmt.Errorf("error while adding to existing comment: %v", err) + } + + return strategy.CommentId, commentUrl, err +} + type CommentPerRunStrategy struct { Title string TimeOfRun time.Time @@ -156,26 +192,37 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, return fmt.Sprintf("%v", comment.Id), comment.Url, nil } + err := appendToExistingComment(ciService, PrNumber, commentBody, report, reportTitle, commentIdForThisRun, supportsCollapsible) + if err != nil { + return "", "", err + } + + return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil + +} + +func appendToExistingComment(ciService ci.PullRequestService, prNumber int, existingCommentBody string, reportToAppend string, reportTitle string, commentId string, supportsCollapsible bool) error { + // strip first and last lines - lines := strings.Split(commentBody, "\n") + lines := strings.Split(existingCommentBody, "\n") lines = lines[1 : len(lines)-1] - commentBody = strings.Join(lines, "\n") + existingCommentBody = strings.Join(lines, "\n") - commentBody = commentBody + "\n\n" + report + "\n" + existingCommentBody = existingCommentBody + "\n\n" + reportToAppend + "\n" var completeComment string if !supportsCollapsible { - completeComment = utils.AsComment(reportTitle)(commentBody) + completeComment = utils.AsComment(reportTitle)(existingCommentBody) } else { - completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody) + completeComment = utils.AsCollapsibleComment(reportTitle, false)(existingCommentBody) } - err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment) + err := ciService.EditComment(prNumber, commentId, completeComment) if err != nil { - return "", "", fmt.Errorf("error editing comment: %v", err) + return fmt.Errorf("error editing comment: %v", err) } - return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil + return nil } type LatestRunCommentStrategy struct { diff --git a/libs/spec/models.go b/libs/spec/models.go index 3637f920b..8f11ae3f0 100644 --- a/libs/spec/models.go +++ b/libs/spec/models.go @@ -5,9 +5,10 @@ import ( ) type ReporterSpec struct { - ReporterType string `json:"reporter_type"` - ReportingStrategy string `json:"reporting_strategy"` - ReportTerraformOutput bool `json:"report_terraform_output"` + ReporterType string `json:"reporter_type"` + ReportingStrategy string `json:"reporting_strategy"` + ReportTerraformOutput bool `json:"report_terraform_output"` + ReportCommentId *string `json:"report_comment_id,omitempty"` } type CommentUpdaterSpec struct { diff --git a/libs/spec/providers.go b/libs/spec/providers.go index 6ad1aac49..fc6efa676 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -125,6 +125,13 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c return reporting.LatestRunCommentStrategy{ TimeOfRun: time.Now(), } + case "always_same_comment": + return reporting.AlwaysSameCommentStrategy{ + Title: title, + TimeOfRun: time.Now(), + CommentId: *reporterSpec.ReportCommentId, + } + default: return reporting.MultipleCommentsStrategy{} } From 151068cfd8ee92dc4c7d724bd303faab02a113e1 Mon Sep 17 00:00:00 2001 From: motatoes Date: Wed, 4 Dec 2024 19:51:53 +0000 Subject: [PATCH 02/13] fix lines logic --- backend/migrations/20241204194016.sql | 2 ++ backend/migrations/atlas.sum | 3 ++- libs/comment_utils/reporting/reporting.go | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 backend/migrations/20241204194016.sql diff --git a/backend/migrations/20241204194016.sql b/backend/migrations/20241204194016.sql new file mode 100644 index 000000000..8fcc81ecf --- /dev/null +++ b/backend/migrations/20241204194016.sql @@ -0,0 +1,2 @@ +-- Modify "digger_batches" table +ALTER TABLE "public"."digger_batches" ADD COLUMN "placeholder_comment_id_for_report" text NULL; diff --git a/backend/migrations/atlas.sum b/backend/migrations/atlas.sum index 878006c16..c054f6099 100644 --- a/backend/migrations/atlas.sum +++ b/backend/migrations/atlas.sum @@ -1,4 +1,4 @@ -h1:fW4YtFhVfirvwGtHxnGXjFwChYhoj/5pW/XaA6rViHw= +h1:SB1sRi1zY0IoKioJ+Y0FtuIjHQv6XCvcsAJg0AEJNs4= 20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw= 20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA= 20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw= @@ -34,3 +34,4 @@ h1:fW4YtFhVfirvwGtHxnGXjFwChYhoj/5pW/XaA6rViHw= 20241107163722.sql h1:tk28AgXggvpEigTkWMYMxIVDPNdEUFGijaFWBqvlZhA= 20241107172343.sql h1:wtM1+uJZY6NiiDYabuzj/LAANAV7+xyUCL5U23v3e+c= 20241114202249.sql h1:LCRf42VUYB/uugR4gwp+fGPUomT0KhFA3eLjBK9JlZo= +20241204194016.sql h1:RgiJkZlAJSJjNCRD5RHmRr+65O9UDCRwGohQwdIMhGg= diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index b0d488694..14dd9f821 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -205,7 +205,9 @@ func appendToExistingComment(ciService ci.PullRequestService, prNumber int, exis // strip first and last lines lines := strings.Split(existingCommentBody, "\n") - lines = lines[1 : len(lines)-1] + if len(lines) > 1 { + lines = lines[1 : len(lines)-1] + } existingCommentBody = strings.Join(lines, "\n") existingCommentBody = existingCommentBody + "\n\n" + reportToAppend + "\n" From 60a7a1316cc5e81ae64294a4739987e5c6047029 Mon Sep 17 00:00:00 2001 From: motatoes Date: Wed, 4 Dec 2024 20:02:45 +0000 Subject: [PATCH 03/13] attempt to append each time --- backend/controllers/github.go | 4 ++-- libs/comment_utils/reporting/reporting.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/controllers/github.go b/backend/controllers/github.go index 51edca65e..ad4250138 100644 --- a/backend/controllers/github.go +++ b/backend/controllers/github.go @@ -500,7 +500,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not handle commentId: %v", err)) } - placeholderComment, err := ghService.PublishComment(prNumber, "") + placeholderComment, err := ghService.PublishComment(prNumber, "digger report placehoder") if err != nil { log.Printf("strconv.ParseInt error: %v", err) commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not create placeholder commentId for report: %v", err)) @@ -882,7 +882,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu return fmt.Errorf("comment reporter error: %v", err) } - placeholderComment, err := ghService.PublishComment(issueNumber, "") + placeholderComment, err := ghService.PublishComment(issueNumber, "digger report placehoder") if err != nil { log.Printf("strconv.ParseInt error: %v", err) commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not create placeholder commentId for report: %v", err)) diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index 14dd9f821..02b0c7854 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -205,9 +205,9 @@ func appendToExistingComment(ciService ci.PullRequestService, prNumber int, exis // strip first and last lines lines := strings.Split(existingCommentBody, "\n") - if len(lines) > 1 { - lines = lines[1 : len(lines)-1] - } + //if len(lines) > 1 { + // lines = lines[1 : len(lines)-1] + //} existingCommentBody = strings.Join(lines, "\n") existingCommentBody = existingCommentBody + "\n\n" + reportToAppend + "\n" From ffa6e556ee97d2e7103058de9293a69660f5c8db Mon Sep 17 00:00:00 2001 From: motatoes Date: Thu, 19 Dec 2024 08:57:30 +0000 Subject: [PATCH 04/13] revert upsert function, remove lazyreporter --- libs/comment_utils/reporting/reporting.go | 118 ++-------------------- libs/spec/providers.go | 2 +- 2 files changed, 10 insertions(+), 110 deletions(-) diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index 02b0c7854..9630c6c06 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -33,57 +33,6 @@ func (ciReporter CiReporter) SupportsMarkdown() bool { return ciReporter.IsSupportMarkdown } -type CiReporterLazy struct { - CiReporter CiReporter - isSuppressed bool - reports []string - formatters []func(report string) string -} - -func NewCiReporterLazy(ciReporter CiReporter) *CiReporterLazy { - return &CiReporterLazy{ - CiReporter: ciReporter, - isSuppressed: false, - reports: []string{}, - formatters: []func(report string) string{}, - } -} - -func (lazyReporter *CiReporterLazy) Report(report string, reportFormatting func(report string) string) (string, string, error) { - lazyReporter.reports = append(lazyReporter.reports, report) - lazyReporter.formatters = append(lazyReporter.formatters, reportFormatting) - return "", "", nil -} - -func (lazyReporter *CiReporterLazy) Flush() (string, string, error) { - if lazyReporter.isSuppressed { - log.Printf("Reporter is suprresed, ignoring messages ...") - return "", "", nil - } - var commentId, commentUrl string - for i, _ := range lazyReporter.formatters { - var err error - commentId, commentUrl, err = lazyReporter.CiReporter.ReportStrategy.Report(lazyReporter.CiReporter.CiService, lazyReporter.CiReporter.PrNumber, lazyReporter.reports[i], lazyReporter.formatters[i], lazyReporter.SupportsMarkdown()) - if err != nil { - log.Printf("failed to report strategy: ") - return "", "", err - } - } - // clear the buffers - lazyReporter.formatters = []func(comment string) string{} - lazyReporter.reports = []string{} - return commentId, commentUrl, nil -} - -func (lazyReporter *CiReporterLazy) Suppress() error { - lazyReporter.isSuppressed = true - return nil -} - -func (lazyReporter *CiReporterLazy) SupportsMarkdown() bool { - return lazyReporter.CiReporter.IsSupportMarkdown -} - type StdOutReporter struct{} func (reporter StdOutReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) { @@ -107,42 +56,6 @@ type ReportStrategy interface { Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error) } -type AlwaysSameCommentStrategy struct { - Title string - CommentId string - TimeOfRun time.Time -} - -func (strategy AlwaysSameCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { - comments, err := ciService.GetComments(PrNumber) - if err != nil { - return "", "", fmt.Errorf("error getting comments: %v", err) - } - - var commentBody *string - var commentUrl string - for _, comment := range comments { - if comment.Id == strategy.CommentId { - commentBody = comment.Body - commentUrl = comment.Url - } - } - - var reportTitle string - if strategy.Title != "" { - reportTitle = strategy.Title + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") - } else { - reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") - } - - err = appendToExistingComment(ciService, PrNumber, *commentBody, report, reportTitle, strategy.CommentId, supportsCollapsibleComment) - if err != nil { - return "", "", fmt.Errorf("error while adding to existing comment: %v", err) - } - - return strategy.CommentId, commentUrl, err -} - type CommentPerRunStrategy struct { Title string TimeOfRun time.Time @@ -192,39 +105,26 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, return fmt.Sprintf("%v", comment.Id), comment.Url, nil } - err := appendToExistingComment(ciService, PrNumber, commentBody, report, reportTitle, commentIdForThisRun, supportsCollapsible) - if err != nil { - return "", "", err - } - - return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil - -} - -func appendToExistingComment(ciService ci.PullRequestService, prNumber int, existingCommentBody string, reportToAppend string, reportTitle string, commentId string, supportsCollapsible bool) error { - // strip first and last lines - lines := strings.Split(existingCommentBody, "\n") - //if len(lines) > 1 { - // lines = lines[1 : len(lines)-1] - //} - existingCommentBody = strings.Join(lines, "\n") + lines := strings.Split(commentBody, "\n") + lines = lines[1 : len(lines)-1] + commentBody = strings.Join(lines, "\n") - existingCommentBody = existingCommentBody + "\n\n" + reportToAppend + "\n" + commentBody = commentBody + "\n\n" + report + "\n" var completeComment string if !supportsCollapsible { - completeComment = utils.AsComment(reportTitle)(existingCommentBody) + completeComment = utils.AsComment(reportTitle)(commentBody) } else { - completeComment = utils.AsCollapsibleComment(reportTitle, false)(existingCommentBody) + completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody) } - err := ciService.EditComment(prNumber, commentId, completeComment) + err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment) if err != nil { - return fmt.Errorf("error editing comment: %v", err) + return "", "", fmt.Errorf("error editing comment: %v", err) } - return nil + return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil } type LatestRunCommentStrategy struct { diff --git a/libs/spec/providers.go b/libs/spec/providers.go index fc6efa676..9fd5f7445 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -156,7 +156,7 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c IsSupportMarkdown: true, ReportStrategy: strategy, } - return reporting.NewCiReporterLazy(ciReporter), nil + return ciReporter, nil default: return reporting.NoopReporter{}, nil } From dfe91e9941e4745231084350cab6615bf799dfa5 Mon Sep 17 00:00:00 2001 From: motatoes Date: Thu, 19 Dec 2024 10:54:00 +0000 Subject: [PATCH 05/13] refactor strategies --- cli/cmd/digger/root.go | 4 --- cli/pkg/digger/digger.go | 18 ++++++------ ee/cli/cmd/digger/root.go | 4 --- libs/comment_utils/reporting/core.go | 2 +- libs/comment_utils/reporting/mock.go | 4 +-- libs/comment_utils/reporting/noop.go | 4 +-- libs/comment_utils/reporting/reporting.go | 34 ++++++++++------------- libs/comment_utils/reporting/utils.go | 8 +++++- libs/execution/execution.go | 16 +++++------ libs/locking/locking.go | 12 ++++---- libs/spec/providers.go | 11 -------- 11 files changed, 49 insertions(+), 68 deletions(-) diff --git a/cli/cmd/digger/root.go b/cli/cmd/digger/root.go index 8b6481a71..a3818c84b 100644 --- a/cli/cmd/digger/root.go +++ b/cli/cmd/digger/root.go @@ -96,10 +96,6 @@ func PreRun(cmd *cobra.Command, args []string) { ReportStrategy = &reporting.CommentPerRunStrategy{ TimeOfRun: time.Now(), } - } else if os.Getenv("REPORTING_STRATEGY") == "latest_run_comment" { - ReportStrategy = &reporting.LatestRunCommentStrategy{ - TimeOfRun: time.Now(), - } } else { ReportStrategy = &reporting.MultipleCommentsStrategy{} } diff --git a/cli/pkg/digger/digger.go b/cli/pkg/digger/digger.go index b7c7a1e33..b97d3adc6 100644 --- a/cli/pkg/digger/digger.go +++ b/cli/pkg/digger/digger.go @@ -170,12 +170,12 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic func reportPolicyError(projectName string, command string, requestedBy string, reporter reporting.Reporter) string { msg := fmt.Sprintf("User %s is not allowed to perform action: %s. Check your policies :x:", requestedBy, command) if reporter.SupportsMarkdown() { - _, _, err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command), false)) + err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command), false)) if err != nil { log.Printf("Error publishing comment: %v", err) } } else { - _, _, err := reporter.Report(msg, coreutils.AsComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command))) + err := reporter.Report(msg, coreutils.AsComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command))) if err != nil { log.Printf("Error publishing comment: %v", err) } @@ -311,7 +311,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org preformattedMessaged = append(preformattedMessaged, fmt.Sprintf(" %v", message)) } planReportMessage = planReportMessage + strings.Join(preformattedMessaged, "
") - _, _, err = reporter.Report(planReportMessage, planPolicyFormatter) + err = reporter.Report(planReportMessage, planPolicyFormatter) if err != nil { log.Printf("Failed to report plan. %v", err) @@ -320,7 +320,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org log.Printf(msg) return nil, msg, fmt.Errorf(msg) } else { - _, _, err := reporter.Report("Terraform plan validation checks succeeded :white_check_mark:", planPolicyFormatter) + err := reporter.Report("Terraform plan validation checks succeeded :white_check_mark:", planPolicyFormatter) if err != nil { log.Printf("Failed to report plan. %v", err) } @@ -496,12 +496,12 @@ func reportApplyMergeabilityError(reporter reporting.Reporter) string { log.Println(comment) if reporter.SupportsMarkdown() { - _, _, err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error", false)) + err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error", false)) if err != nil { log.Printf("error publishing comment: %v\n", err) } } else { - _, _, err := reporter.Report(comment, coreutils.AsComment("Apply error")) + err := reporter.Report(comment, coreutils.AsComment("Apply error")) if err != nil { log.Printf("error publishing comment: %v\n", err) } @@ -518,7 +518,7 @@ func reportTerraformPlanOutput(reporter reporting.Reporter, projectId string, pl formatter = coreutils.GetTerraformOutputAsComment("Plan output") } - _, _, err := reporter.Report(plan, formatter) + err := reporter.Report(plan, formatter) if err != nil { log.Printf("Failed to report plan. %v", err) } @@ -533,7 +533,7 @@ func reportPlanSummary(reporter reporting.Reporter, summary string) { formatter = coreutils.AsComment("Plan summary") } - _, _, err := reporter.Report("\n"+summary, formatter) + err := reporter.Report("\n"+summary, formatter) if err != nil { log.Printf("Failed to report plan summary. %v", err) } @@ -543,7 +543,7 @@ func reportEmptyPlanOutput(reporter reporting.Reporter, projectId string) { identityFormatter := func(comment string) string { return comment } - _, _, err := reporter.Report("→ No changes in terraform output for "+projectId, identityFormatter) + err := reporter.Report("→ No changes in terraform output for "+projectId, identityFormatter) // suppress the comment (if reporter is suppressible) reporter.Suppress() if err != nil { diff --git a/ee/cli/cmd/digger/root.go b/ee/cli/cmd/digger/root.go index c89d932c2..e8ea8200e 100644 --- a/ee/cli/cmd/digger/root.go +++ b/ee/cli/cmd/digger/root.go @@ -90,10 +90,6 @@ func PreRun(cmd *cobra.Command, args []string) { ReportStrategy = &reporting.CommentPerRunStrategy{ TimeOfRun: time.Now(), } - } else if os.Getenv("REPORTING_STRATEGY") == "latest_run_comment" { - ReportStrategy = &reporting.LatestRunCommentStrategy{ - TimeOfRun: time.Now(), - } } else { ReportStrategy = &reporting.MultipleCommentsStrategy{} } diff --git a/libs/comment_utils/reporting/core.go b/libs/comment_utils/reporting/core.go index adfdbdce1..a891f2872 100644 --- a/libs/comment_utils/reporting/core.go +++ b/libs/comment_utils/reporting/core.go @@ -1,7 +1,7 @@ package reporting type Reporter interface { - Report(report string, reportFormatting func(report string) string) (commentId string, commentUrl string, error error) + Report(report string, reportFormatting func(report string) string) (error error) Flush() (string, string, error) Suppress() error SupportsMarkdown() bool diff --git a/libs/comment_utils/reporting/mock.go b/libs/comment_utils/reporting/mock.go index b68c9eb28..a32bcf4e6 100644 --- a/libs/comment_utils/reporting/mock.go +++ b/libs/comment_utils/reporting/mock.go @@ -4,9 +4,9 @@ type MockReporter struct { commands []string } -func (mockReporter *MockReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) { +func (mockReporter *MockReporter) Report(report string, reportFormatting func(report string) string) error { mockReporter.commands = append(mockReporter.commands, "Report") - return "", "", nil + return nil } func (mockReporter *MockReporter) Flush() (string, string, error) { diff --git a/libs/comment_utils/reporting/noop.go b/libs/comment_utils/reporting/noop.go index d0d68462d..5ea15405e 100644 --- a/libs/comment_utils/reporting/noop.go +++ b/libs/comment_utils/reporting/noop.go @@ -2,8 +2,8 @@ package reporting type NoopReporter struct{} -func (reporter NoopReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) { - return "", "", nil +func (reporter NoopReporter) Report(report string, reportFormatting func(report string) string) error { + return nil } func (reporter NoopReporter) Flush() (string, string, error) { diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index 9630c6c06..15da6ffbd 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -16,9 +16,9 @@ type CiReporter struct { ReportStrategy ReportStrategy } -func (ciReporter CiReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) { - commentId, commentUrl, err := ciReporter.ReportStrategy.Report(ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown()) - return commentId, commentUrl, err +func (ciReporter CiReporter) Report(report string, reportFormatting func(report string) string) error { + _, _, err := ciReporter.ReportStrategy.Report(ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown()) + return err } func (ciReporter CiReporter) Flush() (string, string, error) { @@ -35,9 +35,9 @@ func (ciReporter CiReporter) SupportsMarkdown() bool { type StdOutReporter struct{} -func (reporter StdOutReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) { +func (reporter StdOutReporter) Report(report string, reportFormatting func(report string) string) error { log.Printf("Info: %v", report) - return "", "", nil + return nil } func (reporter StdOutReporter) Flush() (string, string, error) { @@ -54,6 +54,7 @@ func (reporter StdOutReporter) Suppress() error { type ReportStrategy interface { Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error) + Flush() (string, string, error) } type CommentPerRunStrategy struct { @@ -77,6 +78,10 @@ func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, Pr return commentId, commentUrl, err } +func (s CommentPerRunStrategy) Flush() (string, string, error) { + return "", "", nil +} + func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, comments []ci.Comment, reportTitle string, supportsCollapsible bool) (string, string, error) { report = reportFormatter(report) commentIdForThisRun := "" @@ -127,24 +132,13 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil } -type LatestRunCommentStrategy struct { - TimeOfRun time.Time -} - -func (strategy LatestRunCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { - comments, err := ciService.GetComments(PrNumber) - if err != nil { - return "", "", fmt.Errorf("error getting comments: %v", err) - } - - reportTitle := "Digger latest run report" - commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment) - return commentId, commentUrl, err -} - type MultipleCommentsStrategy struct{} func (strategy MultipleCommentsStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { _, err := ciService.PublishComment(PrNumber, reportFormatter(report)) return "", "", err } + +func (s MultipleCommentsStrategy) Flush() (string, string, error) { + return "", "", nil +} diff --git a/libs/comment_utils/reporting/utils.go b/libs/comment_utils/reporting/utils.go index b48e8912e..e909c53a6 100644 --- a/libs/comment_utils/reporting/utils.go +++ b/libs/comment_utils/reporting/utils.go @@ -30,7 +30,13 @@ func PostInitialSourceComments(ghService ci.PullRequestService, prNumber int, im CiService: ghService, ReportStrategy: CommentPerRunStrategy{fmt.Sprintf("Report for location: %v", location), time.Now()}, } - commentId, _, err := reporter.Report("Comment Reporter", func(report string) string { return "" }) + err := reporter.Report("Comment Reporter", func(report string) string { return "" }) + if err != nil { + log.Printf("Error reporting source module comment: %v", err) + return nil, fmt.Errorf("error reporting source module comment: %v", err) + } + + commentId, _, err := reporter.Flush() if err != nil { log.Printf("Error reporting source module comment: %v", err) return nil, fmt.Errorf("error reporting source module comment: %v", err) diff --git a/libs/execution/execution.go b/libs/execution/execution.go index a3dbea138..3233e3065 100644 --- a/libs/execution/execution.go +++ b/libs/execution/execution.go @@ -294,12 +294,12 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin func reportError(r reporting.Reporter, stderr string) { if r.SupportsMarkdown() { - _, _, commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.", false)) + commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } } else { - _, _, commentErr := r.Report(stderr, utils.AsComment("Error during init.")) + commentErr := r.Report(stderr, utils.AsComment("Error during init.")) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } @@ -376,12 +376,12 @@ func (d DiggerExecutor) Apply() (*iac_utils.IacSummary, bool, string, error) { func reportApplyError(r reporting.Reporter, err error) { if r.SupportsMarkdown() { - _, _, commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.", false)) + commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", err) } } else { - _, _, commentErr := r.Report(err.Error(), utils.AsComment("Error during applying.")) + commentErr := r.Report(err.Error(), utils.AsComment("Error during applying.")) if commentErr != nil { log.Printf("error publishing comment: %v", err) } @@ -396,7 +396,7 @@ func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOut formatter = utils.GetTerraformOutputAsComment("Apply output") } - _, _, commentErr := r.Report(applyOutput, formatter) + commentErr := r.Report(applyOutput, formatter) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } @@ -404,12 +404,12 @@ func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOut func reportTerraformError(r reporting.Reporter, stderr string) { if r.SupportsMarkdown() { - _, _, commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false)) + commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } } else { - _, _, commentErr := r.Report(stderr, utils.GetTerraformOutputAsComment("Error during init.")) + commentErr := r.Report(stderr, utils.GetTerraformOutputAsComment("Error during init.")) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } @@ -428,7 +428,7 @@ func reportAdditionalOutput(r reporting.Reporter, projectId string) { output, _ := os.ReadFile(diggerOutPath) outputStr := string(output) if len(outputStr) > 0 { - _, _, commentErr := r.Report(outputStr, formatter) + commentErr := r.Report(outputStr, formatter) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } diff --git a/libs/locking/locking.go b/libs/locking/locking.go index 197059ebf..8ec28bf47 100644 --- a/libs/locking/locking.go +++ b/libs/locking/locking.go @@ -96,12 +96,12 @@ func (projectLock *PullRequestLock) Lock() (bool, error) { func reportingLockingSuccess(r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - _, _, err := r.Report(comment, utils.AsCollapsibleComment("Locking successful", false)) + err := r.Report(comment, utils.AsCollapsibleComment("Locking successful", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } } else { - _, _, err := r.Report(comment, utils.AsComment("Locking successful")) + err := r.Report(comment, utils.AsComment("Locking successful")) if err != nil { log.Println("failed to publish comment: " + err.Error()) } @@ -110,12 +110,12 @@ func reportingLockingSuccess(r reporting.Reporter, comment string) { func reportLockingFailed(r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - _, _, err := r.Report(comment, utils.AsCollapsibleComment("Locking failed", false)) + err := r.Report(comment, utils.AsCollapsibleComment("Locking failed", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } } else { - _, _, err := r.Report(comment, utils.AsComment("Locking failed")) + err := r.Report(comment, utils.AsComment("Locking failed")) if err != nil { log.Println("failed to publish comment: " + err.Error()) } @@ -183,12 +183,12 @@ func (projectLock *PullRequestLock) Unlock() (bool, error) { func reportSuccessfulUnlocking(r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - _, _, err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful", false)) + err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } } else { - _, _, err := r.Report(comment, utils.AsComment("Unlocking successful")) + err := r.Report(comment, utils.AsComment("Unlocking successful")) if err != nil { log.Println("failed to publish comment: " + err.Error()) } diff --git a/libs/spec/providers.go b/libs/spec/providers.go index 9fd5f7445..5c8a8103b 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -121,17 +121,6 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c Title: title, TimeOfRun: time.Now(), } - case "latest_run_comment": - return reporting.LatestRunCommentStrategy{ - TimeOfRun: time.Now(), - } - case "always_same_comment": - return reporting.AlwaysSameCommentStrategy{ - Title: title, - TimeOfRun: time.Now(), - CommentId: *reporterSpec.ReportCommentId, - } - default: return reporting.MultipleCommentsStrategy{} } From 5a33821a6c239d8ea3a3fea88d389f73a6bf3e04 Mon Sep 17 00:00:00 2001 From: motatoes Date: Thu, 19 Dec 2024 17:50:30 +0000 Subject: [PATCH 06/13] temp commit --- cli/cmd/digger/root.go | 2 +- ee/cli/cmd/digger/root.go | 2 +- libs/comment_utils/reporting/reporting.go | 46 +++++++++++++++-------- libs/comment_utils/reporting/utils.go | 2 +- libs/spec/providers.go | 2 +- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/cli/cmd/digger/root.go b/cli/cmd/digger/root.go index a3818c84b..1289d4c02 100644 --- a/cli/cmd/digger/root.go +++ b/cli/cmd/digger/root.go @@ -93,7 +93,7 @@ func PreRun(cmd *cobra.Command, args []string) { //PolicyChecker = policy.NewPolicyChecker(hostName, orgName, token) if os.Getenv("REPORTING_STRATEGY") == "comments_per_run" || os.Getenv("ACCUMULATE_PLANS") == "true" { - ReportStrategy = &reporting.CommentPerRunStrategy{ + ReportStrategy = &reporting.SingleCommentStrategy{ TimeOfRun: time.Now(), } } else { diff --git a/ee/cli/cmd/digger/root.go b/ee/cli/cmd/digger/root.go index e8ea8200e..2e271512e 100644 --- a/ee/cli/cmd/digger/root.go +++ b/ee/cli/cmd/digger/root.go @@ -87,7 +87,7 @@ func PreRun(cmd *cobra.Command, args []string) { BackendApi = NewBackendApi(hostName, token) if os.Getenv("REPORTING_STRATEGY") == "comments_per_run" || os.Getenv("ACCUMULATE_PLANS") == "true" { - ReportStrategy = &reporting.CommentPerRunStrategy{ + ReportStrategy = &reporting.SingleCommentStrategy{ TimeOfRun: time.Now(), } } else { diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index 15da6ffbd..ab62cc35b 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -17,7 +17,7 @@ type CiReporter struct { } func (ciReporter CiReporter) Report(report string, reportFormatting func(report string) string) error { - _, _, err := ciReporter.ReportStrategy.Report(ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown()) + _, _, err := ciReporter.ReportStrategy.Report("", ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown()) return err } @@ -53,16 +53,30 @@ func (reporter StdOutReporter) Suppress() error { } type ReportStrategy interface { - Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error) - Flush() (string, string, error) + Report(projectName string, report string, reportFormatter func(report string) string) (error error) + Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error) } -type CommentPerRunStrategy struct { - Title string - TimeOfRun time.Time +type ReportFormat struct { + ReportTitle string + ReportFormatter func(report string) string } -func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { +type SingleCommentStrategy struct { + Title string + TimeOfRun time.Time + Formatters map[string]ReportFormat +} + +func (strategy SingleCommentStrategy) Report(projectName string, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) error { + strategy.Formatters[projectName] = ReportFormat{ + ReportTitle: report, + ReportFormatter: reportFormatter, + } + return nil +} + +func (strategy SingleCommentStrategy) Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (string, string, error) { comments, err := ciService.GetComments(PrNumber) if err != nil { return "", "", fmt.Errorf("error getting comments: %v", err) @@ -76,9 +90,6 @@ func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, Pr } commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment) return commentId, commentUrl, err -} - -func (s CommentPerRunStrategy) Flush() (string, string, error) { return "", "", nil } @@ -132,13 +143,18 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil } -type MultipleCommentsStrategy struct{} +type MultipleCommentsStrategy struct { + Formatters map[string]ReportFormat +} -func (strategy MultipleCommentsStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { - _, err := ciService.PublishComment(PrNumber, reportFormatter(report)) - return "", "", err +func (strategy MultipleCommentsStrategy) Report(projectName string, report string, reportFormatter func(report string) string) error { + strategy.Formatters[projectName] = ReportFormat{ + ReportTitle: report, + ReportFormatter: reportFormatter, + } + return nil } -func (s MultipleCommentsStrategy) Flush() (string, string, error) { +func (strategy MultipleCommentsStrategy) Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (string, string, error) { return "", "", nil } diff --git a/libs/comment_utils/reporting/utils.go b/libs/comment_utils/reporting/utils.go index e909c53a6..c99118090 100644 --- a/libs/comment_utils/reporting/utils.go +++ b/libs/comment_utils/reporting/utils.go @@ -28,7 +28,7 @@ func PostInitialSourceComments(ghService ci.PullRequestService, prNumber int, im reporter := CiReporter{ PrNumber: prNumber, CiService: ghService, - ReportStrategy: CommentPerRunStrategy{fmt.Sprintf("Report for location: %v", location), time.Now()}, + ReportStrategy: SingleCommentStrategy{fmt.Sprintf("Report for location: %v", location), time.Now()}, } err := reporter.Report("Comment Reporter", func(report string) string { return "" }) if err != nil { diff --git a/libs/spec/providers.go b/libs/spec/providers.go index 5c8a8103b..75eaa9ff4 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -117,7 +117,7 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c getStrategy := func(strategy string) reporting.ReportStrategy { switch reporterSpec.ReportingStrategy { case "comments_per_run": - return reporting.CommentPerRunStrategy{ + return reporting.SingleCommentStrategy{ Title: title, TimeOfRun: time.Now(), } From eac4651678767e1675327b19f229bffd88a6c913 Mon Sep 17 00:00:00 2001 From: motatoes Date: Fri, 20 Dec 2024 19:29:30 +0000 Subject: [PATCH 07/13] refactoring all the strategies --- cli/cmd/digger/main_test.go | 14 +- cli/cmd/digger/root.go | 9 +- cli/pkg/digger/digger.go | 39 ++--- cli/pkg/digger/digger_test.go | 6 +- cli/pkg/integration/integration_test.go | 3 +- ee/cli/cmd/digger/root.go | 9 +- libs/comment_utils/reporting/core.go | 6 +- libs/comment_utils/reporting/mock.go | 8 +- libs/comment_utils/reporting/noop.go | 8 +- libs/comment_utils/reporting/reporting.go | 192 +++++++++++++--------- libs/comment_utils/reporting/utils.go | 10 +- libs/execution/execution.go | 44 ++--- libs/locking/locking.go | 28 ++-- libs/spec/providers.go | 8 +- 14 files changed, 208 insertions(+), 176 deletions(-) diff --git a/cli/cmd/digger/main_test.go b/cli/cmd/digger/main_test.go index 6b3e5352f..fa2e190a8 100644 --- a/cli/cmd/digger/main_test.go +++ b/cli/cmd/digger/main_test.go @@ -895,8 +895,11 @@ func TestGitHubNewPullRequestContext(t *testing.T) { impactedProjects, requestedProject, prNumber, err := dggithub.ProcessGitHubEvent(ghEvent, &diggerConfig, &prManager) reporter := &reporting.CiReporter{ - CiService: &prManager, - PrNumber: prNumber, + CiService: &prManager, + PrNumber: prNumber, + IsSupportMarkdown: true, + IsSuppressed: false, + ReportStrategy: reporting.NewMultipleCommentsStrategy(), } event := context.Event.(github.PullRequestEvent) @@ -923,8 +926,11 @@ func TestGitHubNewCommentContext(t *testing.T) { planStorage := storage.MockPlanStorage{} impactedProjects, requestedProject, prNumber, err := dggithub.ProcessGitHubEvent(ghEvent, &diggerConfig, &prManager) reporter := &reporting.CiReporter{ - CiService: &prManager, - PrNumber: prNumber, + CiService: &prManager, + PrNumber: prNumber, + ReportStrategy: reporting.NewMultipleCommentsStrategy(), + IsSuppressed: false, + IsSupportMarkdown: true, } policyChecker := policy.MockPolicyChecker{} diff --git a/cli/cmd/digger/root.go b/cli/cmd/digger/root.go index 1289d4c02..5afb3c551 100644 --- a/cli/cmd/digger/root.go +++ b/cli/cmd/digger/root.go @@ -14,7 +14,6 @@ import ( "log" "net/http" "os" - "time" ) type RunConfig struct { @@ -93,11 +92,11 @@ func PreRun(cmd *cobra.Command, args []string) { //PolicyChecker = policy.NewPolicyChecker(hostName, orgName, token) if os.Getenv("REPORTING_STRATEGY") == "comments_per_run" || os.Getenv("ACCUMULATE_PLANS") == "true" { - ReportStrategy = &reporting.SingleCommentStrategy{ - TimeOfRun: time.Now(), - } + strategy := reporting.NewSingleCommentStrategy() + ReportStrategy = &strategy } else { - ReportStrategy = &reporting.MultipleCommentsStrategy{} + strategy := reporting.NewMultipleCommentsStrategy() + ReportStrategy = &strategy } var err error diff --git a/cli/pkg/digger/digger.go b/cli/pkg/digger/digger.go index b97d3adc6..3179e0510 100644 --- a/cli/pkg/digger/digger.go +++ b/cli/pkg/digger/digger.go @@ -124,13 +124,14 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic } if allAppliesSuccess == true && reportFinalStatusToBackend == true { - _, jobPrCommentUrl, err := reporter.Flush() + _, jobPrCommentUrls, err := reporter.Flush() if err != nil { log.Printf("error while sending job comments %v", err) return false, false, fmt.Errorf("error while sending job comments %v", err) } currentJob := jobs[0] + jobPrCommentUrl := jobPrCommentUrls[0] repoNameForBackendReporting := strings.ReplaceAll(currentJob.Namespace, "/", "-") projectNameForBackendReporting := currentJob.ProjectName // TODO: handle the apply result summary as well to report it to backend. Possibly reporting changed resources as well @@ -170,12 +171,12 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic func reportPolicyError(projectName string, command string, requestedBy string, reporter reporting.Reporter) string { msg := fmt.Sprintf("User %s is not allowed to perform action: %s. Check your policies :x:", requestedBy, command) if reporter.SupportsMarkdown() { - err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command), false)) + err := reporter.Report(projectName, msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command), false)) if err != nil { log.Printf("Error publishing comment: %v", err) } } else { - err := reporter.Report(msg, coreutils.AsComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command))) + err := reporter.Report(projectName, msg, coreutils.AsComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command))) if err != nil { log.Printf("Error publishing comment: %v", err) } @@ -284,7 +285,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org return nil, msg, fmt.Errorf(msg) } else if planPerformed { if isNonEmptyPlan { - reportTerraformPlanOutput(reporter, projectLock.LockId(), plan) + reportTerraformPlanOutput(reporter, job.ProjectName, plan) planIsAllowed, messages, err := policyChecker.CheckPlanPolicy(SCMrepository, SCMOrganisation, job.ProjectName, job.ProjectDir, planJsonOutput) if err != nil { msg := fmt.Sprintf("Failed to validate plan. %v", err) @@ -311,7 +312,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org preformattedMessaged = append(preformattedMessaged, fmt.Sprintf(" %v", message)) } planReportMessage = planReportMessage + strings.Join(preformattedMessaged, "
") - err = reporter.Report(planReportMessage, planPolicyFormatter) + err = reporter.Report(job.ProjectName, planReportMessage, planPolicyFormatter) if err != nil { log.Printf("Failed to report plan. %v", err) @@ -320,14 +321,14 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org log.Printf(msg) return nil, msg, fmt.Errorf(msg) } else { - err := reporter.Report("Terraform plan validation checks succeeded :white_check_mark:", planPolicyFormatter) + err := reporter.Report(job.ProjectName, "Terraform plan validation checks succeeded :white_check_mark:", planPolicyFormatter) if err != nil { log.Printf("Failed to report plan. %v", err) } - reportPlanSummary(reporter, planSummary) + reportPlanSummary(job.ProjectName, reporter, planSummary) } } else { - reportEmptyPlanOutput(reporter, projectLock.LockId()) + reportEmptyPlanOutput(job.ProjectName, reporter, projectLock.LockId()) } err := prService.SetStatus(*job.PullRequestNumber, "success", job.ProjectName+"/plan") if err != nil { @@ -370,7 +371,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org } log.Printf("PR status, mergeable: %v, merged: %v and skipMergeCheck %v\n", isMergeable, isMerged, job.SkipMergeCheck) if !isMergeable && !isMerged && !job.SkipMergeCheck { - comment := reportApplyMergeabilityError(reporter) + comment := reportApplyMergeabilityError(job.ProjectName, reporter) prService.SetStatus(*job.PullRequestNumber, "failure", job.ProjectName+"/apply") return nil, comment, fmt.Errorf(comment) @@ -491,17 +492,17 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org return &execution.DiggerExecutorResult{}, "", nil } -func reportApplyMergeabilityError(reporter reporting.Reporter) string { +func reportApplyMergeabilityError(projectName string, reporter reporting.Reporter) string { comment := "cannot perform Apply since the PR is not currently mergeable" log.Println(comment) if reporter.SupportsMarkdown() { - err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error", false)) + err := reporter.Report(projectName, comment, coreutils.AsCollapsibleComment("Apply error", false)) if err != nil { log.Printf("error publishing comment: %v\n", err) } } else { - err := reporter.Report(comment, coreutils.AsComment("Apply error")) + err := reporter.Report(projectName, comment, coreutils.AsComment("Apply error")) if err != nil { log.Printf("error publishing comment: %v\n", err) } @@ -509,7 +510,7 @@ func reportApplyMergeabilityError(reporter reporting.Reporter) string { return comment } -func reportTerraformPlanOutput(reporter reporting.Reporter, projectId string, plan string) { +func reportTerraformPlanOutput(reporter reporting.Reporter, projectName string, plan string) { var formatter func(string) string if reporter.SupportsMarkdown() { @@ -518,13 +519,13 @@ func reportTerraformPlanOutput(reporter reporting.Reporter, projectId string, pl formatter = coreutils.GetTerraformOutputAsComment("Plan output") } - err := reporter.Report(plan, formatter) + err := reporter.Report(projectName, plan, formatter) if err != nil { log.Printf("Failed to report plan. %v", err) } } -func reportPlanSummary(reporter reporting.Reporter, summary string) { +func reportPlanSummary(projectName string, reporter reporting.Reporter, summary string) { var formatter func(string) string if reporter.SupportsMarkdown() { @@ -533,19 +534,19 @@ func reportPlanSummary(reporter reporting.Reporter, summary string) { formatter = coreutils.AsComment("Plan summary") } - err := reporter.Report("\n"+summary, formatter) + err := reporter.Report(projectName, "\n"+summary, formatter) if err != nil { log.Printf("Failed to report plan summary. %v", err) } } -func reportEmptyPlanOutput(reporter reporting.Reporter, projectId string) { +func reportEmptyPlanOutput(projectName string, reporter reporting.Reporter, projectId string) { identityFormatter := func(comment string) string { return comment } - err := reporter.Report("→ No changes in terraform output for "+projectId, identityFormatter) + err := reporter.Report(projectName, "→ No changes in terraform output for "+projectId, identityFormatter) // suppress the comment (if reporter is suppressible) - reporter.Suppress() + reporter.Suppress("") if err != nil { log.Printf("Failed to report plan. %v", err) } diff --git a/cli/pkg/digger/digger_test.go b/cli/pkg/digger/digger_test.go index aaba37658..7a52d24e5 100644 --- a/cli/pkg/digger/digger_test.go +++ b/cli/pkg/digger/digger_test.go @@ -247,10 +247,11 @@ func TestCorrectCommandExecutionWhenApplying(t *testing.T) { prManager := &MockPRManager{} lock := &MockProjectLock{} planStorage := &MockPlanStorage{} + strategy := reporting.NewMultipleCommentsStrategy() reporter := &reporting.CiReporter{ CiService: prManager, PrNumber: 1, - ReportStrategy: &reporting.MultipleCommentsStrategy{}, + ReportStrategy: &strategy, IsSupportMarkdown: true, } planPathProvider := &MockPlanPathProvider{} @@ -297,10 +298,11 @@ func TestCorrectCommandExecutionWhenDestroying(t *testing.T) { prManager := &MockPRManager{} lock := &MockProjectLock{} planStorage := &MockPlanStorage{} + strategy := reporting.NewMultipleCommentsStrategy() reporter := &reporting.CiReporter{ CiService: prManager, PrNumber: 1, - ReportStrategy: &reporting.MultipleCommentsStrategy{}, + ReportStrategy: &strategy, } planPathProvider := &MockPlanPathProvider{} executor := execution.DiggerExecutor{ diff --git a/cli/pkg/integration/integration_test.go b/cli/pkg/integration/integration_test.go index 9c45bf4a2..d9379b4b3 100644 --- a/cli/pkg/integration/integration_test.go +++ b/cli/pkg/integration/integration_test.go @@ -44,10 +44,11 @@ func getProjectLockForTests() (error, *locking.PullRequestLock) { repositoryName := "test_dynamodb_lock" ghToken := "token" githubPrService, _ := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner) + strategy := reporting.NewMultipleCommentsStrategy() reporter := reporting.CiReporter{ CiService: &githubPrService, PrNumber: 1, - ReportStrategy: &reporting.MultipleCommentsStrategy{}, + ReportStrategy: &strategy, } projectLock := &locking.PullRequestLock{ diff --git a/ee/cli/cmd/digger/root.go b/ee/cli/cmd/digger/root.go index 2e271512e..3b2bba19f 100644 --- a/ee/cli/cmd/digger/root.go +++ b/ee/cli/cmd/digger/root.go @@ -14,7 +14,6 @@ import ( "log" "net/http" "os" - "time" ) type RunConfig struct { @@ -87,11 +86,11 @@ func PreRun(cmd *cobra.Command, args []string) { BackendApi = NewBackendApi(hostName, token) if os.Getenv("REPORTING_STRATEGY") == "comments_per_run" || os.Getenv("ACCUMULATE_PLANS") == "true" { - ReportStrategy = &reporting.SingleCommentStrategy{ - TimeOfRun: time.Now(), - } + strategy := reporting.NewSingleCommentStrategy() + ReportStrategy = &strategy } else { - ReportStrategy = &reporting.MultipleCommentsStrategy{} + strategy := reporting.NewMultipleCommentsStrategy() + ReportStrategy = &strategy } var err error diff --git a/libs/comment_utils/reporting/core.go b/libs/comment_utils/reporting/core.go index a891f2872..9dd5bcdef 100644 --- a/libs/comment_utils/reporting/core.go +++ b/libs/comment_utils/reporting/core.go @@ -1,8 +1,8 @@ package reporting type Reporter interface { - Report(report string, reportFormatting func(report string) string) (error error) - Flush() (string, string, error) - Suppress() error + Report(projectName string, report string, reportFormatting func(report string) string) (error error) + Flush() ([]string, []string, error) + Suppress(projectName string) error SupportsMarkdown() bool } diff --git a/libs/comment_utils/reporting/mock.go b/libs/comment_utils/reporting/mock.go index a32bcf4e6..6d7e4d943 100644 --- a/libs/comment_utils/reporting/mock.go +++ b/libs/comment_utils/reporting/mock.go @@ -4,16 +4,16 @@ type MockReporter struct { commands []string } -func (mockReporter *MockReporter) Report(report string, reportFormatting func(report string) string) error { +func (mockReporter *MockReporter) Report(projectName string, report string, reportFormatting func(report string) string) error { mockReporter.commands = append(mockReporter.commands, "Report") return nil } -func (mockReporter *MockReporter) Flush() (string, string, error) { - return "", "", nil +func (mockReporter *MockReporter) Flush() ([]string, []string, error) { + return []string{}, []string{}, nil } -func (mockReporter *MockReporter) Suppress() error { +func (mockReporter *MockReporter) Suppress(string) error { return nil } diff --git a/libs/comment_utils/reporting/noop.go b/libs/comment_utils/reporting/noop.go index 5ea15405e..db92a00fd 100644 --- a/libs/comment_utils/reporting/noop.go +++ b/libs/comment_utils/reporting/noop.go @@ -2,18 +2,18 @@ package reporting type NoopReporter struct{} -func (reporter NoopReporter) Report(report string, reportFormatting func(report string) string) error { +func (reporter NoopReporter) Report(projectName string, report string, reportFormatting func(report string) string) error { return nil } -func (reporter NoopReporter) Flush() (string, string, error) { - return "", "", nil +func (reporter NoopReporter) Flush() ([]string, []string, error) { + return []string{}, []string{}, nil } func (reporter NoopReporter) SupportsMarkdown() bool { return false } -func (reporter NoopReporter) Suppress() error { +func (reporter NoopReporter) Suppress(string) error { return nil } diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index ab62cc35b..e69773fec 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -5,7 +5,6 @@ import ( "github.com/diggerhq/digger/libs/ci" "github.com/diggerhq/digger/libs/comment_utils/utils" "log" - "strings" "time" ) @@ -13,20 +12,22 @@ type CiReporter struct { CiService ci.PullRequestService PrNumber int IsSupportMarkdown bool + IsSuppressed bool ReportStrategy ReportStrategy } -func (ciReporter CiReporter) Report(report string, reportFormatting func(report string) string) error { - _, _, err := ciReporter.ReportStrategy.Report("", ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown()) +func (ciReporter CiReporter) Report(projectName string, report string, reportFormatting func(report string) string) error { + err := ciReporter.ReportStrategy.Report(projectName, report, reportFormatting) return err } -func (ciReporter CiReporter) Flush() (string, string, error) { - return "", "", nil +func (ciReporter CiReporter) Flush() ([]string, []string, error) { + commentIds, commentUrls, err := ciReporter.ReportStrategy.Flush(ciReporter.CiService, ciReporter.PrNumber, ciReporter.IsSupportMarkdown) + return commentIds, commentUrls, err } -func (ciReporter CiReporter) Suppress() error { - return nil +func (ciReporter CiReporter) Suppress(projectName string) error { + return ciReporter.ReportStrategy.Suppress(projectName) } func (ciReporter CiReporter) SupportsMarkdown() bool { @@ -35,126 +36,153 @@ func (ciReporter CiReporter) SupportsMarkdown() bool { type StdOutReporter struct{} -func (reporter StdOutReporter) Report(report string, reportFormatting func(report string) string) error { +func (reporter StdOutReporter) Report(projectName string, report string, reportFormatting func(report string) string) error { log.Printf("Info: %v", report) return nil } -func (reporter StdOutReporter) Flush() (string, string, error) { - return "", "", nil +func (reporter StdOutReporter) Flush() ([]string, []string, error) { + return []string{}, []string{}, nil } func (reporter StdOutReporter) SupportsMarkdown() bool { return false } -func (reporter StdOutReporter) Suppress() error { +func (reporter StdOutReporter) Suppress(string) error { return nil } type ReportStrategy interface { Report(projectName string, report string, reportFormatter func(report string) string) (error error) - Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error) + Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (commentId []string, commentUrl []string, error error) + Suppress(projectName string) error } type ReportFormat struct { - ReportTitle string + Report string ReportFormatter func(report string) string } type SingleCommentStrategy struct { - Title string TimeOfRun time.Time - Formatters map[string]ReportFormat + formatters map[string][]ReportFormat } -func (strategy SingleCommentStrategy) Report(projectName string, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) error { - strategy.Formatters[projectName] = ReportFormat{ - ReportTitle: report, - ReportFormatter: reportFormatter, +func NewSingleCommentStrategy() SingleCommentStrategy { + return SingleCommentStrategy{ + TimeOfRun: time.Now(), + formatters: make(map[string][]ReportFormat), } - return nil } -func (strategy SingleCommentStrategy) Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (string, string, error) { - comments, err := ciService.GetComments(PrNumber) - if err != nil { - return "", "", fmt.Errorf("error getting comments: %v", err) +func (strategy SingleCommentStrategy) Report(projectName string, report string, reportFormatter func(report string) string) error { + if _, exists := strategy.formatters[projectName]; !exists { + strategy.formatters[projectName] = []ReportFormat{} } + strategy.formatters[projectName] = append(strategy.formatters[projectName], ReportFormat{ + Report: report, + ReportFormatter: reportFormatter, + }) - var reportTitle string - if strategy.Title != "" { - reportTitle = strategy.Title + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") - } else { - reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") - } - commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment) - return commentId, commentUrl, err - return "", "", nil -} - -func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, comments []ci.Comment, reportTitle string, supportsCollapsible bool) (string, string, error) { - report = reportFormatter(report) - commentIdForThisRun := "" - var commentBody string - var commentUrl string - for _, comment := range comments { - if strings.Contains(*comment.Body, reportTitle) { - commentIdForThisRun = comment.Id - commentBody = *comment.Body - commentUrl = comment.Url - break - } - } + return nil +} - if commentIdForThisRun == "" { - var commentMessage string - if !supportsCollapsible { - commentMessage = utils.AsComment(reportTitle)(report) - } else { - commentMessage = utils.AsCollapsibleComment(reportTitle, false)(report) +func (strategy SingleCommentStrategy) Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) ([]string, []string, error) { + var completeComment = "" + for projectName, projectFormatters := range strategy.formatters { + projectTitle := "report for " + projectName + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") + var projectComment = "" + for _, f := range projectFormatters { + report := f.ReportFormatter(f.Report) + projectComment = projectComment + "\n" + report } - comment, err := ciService.PublishComment(PrNumber, commentMessage) - if err != nil { - return "", "", fmt.Errorf("error publishing comment: %v", err) + if !supportsCollapsibleComment { + projectComment = utils.AsComment(projectTitle)(projectComment) + } else { + projectComment = utils.AsCollapsibleComment(projectTitle, false)(projectComment) } - return fmt.Sprintf("%v", comment.Id), comment.Url, nil - } - - // strip first and last lines - lines := strings.Split(commentBody, "\n") - lines = lines[1 : len(lines)-1] - commentBody = strings.Join(lines, "\n") - - commentBody = commentBody + "\n\n" + report + "\n" - - var completeComment string - if !supportsCollapsible { - completeComment = utils.AsComment(reportTitle)(commentBody) - } else { - completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody) + completeComment = completeComment + "\n" + projectComment } - err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment) - + c, err := ciService.PublishComment(PrNumber, completeComment) if err != nil { - return "", "", fmt.Errorf("error editing comment: %v", err) + log.Printf("error while publishing reporter comment: %v", err) + return nil, nil, fmt.Errorf("error while publishing reporter comment: %v", err) } - return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil + return []string{c.Id}, []string{c.Url}, nil +} + +func (strategy SingleCommentStrategy) Suppress(projectName string) error { + // TODO: figure out how to suppress a particular project (probably pop it from the formatters map?) + return nil } type MultipleCommentsStrategy struct { - Formatters map[string]ReportFormat + formatters map[string][]ReportFormat + TimeOfRun time.Time +} + +func NewMultipleCommentsStrategy() MultipleCommentsStrategy { + return MultipleCommentsStrategy{ + TimeOfRun: time.Now(), + formatters: make(map[string][]ReportFormat), + } } func (strategy MultipleCommentsStrategy) Report(projectName string, report string, reportFormatter func(report string) string) error { - strategy.Formatters[projectName] = ReportFormat{ - ReportTitle: report, - ReportFormatter: reportFormatter, + if _, exists := strategy.formatters[projectName]; !exists { + strategy.formatters[projectName] = []ReportFormat{} } + strategy.formatters[projectName] = append(strategy.formatters[projectName], ReportFormat{ + Report: report, + ReportFormatter: reportFormatter, + }) + return nil } -func (strategy MultipleCommentsStrategy) Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (string, string, error) { - return "", "", nil +func (strategy MultipleCommentsStrategy) Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) ([]string, []string, error) { + hasError := false + var latestError error = nil + commentIds := make([]string, 0) + commentUrls := make([]string, 0) + for projectName, projectFormatters := range strategy.formatters { + projectTitle := "Digger run report for " + projectName + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") + var projectComment = "" + for _, f := range projectFormatters { + report := f.ReportFormatter(f.Report) + projectComment = projectComment + "\n" + report + } + if !supportsCollapsibleComment { + projectComment = utils.AsComment(projectTitle)(projectComment) + } else { + projectComment = utils.AsCollapsibleComment(projectTitle, false)(projectComment) + } + c, err := ciService.PublishComment(PrNumber, projectComment) + if err != nil { + log.Printf("error while publishing reporter comment: %v", err) + hasError = true + latestError = err + // append placeholders + commentIds = append(commentIds, "0") + commentUrls = append(commentUrls, "") + + } else { + commentIds = append(commentIds, c.Id) + commentUrls = append(commentUrls, c.Url) + } + } + + if hasError { + log.Printf("could not publish all comments") + return commentIds, commentUrls, latestError + } + + return commentIds, commentUrls, nil +} + +func (strategy MultipleCommentsStrategy) Suppress(projectName string) error { + // TODO: figure out how to suppress a particular project (probably pop it from the formatters map?) + return nil } diff --git a/libs/comment_utils/reporting/utils.go b/libs/comment_utils/reporting/utils.go index c99118090..63a31c355 100644 --- a/libs/comment_utils/reporting/utils.go +++ b/libs/comment_utils/reporting/utils.go @@ -5,7 +5,6 @@ import ( "github.com/diggerhq/digger/libs/ci" dg_configuration "github.com/diggerhq/digger/libs/digger_config" "log" - "time" ) type SourceDetails struct { @@ -28,21 +27,22 @@ func PostInitialSourceComments(ghService ci.PullRequestService, prNumber int, im reporter := CiReporter{ PrNumber: prNumber, CiService: ghService, - ReportStrategy: SingleCommentStrategy{fmt.Sprintf("Report for location: %v", location), time.Now()}, + ReportStrategy: NewSingleCommentStrategy(), } - err := reporter.Report("Comment Reporter", func(report string) string { return "" }) + + err := reporter.Report(location, "Comment Reporter", func(report string) string { return "" }) if err != nil { log.Printf("Error reporting source module comment: %v", err) return nil, fmt.Errorf("error reporting source module comment: %v", err) } - commentId, _, err := reporter.Flush() + commentIds, _, err := reporter.Flush() if err != nil { log.Printf("Error reporting source module comment: %v", err) return nil, fmt.Errorf("error reporting source module comment: %v", err) } - sourceDetails = append(sourceDetails, SourceDetails{location, commentId, projects}) + sourceDetails = append(sourceDetails, SourceDetails{location, commentIds[0], projects}) } return sourceDetails, nil diff --git a/libs/execution/execution.go b/libs/execution/execution.go index 3233e3065..75444f8b2 100644 --- a/libs/execution/execution.go +++ b/libs/execution/execution.go @@ -223,7 +223,7 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin if step.Action == "init" { _, stderr, err := d.TerraformExecutor.Init(step.ExtraArgs, d.StateEnvVars) if err != nil { - reportError(d.Reporter, stderr) + reportError(d.ProjectName, d.Reporter, stderr) return nil, false, false, "", "", fmt.Errorf("error running init: %v", err) } } @@ -288,18 +288,18 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin } } } - reportAdditionalOutput(d.Reporter, d.projectId()) + reportAdditionalOutput(d.Reporter, d.ProjectName) return planSummary, true, !isEmptyPlan, plan, terraformPlanOutput, nil } -func reportError(r reporting.Reporter, stderr string) { +func reportError(projectName string, r reporting.Reporter, stderr string) { if r.SupportsMarkdown() { - commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.", false)) + commentErr := r.Report(projectName, stderr, utils.AsCollapsibleComment("Error during init.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } } else { - commentErr := r.Report(stderr, utils.AsComment("Error during init.")) + commentErr := r.Report(projectName, stderr, utils.AsComment("Error during init.")) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } @@ -337,7 +337,7 @@ func (d DiggerExecutor) Apply() (*iac_utils.IacSummary, bool, string, error) { if step.Action == "init" { stdout, stderr, err := d.TerraformExecutor.Init(step.ExtraArgs, d.StateEnvVars) if err != nil { - reportTerraformError(d.Reporter, stderr) + reportTerraformError(d.ProjectName, d.Reporter, stderr) return nil, false, stdout, fmt.Errorf("error running init: %v", err) } } @@ -346,9 +346,9 @@ func (d DiggerExecutor) Apply() (*iac_utils.IacSummary, bool, string, error) { stdout, stderr, err := d.TerraformExecutor.Apply(applyArgs, plansFilename, d.CommandEnvVars) applyOutput = cleanupTerraformApply(true, err, stdout, stderr) - reportTerraformApplyOutput(d.Reporter, d.projectId(), applyOutput) + reportTerraformApplyOutput(d.Reporter, d.ProjectName, applyOutput) if err != nil { - reportApplyError(d.Reporter, err) + reportApplyError(d.ProjectName, d.Reporter, err) return nil, false, stdout, fmt.Errorf("error executing apply: %v", err) } @@ -370,25 +370,25 @@ func (d DiggerExecutor) Apply() (*iac_utils.IacSummary, bool, string, error) { } } } - reportAdditionalOutput(d.Reporter, d.projectId()) + reportAdditionalOutput(d.Reporter, d.ProjectName) return &summary, true, applyOutput, nil } -func reportApplyError(r reporting.Reporter, err error) { +func reportApplyError(projectName string, r reporting.Reporter, err error) { if r.SupportsMarkdown() { - commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.", false)) + commentErr := r.Report(projectName, err.Error(), utils.AsCollapsibleComment("Error during applying.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", err) } } else { - commentErr := r.Report(err.Error(), utils.AsComment("Error during applying.")) + commentErr := r.Report(projectName, err.Error(), utils.AsComment("Error during applying.")) if commentErr != nil { log.Printf("error publishing comment: %v", err) } } } -func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOutput string) { +func reportTerraformApplyOutput(r reporting.Reporter, projectName string, applyOutput string) { var formatter func(string) string if r.SupportsMarkdown() { formatter = utils.GetTerraformOutputAsCollapsibleComment("Apply output", false) @@ -396,39 +396,39 @@ func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOut formatter = utils.GetTerraformOutputAsComment("Apply output") } - commentErr := r.Report(applyOutput, formatter) + commentErr := r.Report(projectName, applyOutput, formatter) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } } -func reportTerraformError(r reporting.Reporter, stderr string) { +func reportTerraformError(projectName string, r reporting.Reporter, stderr string) { if r.SupportsMarkdown() { - commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false)) + commentErr := r.Report(projectName, stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } } else { - commentErr := r.Report(stderr, utils.GetTerraformOutputAsComment("Error during init.")) + commentErr := r.Report(projectName, stderr, utils.GetTerraformOutputAsComment("Error during init.")) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } } } -func reportAdditionalOutput(r reporting.Reporter, projectId string) { +func reportAdditionalOutput(r reporting.Reporter, projectName string) { var formatter func(string) string if r.SupportsMarkdown() { - formatter = utils.GetTerraformOutputAsCollapsibleComment("Additional output for "+projectId+"", false) + formatter = utils.GetTerraformOutputAsCollapsibleComment("Additional output for "+projectName+"", false) } else { - formatter = utils.GetTerraformOutputAsComment("Additional output for " + projectId) + formatter = utils.GetTerraformOutputAsComment("Additional output for " + projectName) } diggerOutPath := os.Getenv("DIGGER_OUT") if _, err := os.Stat(diggerOutPath); err == nil { output, _ := os.ReadFile(diggerOutPath) outputStr := string(output) if len(outputStr) > 0 { - commentErr := r.Report(outputStr, formatter) + commentErr := r.Report(projectName, outputStr, formatter) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } @@ -459,7 +459,7 @@ func (d DiggerExecutor) Destroy() (bool, error) { if step.Action == "init" { _, stderr, err := d.TerraformExecutor.Init(step.ExtraArgs, d.StateEnvVars) if err != nil { - reportError(d.Reporter, stderr) + reportError(d.ProjectName, d.Reporter, stderr) return false, fmt.Errorf("error running init: %v", err) } } diff --git a/libs/locking/locking.go b/libs/locking/locking.go index 8ec28bf47..094b0fe04 100644 --- a/libs/locking/locking.go +++ b/libs/locking/locking.go @@ -74,7 +74,7 @@ func (projectLock *PullRequestLock) Lock() (bool, error) { transactionIdStr := strconv.Itoa(*existingLockTransactionId) comment := "Project " + projectLock.projectId() + " locked by another PR #" + transactionIdStr + " (failed to acquire lock " + projectLock.ProjectNamespace + "). The locking plan must be applied or discarded before future plans can execute" - reportLockingFailed(projectLock.Reporter, comment) + reportLockingFailed(projectLock.ProjectName, projectLock.Reporter, comment) return false, fmt.Errorf(comment) } } @@ -87,35 +87,35 @@ func (projectLock *PullRequestLock) Lock() (bool, error) { if lockAcquired && !isNoOpLock { comment := "Project " + projectLock.projectId() + " has been locked by PR #" + strconv.Itoa(projectLock.PrNumber) - reportingLockingSuccess(projectLock.Reporter, comment) + reportingLockingSuccess(projectLock.ProjectName, projectLock.Reporter, comment) log.Println("project " + projectLock.projectId() + " locked successfully. PR # " + strconv.Itoa(projectLock.PrNumber)) } return lockAcquired, nil } -func reportingLockingSuccess(r reporting.Reporter, comment string) { +func reportingLockingSuccess(projectName string, r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - err := r.Report(comment, utils.AsCollapsibleComment("Locking successful", false)) + err := r.Report(projectName, comment, utils.AsCollapsibleComment("Locking successful", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } } else { - err := r.Report(comment, utils.AsComment("Locking successful")) + err := r.Report(projectName, comment, utils.AsComment("Locking successful")) if err != nil { log.Println("failed to publish comment: " + err.Error()) } } } -func reportLockingFailed(r reporting.Reporter, comment string) { +func reportLockingFailed(projectName string, r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - err := r.Report(comment, utils.AsCollapsibleComment("Locking failed", false)) + err := r.Report(projectName, comment, utils.AsCollapsibleComment("Locking failed", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } } else { - err := r.Report(comment, utils.AsComment("Locking failed")) + err := r.Report(projectName, comment, utils.AsComment("Locking failed")) if err != nil { log.Println("failed to publish comment: " + err.Error()) } @@ -146,7 +146,7 @@ func (projectLock *PullRequestLock) verifyNoHangingLocks() (bool, error) { } transactionIdStr := strconv.Itoa(*transactionId) comment := "Project " + projectLock.projectId() + " locked by another PR #" + transactionIdStr + "(failed to acquire lock " + projectLock.ProjectName + "). The locking plan must be applied or discarded before future plans can execute" - reportLockingFailed(projectLock.Reporter, comment) + reportLockingFailed(projectLock.ProjectName, projectLock.Reporter, comment) return false, fmt.Errorf(comment) } return true, nil @@ -171,7 +171,7 @@ func (projectLock *PullRequestLock) Unlock() (bool, error) { } if lockReleased { comment := "Project unlocked (" + projectLock.projectId() + ")." - reportSuccessfulUnlocking(projectLock.Reporter, comment) + reportSuccessfulUnlocking(projectLock.ProjectName, projectLock.Reporter, comment) log.Println("Project unlocked") return true, nil @@ -181,14 +181,14 @@ func (projectLock *PullRequestLock) Unlock() (bool, error) { return false, nil } -func reportSuccessfulUnlocking(r reporting.Reporter, comment string) { +func reportSuccessfulUnlocking(projectName string, r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful", false)) + err := r.Report(projectName, comment, utils.AsCollapsibleComment("Unlocking successful", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } } else { - err := r.Report(comment, utils.AsComment("Unlocking successful")) + err := r.Report(projectName, comment, utils.AsComment("Unlocking successful")) if err != nil { log.Println("failed to publish comment: " + err.Error()) } @@ -210,7 +210,7 @@ func (projectLock *PullRequestLock) ForceUnlock() error { if lockReleased { comment := "Project unlocked (" + projectLock.projectId() + ")." - reportSuccessfulUnlocking(projectLock.Reporter, comment) + reportSuccessfulUnlocking(projectLock.ProjectName, projectLock.Reporter, comment) log.Println("Project unlocked") } return nil diff --git a/libs/spec/providers.go b/libs/spec/providers.go index 75eaa9ff4..236eb87f2 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -27,7 +27,6 @@ import ( "net/http" "os" "strings" - "time" ) type JobSpecProvider struct{} @@ -117,12 +116,9 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c getStrategy := func(strategy string) reporting.ReportStrategy { switch reporterSpec.ReportingStrategy { case "comments_per_run": - return reporting.SingleCommentStrategy{ - Title: title, - TimeOfRun: time.Now(), - } + return reporting.NewSingleCommentStrategy() default: - return reporting.MultipleCommentsStrategy{} + return reporting.NewMultipleCommentsStrategy() } } From 6df110bd0189f93d930deafaeb58b6e85d826eca Mon Sep 17 00:00:00 2001 From: motatoes Date: Sat, 21 Dec 2024 08:13:49 +0000 Subject: [PATCH 08/13] fix tests --- cli/pkg/digger/digger_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cli/pkg/digger/digger_test.go b/cli/pkg/digger/digger_test.go index 7a52d24e5..0361a3112 100644 --- a/cli/pkg/digger/digger_test.go +++ b/cli/pkg/digger/digger_test.go @@ -86,7 +86,12 @@ func (m *MockPRManager) GetApprovals(prNumber int) ([]string, error) { func (m *MockPRManager) PublishComment(prNumber int, comment string) (*ci.Comment, error) { m.Commands = append(m.Commands, RunInfo{"PublishComment", strconv.Itoa(prNumber) + " " + comment, time.Now()}) - return nil, nil + return &ci.Comment{ + Id: "", + DiscussionId: "", + Body: nil, + Url: "", + }, nil } func (m *MockPRManager) ListIssues() ([]*ci.Issue, error) { @@ -241,7 +246,6 @@ func (m MockPlanPathProvider) LocalPlanFilePath() string { } func TestCorrectCommandExecutionWhenApplying(t *testing.T) { - commandRunner := &MockCommandRunner{} terraformExecutor := &MockTerraformExecutor{} prManager := &MockPRManager{} @@ -288,7 +292,7 @@ func TestCorrectCommandExecutionWhenApplying(t *testing.T) { commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage, planPathProvider) - assert.Equal(t, []string{"RetrievePlan plan", "Init ", "Apply ", "PublishComment 1
Apply output\n\n```terraform\n\n```\n
", "Run echo"}, commandStrings) + assert.Equal(t, []string{"RetrievePlan plan", "Init ", "Apply ", "Run echo"}, commandStrings) } func TestCorrectCommandExecutionWhenDestroying(t *testing.T) { From 44c1aff697ac56ac447ff5f47d1b4e5cb0704e2b Mon Sep 17 00:00:00 2001 From: motatoes Date: Sat, 21 Dec 2024 08:23:42 +0000 Subject: [PATCH 09/13] switch to lazy reporter --- backend/controllers/github.go | 1 + backend/services/spec.go | 5 ++--- libs/spec/providers.go | 9 --------- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/backend/controllers/github.go b/backend/controllers/github.go index ad4250138..6ec12a584 100644 --- a/backend/controllers/github.go +++ b/backend/controllers/github.go @@ -419,6 +419,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR return fmt.Errorf("error processing event") } } + diggerCommand, err := orchestrator_scheduler.GetCommandFromJob(jobsForImpactedProjects[0]) if err != nil { log.Printf("could not determine digger command from job: %v", jobsForImpactedProjects[0].Commands) diff --git a/backend/services/spec.go b/backend/services/spec.go index 8152f6f42..1469d74b6 100644 --- a/backend/services/spec.go +++ b/backend/services/spec.go @@ -116,9 +116,8 @@ func GetSpecFromJob(job models.DiggerJob) (*spec.Spec, error) { CommentId: strconv.FormatInt(*batch.CommentId, 10), Job: jobSpec, Reporter: spec.ReporterSpec{ - //ReportingStrategy: "comments_per_run", - ReportingStrategy: "always_same_comment", - ReporterType: "lazy", + ReportingStrategy: "comments_per_run", + ReporterType: "basic", ReportCommentId: job.Batch.PlaceholderCommentIdForReport, }, Lock: spec.LockSpec{ diff --git a/libs/spec/providers.go b/libs/spec/providers.go index 236eb87f2..fd8f807e7 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -133,15 +133,6 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c IsSupportMarkdown: true, ReportStrategy: strategy, }, nil - case "lazy": - strategy := getStrategy(reporterSpec.ReportingStrategy) - ciReporter := reporting.CiReporter{ - CiService: ciService, - PrNumber: prNumber, - IsSupportMarkdown: true, - ReportStrategy: strategy, - } - return ciReporter, nil default: return reporting.NoopReporter{}, nil } From f55299612ab294f38e85004a6e6c196d858dda85 Mon Sep 17 00:00:00 2001 From: motatoes Date: Sat, 21 Dec 2024 15:24:25 +0000 Subject: [PATCH 10/13] update opentofu setup --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index b9477a735..54cf389d8 100644 --- a/action.yml +++ b/action.yml @@ -275,7 +275,7 @@ runs: if: inputs.setup-terragrunt == 'true' - name: Setup OpenTofu - uses: opentofu/setup-opentofu@v1.0.3 + uses: opentofu/setup-opentofu@v1 with: tofu_version: ${{ inputs.opentofu-version }} tofu_wrapper: false From b4068cd65f1b376ca31a35d5b607124efd4eebbd Mon Sep 17 00:00:00 2001 From: motatoes Date: Mon, 23 Dec 2024 12:58:21 +0000 Subject: [PATCH 11/13] Revert "update opentofu setup" This reverts commit f55299612ab294f38e85004a6e6c196d858dda85. --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 54cf389d8..b9477a735 100644 --- a/action.yml +++ b/action.yml @@ -275,7 +275,7 @@ runs: if: inputs.setup-terragrunt == 'true' - name: Setup OpenTofu - uses: opentofu/setup-opentofu@v1 + uses: opentofu/setup-opentofu@v1.0.3 with: tofu_version: ${{ inputs.opentofu-version }} tofu_wrapper: false From 1a48f98cd5d71c25f817e9f61f4d10ca75209e5b Mon Sep 17 00:00:00 2001 From: motatoes Date: Mon, 23 Dec 2024 12:58:56 +0000 Subject: [PATCH 12/13] update the action --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index b9477a735..0a71a9418 100644 --- a/action.yml +++ b/action.yml @@ -275,7 +275,7 @@ runs: if: inputs.setup-terragrunt == 'true' - name: Setup OpenTofu - uses: opentofu/setup-opentofu@v1.0.3 + uses: opentofu/setup-opentofu@v1.0.5 with: tofu_version: ${{ inputs.opentofu-version }} tofu_wrapper: false From 4c9fb5c2e277979fffb5541cd5c608e41a3a1812 Mon Sep 17 00:00:00 2001 From: motatoes Date: Fri, 3 Jan 2025 08:23:18 +0000 Subject: [PATCH 13/13] reorder migrations --- backend/migrations/{20241204194016.sql => 20250102194016.sql} | 0 backend/migrations/atlas.sum | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) rename backend/migrations/{20241204194016.sql => 20250102194016.sql} (100%) diff --git a/backend/migrations/20241204194016.sql b/backend/migrations/20250102194016.sql similarity index 100% rename from backend/migrations/20241204194016.sql rename to backend/migrations/20250102194016.sql diff --git a/backend/migrations/atlas.sum b/backend/migrations/atlas.sum index 4b13a3e4e..a1afdb4cf 100644 --- a/backend/migrations/atlas.sum +++ b/backend/migrations/atlas.sum @@ -1,4 +1,4 @@ -h1:kHWmqYJMe9ZbdcztvM02SVEs5lyw/RFbKzZmqgbmSIk= +h1:DtynCDyaa2VQhu0lgQmvTIZXoG31raLDJNBIyEWKRwg= 20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw= 20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA= 20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw= @@ -35,3 +35,4 @@ h1:kHWmqYJMe9ZbdcztvM02SVEs5lyw/RFbKzZmqgbmSIk= 20241107172343.sql h1:E1j+7R5TZlyCKEpyYmH1mJ2zh+y5hVbtQ/PuEMJR7us= 20241114202249.sql h1:P2DhJK8MLe8gSAAz+Y5KNmsvKVw8KfLQPCncynYXEfM= 20241229112312.sql h1:Fr06uwt7LcQoLh6bjGzKB+uy9i8+uk8m6jfi+OBBbP4= +20250102194016.sql h1:Q6nnbPO/zA0PTdZXowv51sL0aUga1b8dA0yfVHgmAZs=