diff --git a/status/format.go b/status/format.go index 56dbb3ab2..bf84ef60f 100644 --- a/status/format.go +++ b/status/format.go @@ -48,7 +48,7 @@ const summaryTemplate = ` {{ formatFootnotes .TaskRuns }} {{ if .ComponentSnapshotInfos}} -The group snapshot is generated for the component snasphots as below: +The group snapshot is generated for pr group {{ .PRGroup }} and the component snasphots as below: | Component | Snapshot | BuildPipelineRun | PullRequest | | --- | --- | --- | --- | {{- range $cs := .ComponentSnapshotInfos }} @@ -61,6 +61,7 @@ type SummaryTemplateData struct { TaskRuns []*helpers.TaskRun PipelineRunName string Namespace string + PRGroup string ComponentSnapshotInfos []*gitops.ComponentSnapshotInfo Logger logr.Logger } @@ -79,7 +80,7 @@ type CommentTemplateData struct { } // FormatTestsSummary builds a markdown summary for a list of integration TaskRuns. -func FormatTestsSummary(taskRuns []*helpers.TaskRun, pipelineRunName string, namespace string, componentSnapshotInfos []*gitops.ComponentSnapshotInfo, logger logr.Logger) (string, error) { +func FormatTestsSummary(taskRuns []*helpers.TaskRun, pipelineRunName string, namespace string, componentSnapshotInfos []*gitops.ComponentSnapshotInfo, pr_group string, logger logr.Logger) (string, error) { funcMap := template.FuncMap{ "formatTaskName": FormatTaskName, "formatNamespace": FormatNamespace, @@ -92,7 +93,7 @@ func FormatTestsSummary(taskRuns []*helpers.TaskRun, pipelineRunName string, nam "formatRepoURL": FormatRepoURL, } buf := bytes.Buffer{} - data := SummaryTemplateData{TaskRuns: taskRuns, PipelineRunName: pipelineRunName, Namespace: namespace, ComponentSnapshotInfos: componentSnapshotInfos, Logger: logger} + data := SummaryTemplateData{TaskRuns: taskRuns, PipelineRunName: pipelineRunName, Namespace: namespace, PRGroup: pr_group, ComponentSnapshotInfos: componentSnapshotInfos, Logger: logger} t := template.Must(template.New("").Funcs(funcMap).Parse(summaryTemplate)) if err := t.Execute(&buf, data); err != nil { return "", err diff --git a/status/format_test.go b/status/format_test.go index 6490bd7c0..c2d926faa 100644 --- a/status/format_test.go +++ b/status/format_test.go @@ -54,6 +54,7 @@ const expectedTaskLogURL = `https://definetly.not.prod/preview/application-pipel var message = "Taskrun Succeeded, lucky you!" var componentSnapshotInfos = []*gitops.ComponentSnapshotInfo{} +var PRGroup string func newTaskRun(name string, startTime time.Time, completionTime time.Time) *helpers.TaskRun { return helpers.NewTaskRunFromTektonTaskRun(name, &tektonv1.TaskRunStatus{ @@ -236,7 +237,7 @@ var _ = Describe("Formatters", func() { It("CONSOLE_URL env var not set", func() { os.Setenv("CONSOLE_URL", "") - text, err := status.FormatTestsSummary(taskRuns, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, logr.Discard()) + text, err := status.FormatTestsSummary(taskRuns, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, PRGroup, logr.Discard()) Expect(err).To(Succeed()) Expect(text).To(ContainSubstring("https://CONSOLE_URL_NOT_AVAILABLE")) }) @@ -248,7 +249,7 @@ var _ = Describe("Formatters", func() { }) It("can construct a comment", func() { - text, err := status.FormatTestsSummary(taskRuns, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, logr.Discard()) + text, err := status.FormatTestsSummary(taskRuns, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, PRGroup, logr.Discard()) Expect(err).To(Succeed()) comment, err := status.FormatComment("example-title", text) Expect(err).To(BeNil()) @@ -262,7 +263,7 @@ var _ = Describe("Formatters", func() { }) It("can construct a summary", func() { - summary, err := status.FormatTestsSummary(taskRuns, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, logr.Discard()) + summary, err := status.FormatTestsSummary(taskRuns, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, PRGroup, logr.Discard()) Expect(err).To(BeNil()) Expect(summary).To(Equal(expectedSummary)) }) @@ -290,7 +291,7 @@ var _ = Describe("Formatters", func() { }) It("won't fail when summary is generated from invalid result", func() { - _, err := status.FormatTestsSummary([]*helpers.TaskRun{taskRun}, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, logr.Discard()) + _, err := status.FormatTestsSummary([]*helpers.TaskRun{taskRun}, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, PRGroup, logr.Discard()) Expect(err).To(Succeed()) }) }) @@ -312,7 +313,7 @@ var _ = Describe("Formatters", func() { }) It("won't fail when summary is generated from taskrun without TEST_OUTPUT", func() { - _, err := status.FormatTestsSummary([]*helpers.TaskRun{taskRun}, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, logr.Discard()) + _, err := status.FormatTestsSummary([]*helpers.TaskRun{taskRun}, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, PRGroup, logr.Discard()) Expect(err).To(Succeed()) }) }) @@ -354,8 +355,10 @@ var _ = Describe("Formatters", func() { PullRequestNumber: "1", }, } + PRGroup = "feature-1" It("component snapshot info is generated", func() { - text, err := status.FormatTestsSummary([]*helpers.TaskRun{taskRun}, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, logr.Discard()) + text, err := status.FormatTestsSummary([]*helpers.TaskRun{taskRun}, pipelineRun.Name, pipelineRun.Namespace, componentSnapshotInfos, PRGroup, logr.Discard()) + Expect(text).To(ContainSubstring("The group snapshot is generated for pr group feature-1 and the component snasphots as below:")) Expect(text).To(ContainSubstring("| com3 | snapshot3 | buildPLR3 | example |")) Expect(err).To(Succeed()) }) diff --git a/status/status.go b/status/status.go index eb78c64ed..2951af743 100644 --- a/status/status.go +++ b/status/status.go @@ -229,16 +229,9 @@ func (s *Status) GetReporter(snapshot *applicationapiv1alpha1.Snapshot) Reporter // GenerateTestReport generates TestReport to be used by all reporters func GenerateTestReport(ctx context.Context, client client.Client, detail intgteststat.IntegrationTestStatusDetail, testedSnapshot *applicationapiv1alpha1.Snapshot, componentName string) (*TestReport, error) { - var componentSnapshotInfos []*gitops.ComponentSnapshotInfo var err error - if componentSnapshotInfoString, ok := testedSnapshot.Annotations[gitops.GroupSnapshotInfoAnnotation]; ok { - componentSnapshotInfos, err = gitops.UnmarshalJSON([]byte(componentSnapshotInfoString)) - if err != nil { - return nil, fmt.Errorf("failed to unmarshal JSON string: %w", err) - } - } - text, err := generateText(ctx, client, detail, testedSnapshot.Namespace, componentSnapshotInfos) + text, err := generateText(ctx, client, detail, testedSnapshot) if err != nil { return nil, fmt.Errorf("failed to generate text message: %w", err) } @@ -271,20 +264,32 @@ func GenerateTestReport(ctx context.Context, client client.Client, detail intgte } // generateText generates a text with details for the given state -func generateText(ctx context.Context, client client.Client, integrationTestStatusDetail intgteststat.IntegrationTestStatusDetail, namespace string, componentSnapshotInfos []*gitops.ComponentSnapshotInfo) (string, error) { +func generateText(ctx context.Context, client client.Client, integrationTestStatusDetail intgteststat.IntegrationTestStatusDetail, snapshot *applicationapiv1alpha1.Snapshot) (string, error) { log := log.FromContext(ctx) + + var componentSnapshotInfos []*gitops.ComponentSnapshotInfo + var err error + if componentSnapshotInfoString, ok := snapshot.Annotations[gitops.GroupSnapshotInfoAnnotation]; ok { + componentSnapshotInfos, err = gitops.UnmarshalJSON([]byte(componentSnapshotInfoString)) + if err != nil { + return "", fmt.Errorf("failed to unmarshal JSON string: %w", err) + } + } + + pr_group := snapshot.GetAnnotations()[gitops.PRGroupAnnotation] + if integrationTestStatusDetail.Status == intgteststat.IntegrationTestStatusTestPassed || integrationTestStatusDetail.Status == intgteststat.IntegrationTestStatusTestFail { pipelineRunName := integrationTestStatusDetail.TestPipelineRunName pipelineRun := &tektonv1.PipelineRun{} err := client.Get(ctx, types.NamespacedName{ - Namespace: namespace, + Namespace: snapshot.Namespace, Name: pipelineRunName, }, pipelineRun) if err != nil { if apierrors.IsNotFound(err) { log.Error(err, "Failed to fetch pipelineRun", "pipelineRun.Name", pipelineRunName) - text := fmt.Sprintf("%s\n\n\n(Failed to fetch test result details because pipelineRun %s/%s can not be found.)", integrationTestStatusDetail.Details, namespace, pipelineRunName) + text := fmt.Sprintf("%s\n\n\n(Failed to fetch test result details because pipelineRun %s/%s can not be found.)", integrationTestStatusDetail.Details, snapshot.Namespace, pipelineRunName) return text, nil } @@ -295,7 +300,7 @@ func generateText(ctx context.Context, client client.Client, integrationTestStat if err != nil { return "", fmt.Errorf("error while getting all child taskRuns from pipelineRun %s: %w", pipelineRunName, err) } - text, err := FormatTestsSummary(taskRuns, pipelineRunName, namespace, componentSnapshotInfos, log) + text, err := FormatTestsSummary(taskRuns, pipelineRunName, snapshot.Namespace, componentSnapshotInfos, pr_group, log) if err != nil { return "", err }