Skip to content

Commit e37b5c7

Browse files
authored
fix(STONEINTG-1450): handle missing onboarding branch (#1773)
feat(STONEINTG-1450): check for onboarding branch Added helper functions to handle missing onboarding branches used by integration-service tests. Signed-off-by: Josh Everett <jeverett@redhat.com>
1 parent 621a01e commit e37b5c7

6 files changed

Lines changed: 42 additions & 2 deletions

File tree

pkg/clients/github/git.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,17 @@ func (g *Github) ExistsRef(repository, branchName string) (bool, error) {
6969
func (g *Github) UpdateGithubOrg(githubOrg string) {
7070
g.organization = githubOrg
7171
}
72+
73+
// EnsureBranchExists checks if a branch exists in the repository and creates one from
74+
// fallback branch if not.
75+
func (g *Github) EnsureBranchExists(repository, branchName, fallbackBranch string) error {
76+
exists, err := g.ExistsRef(repository, branchName)
77+
if err != nil {
78+
return fmt.Errorf("error checking if branch '%s' exists: %w", branchName, err)
79+
}
80+
if exists {
81+
return nil
82+
}
83+
// Branch doesn't exist, create it from fallback branch
84+
return g.CreateRef(repository, fallbackBranch, "", branchName)
85+
}

pkg/clients/gitlab/git.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,20 @@ func (gc *GitlabClient) ForkRepository(sourceOrgName, sourceName, targetOrgName,
425425
return forkedProject, nil
426426
}
427427

428+
// EnsureBranchExists checks if a branch exists in the repository and creates one from
429+
// fallback branch if not.
430+
func (gc *GitlabClient) EnsureBranchExists(projectID, branchName, fallbackBranch string) error {
431+
exists, err := gc.ExistsBranch(projectID, branchName)
432+
if err != nil {
433+
return fmt.Errorf("error checking if branch '%s' exists: %w", branchName, err)
434+
}
435+
if exists {
436+
return nil
437+
}
438+
// Branch doesn't exist, create it from fallback branch
439+
return gc.CreateBranch(projectID, branchName, fallbackBranch)
440+
}
441+
428442
func (gc *GitlabClient) GetAllProjects() ([]*gitlab.Project, error) {
429443
listProjectsOptions := &gitlab.ListProjectsOptions{
430444
Membership: gitlab.Ptr(true),

tests/integration-service/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
multiRepoComponentGitRevision = "79402df023e646c5ad108abc879ad1b28799cbc4"
3131
gitlabComponentRepoName = "hacbs-test-project-integration"
3232
componentDefaultBranch = "onboarding"
33+
fallbackBranchName = "main"
3334
componentRevision = "79402df023e646c5ad108abc879ad1b28799cbc4"
3435
referenceDoesntExist = "Reference does not exist"
3536
checkrunStatusCompleted = "completed"

tests/integration-service/gitlab-integration-reporting.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ var _ = framework.IntegrationServiceSuiteDescribe("Gitlab Status Reporting of In
7373
gitlabToken = utils.GetEnv(constants.GITLAB_BOT_TOKEN_ENV, "")
7474
gomega.Expect(gitlabToken).ShouldNot(gomega.BeEmpty(), fmt.Sprintf("'%s' env var is not set", constants.GITLAB_BOT_TOKEN_ENV))
7575

76+
err = f.AsKubeAdmin.CommonController.Gitlab.EnsureBranchExists(projectID, componentDefaultBranch, fallbackBranchName)
77+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
78+
7679
err = f.AsKubeAdmin.CommonController.Gitlab.CreateGitlabNewBranch(projectID, componentBaseBranchName, componentRevision, componentDefaultBranch)
7780
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
7881

tests/integration-service/group-snapshots-tests.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ var _ = framework.IntegrationServiceSuiteDescribe("Creation of group snapshots f
6161

6262
applicationName = createApp(*f, testNamespace)
6363

64+
err = f.AsKubeAdmin.CommonController.Github.EnsureBranchExists(multiComponentRepoNameForGroupSnapshot, multiComponentDefaultBranch, fallbackBranchName)
65+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
66+
67+
err = f.AsKubeAdmin.CommonController.Github.EnsureBranchExists(componentRepoNameForGroupIntegration, multiComponentDefaultBranch, fallbackBranchName)
68+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
69+
6470
// The base branch or a ToBranch where all multi-component definitions will live
6571
multiComponentBaseBranchName = fmt.Sprintf("love-triangle-%s", util.GenerateRandomString(6))
6672
err = f.AsKubeAdmin.CommonController.Github.CreateRef(multiComponentRepoNameForGroupSnapshot, multiComponentDefaultBranch, multiComponentGitRevision, multiComponentBaseBranchName)
@@ -720,7 +726,6 @@ var _ = framework.IntegrationServiceSuiteDescribe("Creation of group snapshots f
720726
})
721727
})
722728

723-
724729
ginkgo.When("IntegrationTestScenario reference to task as pipelinerun resolution", func() {
725730
ginkgo.BeforeAll(func() {
726731
invalidIntegrationTestScenario, err = f.AsKubeAdmin.IntegrationController.CreateIntegrationTestScenario("", applicationName, testNamespace, gitURL, revision, pathInRepoPass, "pipelinerun", []string{"application"})

tests/integration-service/integration.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,10 @@ func createComponent(f framework.Framework, testNamespace, applicationName, comp
433433
pacBranchName := constants.PaCPullRequestBranchPrefix + componentName
434434
componentBaseBranchName := fmt.Sprintf("base-%s", util.GenerateRandomString(6))
435435

436-
err := f.AsKubeAdmin.CommonController.Github.CreateRef(componentRepoName, componentDefaultBranch, componentRevision, componentBaseBranchName)
436+
err := f.AsKubeAdmin.CommonController.Github.EnsureBranchExists(componentRepoName, componentDefaultBranch, fallbackBranchName)
437+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
438+
439+
err = f.AsKubeAdmin.CommonController.Github.CreateRef(componentRepoName, componentDefaultBranch, componentRevision, componentBaseBranchName)
437440
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
438441

439442
// get the build pipeline bundle annotation

0 commit comments

Comments
 (0)