From e20e1328f2c80f441a13e314c21686cc108bb27d Mon Sep 17 00:00:00 2001 From: Aayush Gupta <43479002+Aayyush@users.noreply.github.com> Date: Mon, 24 Jan 2022 10:30:18 -0800 Subject: [PATCH] Update GenerateProjectJobURL to account for nested repo names (#2012) --- server/router.go | 3 ++- server/router_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/server/router.go b/server/router.go index 5607294703..3905547d3d 100644 --- a/server/router.go +++ b/server/router.go @@ -3,6 +3,7 @@ package server import ( "fmt" "net/url" + "strings" "github.com/gorilla/mux" "github.com/pkg/errors" @@ -44,7 +45,7 @@ func (r *Router) GenerateProjectJobURL(ctx models.ProjectCommandContext) (string projectIdentifier := models.GetProjectIdentifier(ctx.RepoRelDir, ctx.ProjectName) jobURL, err := r.Underlying.Get(r.ProjectJobsViewRouteName).URL( "org", pull.BaseRepo.Owner, - "repo", pull.BaseRepo.Name, + "repo", strings.ReplaceAll(pull.BaseRepo.Name, "/", "-"), // Account for nested repo names repo/sub-repo "pull", fmt.Sprintf("%d", pull.Num), "project", projectIdentifier, "workspace", ctx.Workspace, diff --git a/server/router_test.go b/server/router_test.go index ccabee44de..8b551ae7ca 100644 --- a/server/router_test.go +++ b/server/router_test.go @@ -115,3 +115,23 @@ func TestGenerateProjectJobURL_ShouldGenerateURLWithDirectoryAndWorkspaceWhenPro Equals(t, expectedURL, gotURL) } + +func TestGenerateProjectJobURL_ShouldGenerateURLWhenNestedRepo(t *testing.T) { + router := setupJobsRouter(t) + ctx := models.ProjectCommandContext{ + Pull: models.PullRequest{ + BaseRepo: models.Repo{ + Owner: "test-owner", + Name: "test-repo/sub-repo", + }, + Num: 1, + }, + RepoRelDir: "ops/terraform/test-root", + Workspace: "default", + } + expectedURL := "http://localhost:4141/jobs/test-owner/test-repo-sub-repo/1/ops-terraform-test-root/default" + gotURL, err := router.GenerateProjectJobURL(ctx) + Ok(t, err) + + Equals(t, expectedURL, gotURL) +}