From 5899802767cef5b226171d16415241d155630c98 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Wed, 8 Jan 2025 20:39:45 +0530 Subject: [PATCH] Resolve status update issue for GitLab instances with relative paths Issue: For GitLab instances hosted under a relative path (e.g., https://example.servehttp.com/gitlab), status updates on MR fail to propagate correctly. Although initial events like Repository CR creation, starting PipelineRuns based on webhooks from GitLab, reporting status on Pipeline start works fine. subsequent updates (e.g., marking the PipelineRun as Finished) do not reflect in GitLab, leaving the status stuck on Running state. Root Cause: When we have /gitlab relative path in the API we are getting the wrong organization info because there is a function which get Organization and Repository info again from URL so for a GitLab API https://example.servehttp.com/gitlab/root/testpac initially Organization and Repository value from GitLab coming as root and testpac But later in the event.go file we are refetching Organization and Repository from event.URL because of that Organization is coming as gitlab/root and Repository as testpac. that's why getting project info api call ex: (https://example.servehttp.com/gitlab/api/v4/projects/gitlab/root/testpac) is failing with 404 error With this PR changes now status is getting updated successfully. Signed-off-by: savitaashture --- pkg/cli/webhook/github_test.go | 6 +++--- pkg/formatting/vcs.go | 3 +-- pkg/formatting/vcs_test.go | 2 +- pkg/provider/gitlab/gitlab_test.go | 2 ++ pkg/reconciler/event.go | 6 +++--- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pkg/cli/webhook/github_test.go b/pkg/cli/webhook/github_test.go index 2485be6e0..655b4776d 100644 --- a/pkg/cli/webhook/github_test.go +++ b/pkg/cli/webhook/github_test.go @@ -52,13 +52,13 @@ func TestAskGHWebhookConfig(t *testing.T) { wantErrStr: "", }, { - name: "with defaults and a slash", + name: "with defaults and no slash", askStubs: func(as *prompt.AskStubber) { as.StubOne(true) as.StubOne("webhook-secret") as.StubOne("token") }, - repoURL: "https://github.com/pac/demo/", + repoURL: "https://github.com/pac/demo", controllerURL: "https://test", wantErrStr: "", }, @@ -69,7 +69,7 @@ func TestAskGHWebhookConfig(t *testing.T) { as.StubOne("webhook-secret") as.StubOne("token") }, - repoURL: "https://github.com/pac/demo/", + repoURL: "https://github.com/pac/demo", controllerURL: "https://test", personalaccesstoken: "Yzg5NzhlYmNkNTQwNzYzN2E2ZGExYzhkMTc4NjU0MjY3ZmQ2NmMeZg==", wantErrStr: "", diff --git a/pkg/formatting/vcs.go b/pkg/formatting/vcs.go index d3beadfc0..713f8c1c4 100644 --- a/pkg/formatting/vcs.go +++ b/pkg/formatting/vcs.go @@ -3,7 +3,6 @@ package formatting import ( "fmt" "net/url" - "path/filepath" "strings" "golang.org/x/text/cases" @@ -51,7 +50,7 @@ func GetRepoOwnerSplitted(u string) (string, string, error) { if len(parts) < 3 { return "", "", fmt.Errorf("invalid repo url at least a organization/project and a repo needs to be specified: %s", u) } - org := filepath.Join(parts[0 : len(parts)-1]...) + org := parts[len(parts)-2] repo := parts[len(parts)-1] return org, repo, nil } diff --git a/pkg/formatting/vcs_test.go b/pkg/formatting/vcs_test.go index 3f118f0b3..49692faa8 100644 --- a/pkg/formatting/vcs_test.go +++ b/pkg/formatting/vcs_test.go @@ -50,7 +50,7 @@ func TestGetRepoOwnerSplitted(t *testing.T) { { name: "good/parse url gitlab subpath", url: "https://forge/foo/bar/owner/repo", - retOrg: "foo/bar/owner", + retOrg: "owner", retRepo: "repo", }, { diff --git a/pkg/provider/gitlab/gitlab_test.go b/pkg/provider/gitlab/gitlab_test.go index 383bd30b8..0335d5a1c 100644 --- a/pkg/provider/gitlab/gitlab_test.go +++ b/pkg/provider/gitlab/gitlab_test.go @@ -254,6 +254,8 @@ func TestSetClientDetectAPIURL(t *testing.T) { assert.ErrorContains(t, err, "no git_provider.secret has been set") event.Provider.Token = "hello" + event.TargetProjectID = 10 + event.SourceProjectID = 10 v.repoURL, event.URL, event.Provider.URL = "", "", "" event.URL = fmt.Sprintf("%s/hello-this-is-me-ze/project", fakehost) diff --git a/pkg/reconciler/event.go b/pkg/reconciler/event.go index cf69c816e..7df63bc49 100644 --- a/pkg/reconciler/event.go +++ b/pkg/reconciler/event.go @@ -62,9 +62,9 @@ func buildEventFromPipelineRun(pr *tektonv1.PipelineRun) *info.Event { event.URL = prAnno[keys.RepoURL] // it's safer to get repo, org from repo.url since we have to remove the / and other chars in labels which drops // the SubPath that gitlab is using. - repo, org, _ := formatting.GetRepoOwnerSplitted(event.URL) - event.Organization = repo - event.Repository = org + org, repo, _ := formatting.GetRepoOwnerSplitted(event.URL) + event.Organization = org + event.Repository = repo event.EventType = prAnno[keys.EventType] event.TriggerTarget = triggertype.StringToType(prAnno[keys.EventType]) event.BaseBranch = prAnno[keys.Branch]