Skip to content

Commit

Permalink
Merge pull request #907 from chipspeak/STONEINTG-851
Browse files Browse the repository at this point in the history
feat(STONEINTG-851): allow status reports on push events
  • Loading branch information
chipspeak authored Nov 11, 2024
2 parents 7e31818 + 63497df commit 693c579
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ func (a *Adapter) EnsureStatusReportedInSnapshot() (controller.OperationResult,
return controller.RequeueWithError(fmt.Errorf("failed to update test status in snapshot: %w", err))
}

// Remove the finalizer from Integration PLRs only if they are related to Snapshots created by Push event
// If they are related, then the statusreport controller removes the finalizers from these PLRs
if gitops.IsSnapshotCreatedByPACPushEvent(a.snapshot) && (h.HasPipelineRunFinished(a.pipelineRun) || pipelinerunStatus == intgteststat.IntegrationTestStatusDeleted) {
// Remove the finalizer from Integration PLRs if the snapshot is the override and its PLR has finished
if gitops.IsOverrideSnapshot(a.snapshot) && (h.HasPipelineRunFinished(a.pipelineRun) || pipelinerunStatus == intgteststat.IntegrationTestStatusDeleted) {
err = h.RemoveFinalizerFromPipelineRun(a.context, a.client, a.logger, a.pipelineRun, h.IntegrationPipelineRunFinalizer)
if err != nil {
return controller.RequeueWithError(fmt.Errorf("failed to remove the finalizer: %w", err))
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/statusreport/statusreport_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ func NewAdapter(context context.Context, snapshot *applicationapiv1alpha1.Snapsh
// 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 group snapshot
func (a *Adapter) EnsureSnapshotTestStatusReportedToGitProvider() (controller.OperationResult, error) {
if gitops.IsSnapshotCreatedByPACPushEvent(a.snapshot) && !gitops.IsGroupSnapshot(a.snapshot) {
if gitops.IsOverrideSnapshot(a.snapshot) {
return controller.ContinueProcessing()
}

err := a.ReportSnapshotStatus(a.snapshot)
if err != nil {
a.logger.Error(err, "failed to report test status to git provider for snapshot",
Expand Down
3 changes: 2 additions & 1 deletion status/reporter_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ func (csu *CommitStatusUpdater) UpdateStatus(ctx context.Context, report TestRep
return err
}
// Create a comment when integration test is neither pending nor inprogress since comment for pending/inprogress is less meaningful and there is commitStatus for all statuses
if report.Status != intgteststat.IntegrationTestStatusPending && report.Status != intgteststat.IntegrationTestStatusInProgress {
_, isPullRequest := csu.snapshot.GetAnnotations()[gitops.PipelineAsCodePullRequestAnnotation]
if report.Status != intgteststat.IntegrationTestStatusPending && report.Status != intgteststat.IntegrationTestStatusInProgress && isPullRequest {
err = csu.updateStatusInComment(ctx, report)
if err != nil {
csu.logger.Error(err, "failed to update comment", "snapshot.NameSpace", csu.snapshot.Namespace, "snapshot.Name", csu.snapshot.Name, "scenarioName", report.ScenarioName)
Expand Down
22 changes: 22 additions & 0 deletions status/reporter_github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ var _ = Describe("GitHubReporter", func() {
})

It("creates a commit status for snapshot with correct textual data", func() {
hasSnapshot.Labels["pac.test.appstudio.openshift.io/pull-request"] = "999"
Expect(reporter.ReportStatus(
context.TODO(),
status.TestReport{
Expand All @@ -529,6 +530,27 @@ var _ = Describe("GitHubReporter", func() {
Expect(mockGitHubClient.CreateCommentResult.body).To(Equal("### Integration test for snapshot snapshot-sample and scenario scenario1 failed\n\ndetailed text here"))
})

It("creates a commit status for snapshot with correct textual data, but does not create a comment for push event", func() {
delete(hasSnapshot.Annotations, "pac.test.appstudio.openshift.io/pull-request")
hasSnapshot.Labels["pac.test.appstudio.openshift.io/event-type"] = "push"
Expect(reporter.ReportStatus(
context.TODO(),
status.TestReport{
FullName: "fullname/scenario1",
ScenarioName: "scenario1",
SnapshotName: "snapshot-sample",
ComponentName: "component-sample",
Status: integrationteststatus.IntegrationTestStatusEnvironmentProvisionError_Deprecated,
Summary: "Integration test for snapshot snapshot-sample and scenario scenario1 failed",
Text: "detailed text here",
})).To(Succeed(), "ReportStatus should succeed")

Expect(mockGitHubClient.CreateCommitStatusResult.state).To(Equal(gitops.IntegrationTestStatusErrorGithub))
Expect(mockGitHubClient.CreateCommitStatusResult.description).To(Equal("Integration test for snapshot snapshot-sample and scenario scenario1 failed"))
Expect(mockGitHubClient.CreateCommitStatusResult.statusContext).To(Equal("fullname/scenario1"))
Expect(mockGitHubClient.CreateCommentResult.body).To(BeEmpty(), "Expected no comment to be created for PAC push event")
})

DescribeTable(
"reports correct github statuses from test statuses",
func(teststatus integrationteststatus.IntegrationTestStatus, ghstatus string) {
Expand Down
18 changes: 11 additions & 7 deletions status/reporter_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,19 @@ func (r *GitLabReporter) Initialize(ctx context.Context, snapshot *applicationap
}

mergeRequestStr, found := annotations[gitops.PipelineAsCodePullRequestAnnotation]
if !found {
if !found && !gitops.IsSnapshotCreatedByPACPushEvent(snapshot) {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("pull-request annotation not found %q", gitops.PipelineAsCodePullRequestAnnotation))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
r.mergeRequest, err = strconv.Atoi(mergeRequestStr)
if err != nil {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to convert merge request number '%s' to integer: %s", mergeRequestStr, err.Error()))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError

if found {
r.mergeRequest, err = strconv.Atoi(mergeRequestStr)
if err != nil && !gitops.IsSnapshotCreatedByPACPushEvent(snapshot) {
unRecoverableError = helpers.NewUnrecoverableMetadataError(fmt.Sprintf("failed to convert merge request number '%s' to integer: %s", mergeRequestStr, err.Error()))
r.logger.Error(unRecoverableError, "snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
return unRecoverableError
}
}

r.snapshot = snapshot
Expand Down Expand Up @@ -273,7 +276,8 @@ func (r *GitLabReporter) ReportStatus(ctx context.Context, report TestReport) er
}

// Create a note when integration test is neither pending nor inprogress since comment for pending/inprogress is less meaningful
if report.Status != intgteststat.IntegrationTestStatusPending && report.Status != intgteststat.IntegrationTestStatusInProgress {
_, isMergeRequest := r.snapshot.GetAnnotations()[gitops.PipelineAsCodePullRequestAnnotation]
if report.Status != intgteststat.IntegrationTestStatusPending && report.Status != intgteststat.IntegrationTestStatusInProgress && isMergeRequest {
err := r.updateStatusInComment(report)
if err != nil {
return err
Expand Down
27 changes: 27 additions & 0 deletions status/reporter_gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,33 @@ var _ = Describe("GitLabReporter", func() {
})).To(Succeed())
})

It("creates a commit status for push snapshot with correct textual data without comments", func() {

pushSnapshot := hasSnapshot.DeepCopy()
// Removing the pull request annotation and adding the push label
delete(pushSnapshot.Annotations, gitops.PipelineAsCodePullRequestAnnotation)
pushSnapshot.Annotations[gitops.PipelineAsCodeEventTypeLabel] = "Push"

pushEventReporter := status.NewGitLabReporter(log, mockK8sClient)

err := pushEventReporter.Initialize(context.TODO(), pushSnapshot)
Expect(err).To(Succeed())

summary := "Integration test for snapshot snapshot-sample and scenario scenario1 failed"

muxCommitStatusPost(mux, sourceProjectID, digest, summary)

Expect(pushEventReporter.ReportStatus(
context.TODO(),
status.TestReport{
FullName: "fullname/scenario1",
ScenarioName: "scenario1",
Status: integrationteststatus.IntegrationTestStatusEnvironmentProvisionError_Deprecated,
Summary: summary,
Text: "detailed text here",
})).To(Succeed())
})

It("creates a commit status for snapshot with TargetURL in CommitStatus", func() {

PipelineRunName := "TestPipeline"
Expand Down

0 comments on commit 693c579

Please sign in to comment.