diff --git a/server/events/models/models.go b/server/events/models/models.go index 66a102fc81..088eb32469 100644 --- a/server/events/models/models.go +++ b/server/events/models/models.go @@ -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) } } diff --git a/server/events/models/models_test.go b/server/events/models/models_test.go index 253aa52584..b251e974f9 100644 --- a/server/events/models/models_test.go +++ b/server/events/models/models_test.go @@ -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:@company.com/gitlab/owner/repo.git", + CloneURL: "https://u:p@company.com/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", @@ -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