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 URL generation #2021

Merged
merged 5 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion server/events/pull_closed_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions server/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}