Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change clone URL check for gitlab to account for possible subpath #5177

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
8 changes: 7 additions & 1 deletion server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ func NewRepo(vcsHostType VCSHostType, repoFullName string, cloneURL string, vcsU
// Azure DevOps also does not require .git at the end of clone urls.
if vcsHostType != BitbucketServer && vcsHostType != AzureDevops {
expClonePath := fmt.Sprintf("/%s.git", repoFullName)
if expClonePath != cloneURLParsed.Path {
if vcsHostType == Gitlab {
// For GitLab, we need to check if the path ends with our expected path
// This handles cases where GitLab is hosted at a subpath (e.g., acme.com/gitlab)
if !strings.HasSuffix(cloneURLParsed.Path, expClonePath) {
return Repo{}, fmt.Errorf("expected clone url path to end with %q but had %q", expClonePath, cloneURLParsed.Path)
}
} else if expClonePath != cloneURLParsed.Path {
return Repo{}, fmt.Errorf("expected clone url to have path %q but had %q", expClonePath, cloneURLParsed.Path)
}
}
Expand Down
23 changes: 22 additions & 1 deletion server/events/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ func TestNewRepo_HTTPSAuth(t *testing.T) {
}, repo)
}

func TestNewRepo_GitLabSubpath(t *testing.T) {
// When GitLab is hosted at a subpath, the clone URL validation should still work
repo, err := models.NewRepo(models.Gitlab, "owner/repo", "https://company.com/gitlab/owner/repo.git", "u", "p")
Ok(t, err)
Equals(t, models.Repo{
VCSHost: models.VCSHost{
Hostname: "company.com",
Type: models.Gitlab,
},
SanitizedCloneURL: "https://u:<redacted>@company.com/gitlab/owner/repo.git",
CloneURL: "https://u:[email protected]/gitlab/owner/repo.git",
FullName: "owner/repo",
Owner: "owner",
Name: "repo",
}, repo)

// Should fail if repo name doesn't match
_, err = models.NewRepo(models.Gitlab, "owner/repo", "https://company.com/gitlab/owner/different-repo.git", "u", "p")
ErrEquals(t, `expected clone url path to end with "/owner/repo.git" but had "/gitlab/owner/different-repo.git"`, err)
}

func TestProject_String(t *testing.T) {
Equals(t, "repofullname=owner/repo path=my/path", (models.Project{
RepoFullName: "owner/repo",
Expand Down Expand Up @@ -679,7 +700,7 @@ func TestPlanSuccessStats(t *testing.T) {
{
"with imports",
`Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
plan. Resource actions are indicated with the following symbols:
+ create
~ update in-place
- destroy
Expand Down