Skip to content

Commit e62b952

Browse files
committed
fix(STONEINTG-1385): ignore refs/heads prefix from source branch
* The pipeline-as-code source branch annotation can contain the `refs/heads` prefix in it which needs to be ignored when detecting merge queues and extracting the PR number Signed-off-by: dirgim <[email protected]> rh-pre-commit.version: 2.2.0 rh-pre-commit.check-secrets: ENABLED
1 parent 2849e1d commit e62b952

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

gitops/snapshot.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ const (
207207
// PipelineAsCodeGitHubMergeQueueBranchPrefix is the prefix added to temporary branches which are created for merge queues
208208
PipelineAsCodeGitHubMergeQueueBranchPrefix = "gh-readonly-queue/"
209209

210+
// GitRefBranchPrefix is the git prefix denoting a reference is a branch
211+
GitRefBranchPrefix = "refs/heads/"
212+
210213
//AppStudioTestSucceededCondition is the condition for marking if the AppStudio Tests succeeded for the Snapshot.
211214
AppStudioTestSucceededCondition = "AppStudioTestSucceeded"
212215

@@ -783,7 +786,7 @@ func CompareSnapshots(expectedSnapshot *applicationapiv1alpha1.Snapshot, foundSn
783786

784787
func IsSnapshotCreatedByPACMergeQueueEvent(snapshot *applicationapiv1alpha1.Snapshot) bool {
785788
if branch, found := snapshot.Annotations[PipelineAsCodeSourceBranchAnnotation]; found {
786-
if strings.HasPrefix(branch, PipelineAsCodeGitHubMergeQueueBranchPrefix) {
789+
if strings.HasPrefix(strings.TrimPrefix(branch, GitRefBranchPrefix), PipelineAsCodeGitHubMergeQueueBranchPrefix) {
787790
return true
788791
}
789792
}
@@ -898,8 +901,9 @@ func ExtractPullRequestNumberFromMergeQueueSnapshot(snapshot *applicationapiv1al
898901

899902
// If the PR number is not found above, attempt to extract it from the source branch name of the merge queue
900903
// The branch should be in the format of 'gh-readonly-queue/{original_branch_name}/pr-{pull_request_number}-{sha}'
901-
if snapshot.Annotations != nil && snapshot.Annotations[PipelineAsCodeSourceBranchAnnotation] != "" {
902-
branchWithoutPrefix := strings.Split(snapshot.Annotations[PipelineAsCodeSourceBranchAnnotation], "/")
904+
if branch, found := snapshot.Annotations[PipelineAsCodeSourceBranchAnnotation]; found {
905+
// remove the refs/heads prefix to get the actual branch name
906+
branchWithoutPrefix := strings.Split(strings.TrimPrefix(branch, GitRefBranchPrefix), "/")
903907
if len(branchWithoutPrefix) > 1 {
904908
branchSections := strings.Split(branchWithoutPrefix[len(branchWithoutPrefix)-1], "-")
905909
if len(branchSections) > 1 && branchSections[0] == "pr" && branchSections[1] != "" {

gitops/snapshot_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,10 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
708708
pullRequestNumber = gitops.ExtractPullRequestNumberFromMergeQueueSnapshot(mergeQueueSnapshot)
709709
Expect(pullRequestNumber).To(Equal("2987"))
710710

711+
mergeQueueSnapshot.Annotations[gitops.PipelineAsCodeSourceBranchAnnotation] = "refs/heads/gh-readonly-queue/main/pr-7-54e7d2bfec0e0570915f5770c890407c714e6139"
712+
pullRequestNumber = gitops.ExtractPullRequestNumberFromMergeQueueSnapshot(mergeQueueSnapshot)
713+
Expect(pullRequestNumber).To(Equal("7"))
714+
711715
mergeQueueSnapshot.Annotations[gitops.PipelineAsCodePullRequestAnnotation] = "214"
712716
pullRequestNumber = gitops.ExtractPullRequestNumberFromMergeQueueSnapshot(mergeQueueSnapshot)
713717
Expect(pullRequestNumber).To(Equal("214"))
@@ -1069,6 +1073,8 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
10691073
snapshot.Labels[gitops.PipelineAsCodeEventTypeLabel] = gitops.PipelineAsCodePushType
10701074
snapshot.Annotations[gitops.PipelineAsCodeSourceBranchAnnotation] = "gh-readonly-queue/main/pr-2987-bda9b312bf224a6b5fb1e7ed6ae76dd9e6b1b75b"
10711075
Expect(gitops.IsSnapshotCreatedByPACPushEvent(snapshot)).To(BeFalse())
1076+
snapshot.Annotations[gitops.PipelineAsCodeSourceBranchAnnotation] = "refs/heads/gh-readonly-queue/main/pr-7-54e7d2bfec0e0570915f5770c890407c714e6139"
1077+
Expect(gitops.IsSnapshotCreatedByPACPushEvent(snapshot)).To(BeFalse())
10721078
})
10731079

10741080
It("Testing UnmarshalJSON", func() {

tekton/build_pipeline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func GenerateSHA(str string) string {
106106
// IsPLRCreatedByPACPushEvent checks if a PLR has label PipelineAsCodeEventTypeLabel and with push or Push value
107107
func IsPLRCreatedByPACPushEvent(plr *tektonv1.PipelineRun) bool {
108108
if branch, found := plr.Annotations[consts.PipelineAsCodeSourceBranchAnnotation]; found {
109-
if strings.HasPrefix(branch, consts.PipelineAsCodeGitHubMergeQueueBranchPrefix) {
109+
if strings.HasPrefix(strings.TrimPrefix(branch, consts.GitRefBranchPrefix), consts.PipelineAsCodeGitHubMergeQueueBranchPrefix) {
110110
return false
111111
}
112112
}

tekton/build_pipeline_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ var _ = Describe("build pipeline", func() {
153153
buildPipelineRun.Labels[tektonconsts.PipelineAsCodeEventTypeLabel] = "push"
154154
buildPipelineRun.Annotations[tektonconsts.PipelineAsCodeSourceBranchAnnotation] = "gh-readonly-queue/main/pr-2987-bda9b312bf224a6b5fb1e7ed6ae76dd9e6b1b75b"
155155
Expect(tekton.IsPLRCreatedByPACPushEvent(buildPipelineRun)).To(BeFalse())
156+
buildPipelineRun.Annotations[tektonconsts.PipelineAsCodeSourceBranchAnnotation] = "refs/heads/gh-readonly-queue/main/pr-7-54e7d2bfec0e0570915f5770c890407c714e6139"
157+
Expect(tekton.IsPLRCreatedByPACPushEvent(buildPipelineRun)).To(BeFalse())
156158
})
157159

158160
It("can get the latest build pipelinerun for given component", func() {

tekton/consts/consts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ const (
7070
// main branch in github/gitlab
7171
MainBranch = "main"
7272

73+
// GitRefBranchPrefix is the git prefix denoting a reference is a branch
74+
GitRefBranchPrefix = "refs/heads/"
75+
7376
// PipelineAsCodeSourceBranchAnnotation is the branch name of the the pull request is created from
7477
PipelineAsCodeSourceBranchAnnotation = "pipelinesascode.tekton.dev/source-branch"
7578

0 commit comments

Comments
 (0)