Skip to content

Commit

Permalink
feat(STONEINTG-999): report test status for group snapshot
Browse files Browse the repository at this point in the history
* Update EnsureSnapshotTestStatusReportedToGitProvider to support
  group snapshot

Signed-off-by: Hongwei Liu <hongliu@redhat.com>
  • Loading branch information
hongweiliu17 committed Sep 18, 2024
1 parent 7d2a843 commit dcded64
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 251 deletions.
1 change: 1 addition & 0 deletions gitops/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ func IsComponentSnapshot(snapshot *applicationapiv1alpha1.Snapshot) bool {
return metadata.HasLabelWithValue(snapshot, SnapshotTypeLabel, SnapshotComponentType)
}

// IsGroupSnapshot returns true if snapshot label 'test.appstudio.openshift.io/type' is 'group'
func IsGroupSnapshot(snapshot *applicationapiv1alpha1.Snapshot) bool {
return metadata.HasLabelWithValue(snapshot, SnapshotTypeLabel, SnapshotGroupType)
}
Expand Down
120 changes: 111 additions & 9 deletions internal/controller/statusreport/statusreport_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package statusreport

import (
"context"
e "errors"
"fmt"
"time"

"github.com/konflux-ci/operator-toolkit/controller"
applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/konflux-ci/integration-service/api/v1beta2"
Expand Down Expand Up @@ -66,19 +68,14 @@ func NewAdapter(context context.Context, snapshot *applicationapiv1alpha1.Snapsh

// EnsureSnapshotTestStatusReportedToGitProvider will ensure that integration test status is reported to the git provider
// which (indirectly) triggered its execution.
// The status is reported to git provider if it is a component snapshot
// Or reported to git providers which trigger component snapshots included in group snapshot if it is a gorup snapshot
func (a *Adapter) EnsureSnapshotTestStatusReportedToGitProvider() (controller.OperationResult, error) {
if gitops.IsSnapshotCreatedByPACPushEvent(a.snapshot) {
if gitops.IsSnapshotCreatedByPACPushEvent(a.snapshot) && !gitops.IsGroupSnapshot(a.snapshot) {
return controller.ContinueProcessing()
}

reporter := a.status.GetReporter(a.snapshot)
if reporter == nil {
a.logger.Info("No suitable reporter found, skipping report")
return controller.ContinueProcessing()
}
a.logger.Info(fmt.Sprintf("Detected reporter: %s", reporter.GetReporterName()))

err := a.status.ReportSnapshotStatus(a.context, reporter, a.snapshot)
err := a.ReportSnapshotStatus(a.context, a.snapshot)
if err != nil {
a.logger.Error(err, "failed to report test status to git provider for snapshot",
"snapshot.Namespace", a.snapshot.Namespace, "snapshot.Name", a.snapshot.Name)
Expand Down Expand Up @@ -238,3 +235,108 @@ func (a *Adapter) findUntriggeredIntegrationTestFromStatus(integrationTestScenar
}
return ""
}

// ReportSnapshotStatus reports status of all integration tests into Pull Requests from component snapshot or group snapshot
func (a *Adapter) ReportSnapshotStatus(ctx context.Context, testedSnapshot *applicationapiv1alpha1.Snapshot) error {

statuses, err := gitops.NewSnapshotIntegrationTestStatusesFromSnapshot(testedSnapshot)
if err != nil {
a.logger.Error(err, "failed to get test status annotations from snapshot",
"snapshot.Namespace", testedSnapshot.Namespace, "snapshot.Name", testedSnapshot.Name)
return err
}

integrationTestStatusDetails := statuses.GetStatuses()
if len(integrationTestStatusDetails) == 0 {
// no tests to report, skip
a.logger.Info("No test result to report to GitHub, skipping",
"snapshot.Namespace", testedSnapshot.Namespace, "snapshot.Name", testedSnapshot.Name)
return nil
}

// get the component snapshot list that include the git provider info the report will be reported to
destinationSnapshots := make([]*applicationapiv1alpha1.Snapshot, 0)
if gitops.IsComponentSnapshot(testedSnapshot) {
destinationSnapshots = append(destinationSnapshots, testedSnapshot)
} else if gitops.IsGroupSnapshot(testedSnapshot) {
// get component snapshots from group snapshot annation GroupSnapshotInfoAnnotation
destinationSnapshots, err = status.GetComponentSnapshotsFromGroupSnapshot(ctx, a.client, testedSnapshot)
if err != nil {
a.logger.Error(err, "failed to get component snapshots included in group snapshot",
"snapshot.NameSpace", testedSnapshot.Namespace, "snapshot.Name", testedSnapshot.Name)
return fmt.Errorf("failed to get component snapshots included in group snapshot %s/%s", testedSnapshot.Namespace, testedSnapshot.Name)
}
}

status.MigrateSnapshotToReportStatus(testedSnapshot, integrationTestStatusDetails)

srs, err := status.NewSnapshotReportStatusFromSnapshot(testedSnapshot)
if err != nil {
a.logger.Error(err, "failed to get latest snapshot write metadata annotation for snapshot",
"snapshot.NameSpace", testedSnapshot.Namespace, "snapshot.Name", testedSnapshot.Name)
srs, _ = status.NewSnapshotReportStatus("")
}

// Report the integration test status to pr/commit included in the tested component snapshot
// or the component snapshot included in group snapshot
for _, destinationComponentSnapshot := range destinationSnapshots {
destinationComponentSnapshot := destinationComponentSnapshot
reporter := a.status.GetReporter(destinationComponentSnapshot)
if reporter == nil {
a.logger.Info("No suitable reporter found, skipping report")
continue
}
a.logger.Info(fmt.Sprintf("Detected reporter: %s", reporter.GetReporterName()))

if err := reporter.Initialize(ctx, destinationComponentSnapshot); err != nil {
a.logger.Error(err, "Failed to initialize reporter", "reporter", reporter.GetReporterName())
return fmt.Errorf("failed to initialize reporter: %w", err)
}
a.logger.Info("Reporter initialized", "reporter", reporter.GetReporterName())

// set componentName to component name of component snapshot or pr group name of group snapshot when reporting status to git provider
componentName := ""
if gitops.IsGroupSnapshot(testedSnapshot) {
componentName = "pr group " + testedSnapshot.Annotations[gitops.PRGroupAnnotation]
} else if gitops.IsComponentSnapshot(testedSnapshot) {
componentName = testedSnapshot.Labels[gitops.SnapshotComponentLabel]
}

err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
for _, integrationTestStatusDetail := range integrationTestStatusDetails {
if srs.IsNewer(integrationTestStatusDetail.ScenarioName, integrationTestStatusDetail.LastUpdateTime) {
a.logger.Info("Integration Test contains new status updates", "scenario.Name", integrationTestStatusDetail.ScenarioName)
} else {
//integration test contains no changes
continue
}
testReport, reportErr := status.GenerateTestReport(ctx, a.client, *integrationTestStatusDetail, testedSnapshot, componentName)
if reportErr != nil {
if writeErr := status.WriteSnapshotReportStatus(ctx, a.client, testedSnapshot, srs); writeErr != nil { // try to write what was already written
return fmt.Errorf("failed to generate test report AND write snapshot report status metadata: %w", e.Join(reportErr, writeErr))
}
return fmt.Errorf("failed to generate test report: %w", reportErr)
}
if reportStatusErr := reporter.ReportStatus(ctx, *testReport); reportStatusErr != nil {
if writeErr := status.WriteSnapshotReportStatus(ctx, a.client, testedSnapshot, srs); writeErr != nil { // try to write what was already written
return fmt.Errorf("failed to report status AND write snapshot report status metadata: %w", e.Join(reportStatusErr, writeErr))
}
return fmt.Errorf("failed to update status: %w", reportStatusErr)
}
srs.SetLastUpdateTime(integrationTestStatusDetail.ScenarioName, integrationTestStatusDetail.LastUpdateTime)
}
if err := status.WriteSnapshotReportStatus(ctx, a.client, testedSnapshot, srs); err != nil {
return fmt.Errorf("failed to write snapshot report status metadata: %w", err)
}
return err
})
}

if err != nil {
return fmt.Errorf("issue occured during generating or updating report status: %w", err)
}

a.logger.Info(fmt.Sprintf("Successfully updated the %s annotation", gitops.SnapshotStatusReportAnnotation), "snapshot.Name", testedSnapshot.Name)

return nil
}
Loading

0 comments on commit dcded64

Please sign in to comment.