Skip to content

Commit e938730

Browse files
authored
Merge pull request #1351 from hongweiliu17/STONEINTG-1390
fix(STONEINTG-1390): update existing GH/GL comment not creating new one
2 parents af6a869 + 3a71df4 commit e938730

File tree

9 files changed

+37
-24
lines changed

9 files changed

+37
-24
lines changed

git/github/github.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type ClientInterface interface {
110110
GetAllCommitStatusesForRef(ctx context.Context, owner, repo, sha string) ([]*ghapi.RepoStatus, int, error)
111111
GetAllCommentsForPR(ctx context.Context, owner string, repo string, pr int) ([]*ghapi.IssueComment, int, error)
112112
CommitStatusExists(res []*ghapi.RepoStatus, commitStatus *CommitStatusAdapter) (bool, error)
113-
GetExistingCommentID(comments []*ghapi.IssueComment, snapshotName, scenarioName string) *int64
113+
GetExistingCommentID(comments []*ghapi.IssueComment, componentName, scenarioName string) *int64
114114
EditComment(ctx context.Context, owner string, repo string, commentID int64, body string) (int64, int, error)
115115
GetPullRequest(ctx context.Context, owner string, repo string, prID int) (*ghapi.PullRequest, int, error)
116116
}
@@ -428,9 +428,9 @@ func (c *Client) GetExistingCheckRun(checkRuns []*ghapi.CheckRun, newCheckRun *C
428428
}
429429

