Skip to content

Commit

Permalink
Merge pull request #987 from hongweiliu17/STONEINTG-1096
Browse files Browse the repository at this point in the history
feat(STONEINTG-1096): move pr group name from summary to text
  • Loading branch information
hongweiliu17 authored Jan 16, 2025
2 parents a0ef20f + b3fc7ce commit ee6b8b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
7 changes: 4 additions & 3 deletions status/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -61,6 +61,7 @@ type SummaryTemplateData struct {
TaskRuns []*helpers.TaskRun
PipelineRunName string
Namespace string
PRGroup string
ComponentSnapshotInfos []*gitops.ComponentSnapshotInfo
Logger logr.Logger
}
Expand All @@ -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,
Expand All @@ -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
Expand Down
15 changes: 9 additions & 6 deletions status/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"))
})
Expand All @@ -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())
Expand All @@ -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))
})
Expand Down Expand Up @@ -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())
})
})
Expand All @@ -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())
})
})
Expand Down Expand Up @@ -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 | <a href=\"https://definetly.not.prod/preview/application-pipeline/ns/default/pipelinerun/buildPLR3\">buildPLR3</a> | <a href=\"https://github.com/example/pull/1\">example</a> |"))
Expect(err).To(Succeed())
})
Expand Down
29 changes: 17 additions & 12 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down

0 comments on commit ee6b8b2

Please sign in to comment.