From a8bdbfe595d75ebb39a8c273669923a561e34272 Mon Sep 17 00:00:00 2001 From: Roman Pertsev Date: Mon, 31 Jan 2022 22:15:16 +0300 Subject: [PATCH] Fix URL generation (#2021) * fix org generation * fix key generation * fix variable name * add normalization vars * encapsulate vars Co-authored-by: Roman Pertsev --- server/events/models/models.go | 6 +++++- server/events/pull_closed_executor.go | 7 ++++++- server/router.go | 2 +- server/router_test.go | 20 ++++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/server/events/models/models.go b/server/events/models/models.go index 63ef9b723d..1afac77e00 100644 --- a/server/events/models/models.go +++ b/server/events/models/models.go @@ -425,7 +425,11 @@ func (p ProjectCommandContext) GetShowResultFileName() string { // Gets a unique identifier for the current pull request as a single string func (p ProjectCommandContext) PullInfo() string { - return BuildPullInfo(p.BaseRepo.FullName, p.Pull.Num, p.ProjectName, p.RepoRelDir, p.Workspace) + normalizedOwner := strings.ReplaceAll(p.BaseRepo.Owner, "/", "-") + normalizedName := strings.ReplaceAll(p.BaseRepo.Name, "/", "-") + projectRepo := fmt.Sprintf("%s/%s", normalizedOwner, normalizedName) + + return BuildPullInfo(projectRepo, p.Pull.Num, p.ProjectName, p.RepoRelDir, p.Workspace) } func BuildPullInfo(repoName string, pullNum int, projectName string, relDir string, workspace string) string { diff --git a/server/events/pull_closed_executor.go b/server/events/pull_closed_executor.go index e20fb4b50d..4b78751c0d 100644 --- a/server/events/pull_closed_executor.go +++ b/server/events/pull_closed_executor.go @@ -83,7 +83,12 @@ func (p *PullClosedExecutor) CleanUpPull(repo models.Repo, pull models.PullReque if pullStatus != nil { for _, project := range pullStatus.Projects { - projectKey := models.BuildPullInfo(pullStatus.Pull.BaseRepo.FullName, pull.Num, project.ProjectName, project.RepoRelDir, project.Workspace) + normalizedOwner := strings.ReplaceAll(pullStatus.Pull.BaseRepo.Owner, "/", "-") + normalizedName := strings.ReplaceAll(pullStatus.Pull.BaseRepo.Name, "/", "-") + projectRepo := fmt.Sprintf("%s/%s", normalizedOwner, normalizedName) + + projectKey := models.BuildPullInfo(projectRepo, pull.Num, project.ProjectName, project.RepoRelDir, project.Workspace) + p.LogStreamResourceCleaner.CleanUp(projectKey) } } diff --git a/server/router.go b/server/router.go index 3905547d3d..33f4f2f4a9 100644 --- a/server/router.go +++ b/server/router.go @@ -44,7 +44,7 @@ func (r *Router) GenerateProjectJobURL(ctx models.ProjectCommandContext) (string pull := ctx.Pull projectIdentifier := models.GetProjectIdentifier(ctx.RepoRelDir, ctx.ProjectName) jobURL, err := r.Underlying.Get(r.ProjectJobsViewRouteName).URL( - "org", pull.BaseRepo.Owner, + "org", strings.ReplaceAll(pull.BaseRepo.Owner, "/", "-"), "repo", strings.ReplaceAll(pull.BaseRepo.Name, "/", "-"), // Account for nested repo names repo/sub-repo "pull", fmt.Sprintf("%d", pull.Num), "project", projectIdentifier, diff --git a/server/router_test.go b/server/router_test.go index 5ebe2367f1..48bce1c8e9 100644 --- a/server/router_test.go +++ b/server/router_test.go @@ -155,3 +155,23 @@ func TestGenerateProjectJobURL_ShouldGenerateURLWhenNestedRepo(t *testing.T) { Equals(t, expectedURL, gotURL) } + +func TestGenerateProjectJobURL_ShouldGenerateURLWhenNestedOwner(t *testing.T) { + router := setupJobsRouter(t) + ctx := models.ProjectCommandContext{ + Pull: models.PullRequest{ + BaseRepo: models.Repo{ + Owner: "test-owner/sub-proj", + Name: "test-repo/sub-repo", + }, + Num: 1, + }, + RepoRelDir: "ops/terraform/test-root", + Workspace: "default", + } + expectedURL := "http://localhost:4141/jobs/test-owner-sub-proj/test-repo-sub-repo/1/ops-terraform-test-root/default" + gotURL, err := router.GenerateProjectJobURL(ctx) + Ok(t, err) + + Equals(t, expectedURL, gotURL) +}