430430
// GetExistingComment returns existing GitHub comment for the scenario of ref.
431-
func (c *Client) GetExistingCommentID(comments []*ghapi.IssueComment, snapshotName, scenarioName string) *int64 {
431+
func (c *Client) GetExistingCommentID(comments []*ghapi.IssueComment, componentName, scenarioName string) *int64 {
432432
for _, comment := range comments {
433-
if strings.Contains(*comment.Body, snapshotName) && strings.Contains(*comment.Body, scenarioName) {
433+
if strings.Contains(*comment.Body, componentName) && strings.Contains(*comment.Body, scenarioName) {
434434
c.logger.Info("found comment ID with a matching scenarioName", "scenarioName", scenarioName)
435435
return comment.ID
436436
}

git/github/github_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (MockIssuesService) CreateComment(
114114
func (MockIssuesService) ListComments(ctx context.Context, owner string, repo string,
115115
number int, opts *ghapi.IssueListCommentsOptions) ([]*ghapi.IssueComment, *ghapi.Response, error) {
116116
var id int64 = 40
117-
var body = "Integration test for snapshot snapshotName and scenario scenarioName"
117+
var body = "Integration test for component component-sample snapshot snapshotName and scenario scenarioName"
118118
issueComments := []*ghapi.IssueComment{{ID: &id, Body: &body}}
119119
return issueComments, nil, nil
120120
}
@@ -355,7 +355,7 @@ var _ = Describe("Client", func() {
355355
Expect(comments).NotTo(BeEmpty())
356356
Expect(statusCode).NotTo(BeNil())
357357

358-
commentID := client.GetExistingCommentID(comments, "snapshotName", "scenarioName")
358+
commentID := client.GetExistingCommentID(comments, "component-sample", "scenarioName")
359359
Expect(*commentID).To(Equal(int64(40)))
360360
})
361361

internal/controller/statusreport/statusreport_adapter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
931931
SnapshotName: "snapshot-pr-sample",
932932
ComponentName: "component-sample",
933933
Text: "Test in progress",
934-
Summary: "Integration test for snapshot snapshot-pr-sample and scenario scenario1 is in progress",
934+
Summary: "Integration test for component component-sample snapshot snapshot-pr-sample and scenario scenario1 is in progress",
935935
Status: intgteststat.IntegrationTestStatusInProgress,
936936
StartTime: &t,
937937
TestPipelineRunName: "test-pipelinerun",

internal/controller/statusreport/statusreport_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
9696

9797
// AdapterInterface is an interface defining all the operations that should be defined in an Integration adapter.
9898
type AdapterInterface interface {
99-
EnsureSnapshotTestStatusReportedToGitHub() (controller.OperationResult, error)
99+
EnsureSnapshotTestStatusReportedToGitProvider() (controller.OperationResult, error)
100100
EnsureGroupSnapshotCreationStatusReportedToGitProvider(controller.OperationResult, error)
101101
EnsureSnapshotFinishedAllTests() (controller.OperationResult, error)
102102
}

status/reporter_github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func (csu *CommitStatusUpdater) updateStatusInComment(ctx context.Context, repor
417417
csu.logger.Error(err, fmt.Sprintf("error while getting all comments for pull-request %s", issueNumberStr))
418418
return statusCode, fmt.Errorf("error while getting all comments for pull-request %s: %w", issueNumberStr, err)
419419
}
420-
existingCommentId := csu.ghClient.GetExistingCommentID(allComments, csu.snapshot.Name, report.ScenarioName)
420+
existingCommentId := csu.ghClient.GetExistingCommentID(allComments, GenerateComponentNameWithPrefix(report.ComponentName), report.ScenarioName)
421421
if existingCommentId == nil {
422422
_, statusCode, err = csu.ghClient.CreateComment(ctx, csu.owner, csu.repo, issueNumber, comment)
423423
if err != nil {

status/reporter_github_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (c *MockGitHubClient) GetAllCommentsForPR(ctx context.Context, owner string
153153
return comments, 200, nil
154154
}
155155

156-
func (c *MockGitHubClient) GetExistingCommentID(comments []*ghapi.IssueComment, snapshotName, scenarioName string) *int64 {
156+
func (c *MockGitHubClient) GetExistingCommentID(comments []*ghapi.IssueComment, componentName, scenarioName string) *int64 {
157157
return nil
158158
}
159159

status/reporter_gitlab.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (r *GitLabReporter) updateStatusInComment(report TestReport) (int, error) {
256256
r.logger.Error(err, "error while getting all comments for merge-request", "mergeRequest", r.mergeRequest, "report.SnapshotName", report.SnapshotName)
257257
return statusCode, fmt.Errorf("error while getting all comments for merge-request %d: %w", r.mergeRequest, err)
258258
}
259-
existingCommentId := r.GetExistingNoteID(allNotes, report.ScenarioName, report.SnapshotName)
259+
existingCommentId := r.GetExistingNoteID(allNotes, report.ScenarioName, GenerateComponentNameWithPrefix(report.ComponentName))
260260
if existingCommentId == nil {
261261
noteOptions := gitlab.CreateMergeRequestNoteOptions{Body: &comment}
262262
_, response, err := r.client.Notes.CreateMergeRequestNote(r.targetProjectID, r.mergeRequest, &noteOptions)
@@ -294,10 +294,10 @@ func (r *GitLabReporter) GetExistingCommitStatus(commitStatuses []*gitlab.Commit
294294
}
295295

296296
// GetExistingNoteID returns existing GitLab note for the scenario of ref.
297-
func (r *GitLabReporter) GetExistingNoteID(notes []*gitlab.Note, scenarioName, snapshotName string) *int {
297+
func (r *GitLabReporter) GetExistingNoteID(notes []*gitlab.Note, scenarioName, componentName string) *int {
298298
for _, note := range notes {
299-
if strings.Contains(note.Body, snapshotName) && strings.Contains(note.Body, scenarioName) {
300-
r.logger.Info("found note ID with a matching scenarioName", "scenarioName", scenarioName, "noteID", &note.ID)
299+
if strings.Contains(note.Body, componentName) && strings.Contains(note.Body, scenarioName) {
300+
r.logger.Info("found note ID with a matching componentName and scenarioName", "omponentName", componentName, "scenarioName", scenarioName, "noteID", &note.ID)
301301
return &note.ID
302302
}
303303
}

status/status.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func GenerateTestReport(ctx context.Context, client client.Client, detail intgte
239239
return nil, fmt.Errorf("failed to generate text message: %w", err)
240240
}
241241

242-
summary, err := GenerateSummary(detail.Status, testedSnapshot.Name, detail.ScenarioName)
242+
summary, err := GenerateSummary(detail.Status, testedSnapshot.Name, detail.ScenarioName, componentName)
243243
if err != nil {
244244
return nil, fmt.Errorf("failed to generate summary message: %w", err)
245245
}
@@ -315,7 +315,7 @@ func generateText(ctx context.Context, client client.Client, integrationTestStat
315315
}
316316

317317
// GenerateSummary returns summary for the given state, snapshotName and scenarioName
318-
func GenerateSummary(state intgteststat.IntegrationTestStatus, snapshotName, scenarioName string) (string, error) {
318+
func GenerateSummary(state intgteststat.IntegrationTestStatus, snapshotName, scenarioName, componentName string) (string, error) {
319319
var summary string
320320
var statusDesc string
321321

@@ -348,15 +348,28 @@ func GenerateSummary(state intgteststat.IntegrationTestStatus, snapshotName, sce
348348
return summary, fmt.Errorf("unknown status")
349349
}
350350

351+
componentName = GenerateComponentNameWithPrefix(componentName)
352+
351353
if state == intgteststat.BuildPLRInProgress || state == intgteststat.SnapshotCreationFailed || state == intgteststat.BuildPLRFailed || state == intgteststat.GroupSnapshotCreationFailed {
352-
summary = fmt.Sprintf("Integration test for scenario %s %s", scenarioName, statusDesc)
354+
summary = fmt.Sprintf("Integration test for %s snapshot scenario %s %s", componentName, scenarioName, statusDesc)
353355
} else {
354-
summary = fmt.Sprintf("Integration test for snapshot %s and scenario %s %s", snapshotName, scenarioName, statusDesc)
356+
summary = fmt.Sprintf("Integration test for %s snapshot %s and scenario %s %s", componentName, snapshotName, scenarioName, statusDesc)
355357
}
356358

357359
return summary, nil
358360
}
359361

362+
// function GenerateComponentNameWithPrefix to generate component name "pr group" or "component component-sample"
363+
// to help search it in comment correctly to avoid component name is searched in pr group report
364+
func GenerateComponentNameWithPrefix(componentName string) string {
365+
if componentName == gitops.ComponentNameForGroupSnapshot {
366+
componentName = gitops.ComponentNameForGroupSnapshot
367+
} else {
368+
componentName = "component " + componentName
369+
}
370+
return componentName
371+
}
372+
360373
func getConsoleName() string {
361374
consoleName := os.Getenv("CONSOLE_NAME")
362375
if consoleName == "" {

status/status_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,19 +522,19 @@ var _ = Describe("Status Adapter", func() {
522522
523523
`
524524
expectedTestReport := status.TestReport{
525-
FullName: "Red Hat Konflux / scenario1",
525+
FullName: "Red Hat Konflux / scenario1 / component-sample",
526526
ScenarioName: "scenario1",
527527
SnapshotName: "snapshot-sample",
528-
ComponentName: "",
528+
ComponentName: "component-sample",
529529
Text: text,
530-
Summary: "Integration test for snapshot snapshot-sample and scenario scenario1 has passed",
530+
Summary: "Integration test for component component-sample snapshot snapshot-sample and scenario scenario1 has passed",
531531
Status: integrationteststatus.IntegrationTestStatusTestPassed,
532532
StartTime: &ts,
533533
CompletionTime: &tc,
534534
TestPipelineRunName: "test-pipelinerun",
535535
}
536536

537-
testReport, err := status.GenerateTestReport(context.Background(), mockK8sClient, integrationTestStatusDetail, hasSnapshot, "")
537+
testReport, err := status.GenerateTestReport(context.Background(), mockK8sClient, integrationTestStatusDetail, hasSnapshot, "component-sample")
538538
Expect(err).NotTo(HaveOccurred())
539539
Expect(testReport).To(Equal(&expectedTestReport))
540540
})
@@ -545,7 +545,7 @@ var _ = Describe("Status Adapter", func() {
545545

546546
integrationTestStatusDetail := newIntegrationTestStatusDetail(expectedScenarioStatus)
547547

548-
expectedSummary := fmt.Sprintf("Integration test for snapshot snapshot-sample and scenario scenario1 %s", expectedTextEnding)
548+
expectedSummary := fmt.Sprintf("Integration test for component component-sample snapshot snapshot-sample and scenario scenario1 %s", expectedTextEnding)
549549
testReport, err := status.GenerateTestReport(context.Background(), mockK8sClient, integrationTestStatusDetail, hasSnapshot, "component-sample")
550550
Expect(err).NotTo(HaveOccurred())
551551
Expect(testReport.Summary).To(Equal(expectedSummary))
@@ -566,7 +566,7 @@ var _ = Describe("Status Adapter", func() {
566566

567567
integrationTestStatusDetail := newIntegrationTestStatusDetail(expectedScenarioStatus)
568568

569-
expectedSummary := fmt.Sprintf("Integration test for scenario scenario1 %s", expectedTextEnding)
569+
expectedSummary := fmt.Sprintf("Integration test for component component-sample snapshot scenario scenario1 %s", expectedTextEnding)
570570
testReport, err := status.GenerateTestReport(context.Background(), mockK8sClient, integrationTestStatusDetail, hasSnapshot, "component-sample")
571571
Expect(err).NotTo(HaveOccurred())
572572
Expect(testReport.Summary).To(Equal(expectedSummary))
@@ -578,7 +578,7 @@ var _ = Describe("Status Adapter", func() {
578578

579579
It("check if GenerateSummary supports all integration test statuses", func() {
580580
for _, teststatus := range integrationteststatus.IntegrationTestStatusValues() {
581-
_, err := status.GenerateSummary(teststatus, "yolo", "yolo")
581+
_, err := status.GenerateSummary(teststatus, "yolo", "yolo", "yoyo")
582582
Expect(err).NotTo(HaveOccurred())
583583
}
584584
})

0 commit comments

Comments
 (0)