Skip to content

Commit

Permalink
Merge pull request konflux-ci#858 from hongweiliu17/STONEINTG-998-fix
Browse files Browse the repository at this point in the history
fix(STONEINTG-998): fix pipeline as code event
  • Loading branch information
hongweiliu17 authored Sep 5, 2024
2 parents b6660f7 + 029231e commit e79a515
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 36 deletions.
3 changes: 2 additions & 1 deletion gitops/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ func CompareSnapshots(expectedSnapshot *applicationapiv1alpha1.Snapshot, foundSn
func IsSnapshotCreatedByPACPushEvent(snapshot *applicationapiv1alpha1.Snapshot) bool {
return metadata.HasLabelWithValue(snapshot, PipelineAsCodeEventTypeLabel, PipelineAsCodePushType) ||
metadata.HasLabelWithValue(snapshot, PipelineAsCodeEventTypeLabel, PipelineAsCodeGLPushType) ||
!metadata.HasLabel(snapshot, PipelineAsCodeEventTypeLabel)
!metadata.HasLabel(snapshot, PipelineAsCodeEventTypeLabel) ||
!metadata.HasLabel(snapshot, PipelineAsCodePullRequestAnnotation)
}

// IsSnapshotCreatedBySamePACEvent checks if the two snapshot are created by the same PAC event
Expand Down
7 changes: 4 additions & 3 deletions gitops/snapshot_predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ var _ = Describe("Predicates", Ordered, func() {
Name: snapshotAnnotationNew,
Namespace: namespace,
Labels: map[string]string{
gitops.SnapshotTypeLabel: gitops.SnapshotComponentType,
gitops.SnapshotComponentLabel: componentName,
"pac.test.appstudio.openshift.io/event-type": "pull_request",
gitops.SnapshotTypeLabel: gitops.SnapshotComponentType,
gitops.SnapshotComponentLabel: componentName,
"pac.test.appstudio.openshift.io/event-type": "pull_request",
"pac.test.appstudio.openshift.io/pull-request": "1",
},
Annotations: map[string]string{
gitops.SnapshotTestsStatusAnnotation: "[{\"scenario\":\"scenario-1\",\"status\":\"TestPassed\",\"startTime\":\"2023-07-26T16:57:49+02:00\",\"completionTime\":\"2023-07-26T17:57:49+02:00\",\"details\": \"test pass\"}]",
Expand Down
23 changes: 14 additions & 9 deletions gitops/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
Name: hasComSnapshot1Name,
Namespace: namespace,
Labels: map[string]string{
gitops.SnapshotTypeLabel: gitops.SnapshotComponentType,
gitops.SnapshotComponentLabel: hasComSnapshot1Name,
gitops.PipelineAsCodeEventTypeLabel: gitops.PipelineAsCodePullRequestType,
gitops.SnapshotTypeLabel: gitops.SnapshotComponentType,
gitops.SnapshotComponentLabel: hasComSnapshot1Name,
gitops.PipelineAsCodeEventTypeLabel: gitops.PipelineAsCodePullRequestType,
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
"test.appstudio.openshift.io/pr-last-update": "2023-08-26T17:57:50+02:00",
Expand Down Expand Up @@ -175,9 +176,10 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
Name: hasComSnapshot2Name,
Namespace: namespace,
Labels: map[string]string{
gitops.SnapshotTypeLabel: gitops.SnapshotComponentType,
gitops.SnapshotComponentLabel: hasComSnapshot2Name,
gitops.PipelineAsCodeEventTypeLabel: gitops.PipelineAsCodePullRequestType,
gitops.SnapshotTypeLabel: gitops.SnapshotComponentType,
gitops.SnapshotComponentLabel: hasComSnapshot2Name,
gitops.PipelineAsCodeEventTypeLabel: gitops.PipelineAsCodePullRequestType,
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
"test.appstudio.openshift.io/pr-last-update": "2023-08-26T17:57:50+02:00",
Expand Down Expand Up @@ -213,9 +215,10 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
Name: hasComSnapshot3Name,
Namespace: namespace,
Labels: map[string]string{
gitops.SnapshotTypeLabel: gitops.SnapshotComponentType,
gitops.SnapshotComponentLabel: hasComSnapshot3Name,
gitops.PipelineAsCodeEventTypeLabel: gitops.PipelineAsCodePullRequestType,
gitops.SnapshotTypeLabel: gitops.SnapshotComponentType,
gitops.SnapshotComponentLabel: hasComSnapshot3Name,
gitops.PipelineAsCodeEventTypeLabel: gitops.PipelineAsCodePullRequestType,
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
"test.appstudio.openshift.io/pr-last-update": "2023-08-26T17:57:50+02:00",
Expand Down Expand Up @@ -547,6 +550,7 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
Expect(reasons).To(HaveLen(1))

hasSnapshot.Labels[gitops.PipelineAsCodeEventTypeLabel] = gitops.PipelineAsCodePullRequestType
hasSnapshot.Labels[gitops.PipelineAsCodePullRequestAnnotation] = "1"
canBePromoted, reasons = gitops.CanSnapshotBePromoted(hasSnapshot)
Expect(canBePromoted).To(BeFalse())
Expect(reasons).To(HaveLen(2))
Expand Down Expand Up @@ -818,6 +822,7 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {

// A component Snapshot for pull request event referencing the component-sample
hasSnapshot.Labels[gitops.PipelineAsCodeEventTypeLabel] = gitops.PipelineAsCodePullRequestType
hasSnapshot.Labels[gitops.PipelineAsCodePullRequestAnnotation] = "1"
filteredScenarios = gitops.FilterIntegrationTestScenariosWithContext(&allScenarios, hasSnapshot)
Expect(*filteredScenarios).To(HaveLen(6))

Expand Down
10 changes: 6 additions & 4 deletions internal/controller/buildpipeline/buildpipeline_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ var _ = Describe("Pipeline Adapter", Ordered, func() {
Name: "snapshot-sample",
Namespace: "default",
Labels: map[string]string{
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: hasComp.Name,
gitops.PipelineAsCodeEventTypeLabel: gitops.PipelineAsCodePullRequestType,
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: hasComp.Name,
gitops.PipelineAsCodeEventTypeLabel: gitops.PipelineAsCodePullRequestType,
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
gitops.PipelineAsCodeInstallationIDAnnotation: "123",
Expand Down Expand Up @@ -260,7 +261,8 @@ var _ = Describe("Pipeline Adapter", Ordered, func() {
"appstudio.openshift.io/component": "component-sample",
"build.appstudio.redhat.com/target_branch": "main",
"pipelinesascode.tekton.dev/event-type": "pull_request",
customLabel: "custom-label",
"pipelinesascode.tekton.dev/pull-request": "1",
customLabel: "custom-label",
},
Annotations: map[string]string{
"appstudio.redhat.com/updateComponentOnSuccess": "false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ var _ = Describe("Pipeline Adapter", Ordered, func() {
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: hasComp.Name,
"pac.test.appstudio.openshift.io/event-type": "pull_request",
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
gitops.PipelineAsCodeInstallationIDAnnotation: "123",
Expand Down
11 changes: 8 additions & 3 deletions internal/controller/snapshot/snapshot_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,10 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
Name: "snapshotpr-sample",
Namespace: "default",
Labels: map[string]string{
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: "component-sample",
gitops.PipelineAsCodeEventTypeLabel: "pull_request",
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: "component-sample",
gitops.PipelineAsCodeEventTypeLabel: "pull_request",
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
gitops.PipelineAsCodeInstallationIDAnnotation: "123",
Expand Down Expand Up @@ -410,6 +411,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
"pac.test.appstudio.openshift.io/url-org": "testorg",
"pac.test.appstudio.openshift.io/url-repository": "testrepo",
gitops.PipelineAsCodeSHALabel: "sha",
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
"test.appstudio.openshift.io/pr-last-update": "2023-08-26T17:57:50+02:00",
Expand Down Expand Up @@ -456,6 +458,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
"pac.test.appstudio.openshift.io/url-org": "testorg",
"pac.test.appstudio.openshift.io/url-repository": "testrepo",
gitops.PipelineAsCodeSHALabel: "sha",
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
"test.appstudio.openshift.io/pr-last-update": "2023-08-26T17:57:50+02:00",
Expand Down Expand Up @@ -492,6 +495,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
"pac.test.appstudio.openshift.io/url-org": "testorg",
"pac.test.appstudio.openshift.io/url-repository": "testrepo",
gitops.PipelineAsCodeSHALabel: "sha",
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
"test.appstudio.openshift.io/pr-last-update": "2023-08-26T17:57:50+02:00",
Expand Down Expand Up @@ -876,6 +880,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
Expect(err).ShouldNot(HaveOccurred())
gitops.SetSnapshotIntegrationStatusAsInvalid(hasSnapshot, "snapshot invalid")
hasSnapshot.Labels[gitops.PipelineAsCodeEventTypeLabel] = gitops.PipelineAsCodePullRequestType
hasSnapshot.Labels[gitops.PipelineAsCodePullRequestAnnotation] = "1"
Expect(gitops.HaveAppStudioTestsSucceeded(hasSnapshot)).To(BeFalse())
Expect(gitops.IsSnapshotValid(hasSnapshot)).To(BeFalse())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
gitops.SnapshotComponentLabel: "component-sample",
"build.appstudio.redhat.com/pipeline": "enterprise-contract",
gitops.PipelineAsCodeEventTypeLabel: "pull_request",
gitops.PipelineAsCodePullRequestAnnotation: "1",
"pac.test.appstudio.openshift.io/url-org": "testorg",
"pac.test.appstudio.openshift.io/url-repository": "testrepo",
"pac.test.appstudio.openshift.io/sha": "testsha",
Expand Down
11 changes: 6 additions & 5 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ var _ = Describe("Loader", Ordered, func() {
Name: snapshotName,
Namespace: "default",
Labels: map[string]string{
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: "component-sample",
gitops.BuildPipelineRunNameLabel: "pipelinerun-sample",
gitops.PRGroupHashLabel: "featuresha",
gitops.PipelineAsCodeEventTypeLabel: "pull_request",
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: "component-sample",
gitops.BuildPipelineRunNameLabel: "pipelinerun-sample",
gitops.PRGroupHashLabel: "featuresha",
gitops.PipelineAsCodeEventTypeLabel: "pull_request",
gitops.PipelineAsCodePullRequestAnnotation: "1",
},
Annotations: map[string]string{
gitops.PipelineAsCodeInstallationIDAnnotation: "123",
Expand Down
14 changes: 3 additions & 11 deletions tekton/build_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@ const (
// PipelineAsCodeSourceRepoOrg is the repo org build PLR is triggered by
PipelineAsCodeSourceRepoOrg = "pipelinesascode.tekton.dev/url-org"

// PipelineAsCodeEventTypeLabel is the type of event which triggered the pipelinerun in build service
PipelineAsCodeEventTypeLabel = "pipelinesascode.tekton.dev/event-type"

// PipelineAsCodePushType is the type of push event which triggered the pipelinerun in build service
PipelineAsCodePushType = "push"

// PipelineAsCodeGLPushType is the type of gitlab push event which triggered the pipelinerun in build service
PipelineAsCodeGLPushType = "Push"
// PipelineAsCodePullRequestLabel is the type of event which triggered the pipelinerun in build service
PipelineAsCodePullRequestLabel = "pipelinesascode.tekton.dev/pull-request"
)

// AnnotateBuildPipelineRun sets annotation for a build pipelineRun in defined context and returns that pipeline
Expand Down Expand Up @@ -126,7 +120,5 @@ func GenerateSHA(str string) string {

// IsPLRCreatedByPACPushEvent checks if a PLR has label PipelineAsCodeEventTypeLabel and with push or Push value
func IsPLRCreatedByPACPushEvent(plr *tektonv1.PipelineRun) bool {
return metadata.HasLabelWithValue(plr, PipelineAsCodeEventTypeLabel, PipelineAsCodePushType) ||
metadata.HasLabelWithValue(plr, PipelineAsCodeEventTypeLabel, PipelineAsCodeGLPushType) ||
!metadata.HasLabel(plr, PipelineAsCodeEventTypeLabel)
return !metadata.HasLabel(plr, PipelineAsCodePullRequestLabel)
}
1 change: 1 addition & 0 deletions tekton/build_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var _ = Describe("build pipeline", func() {
"pipelines.openshift.io/strategy": "s2i",
"appstudio.openshift.io/component": "component-sample",
"build.appstudio.redhat.com/target_branch": "main",
"pipelinesascode.tekton.dev/pull-request": "1",
"pipelinesascode.tekton.dev/event-type": "pull_request",
},
Annotations: map[string]string{
Expand Down

0 comments on commit e79a515

Please sign in to comment.