Skip to content

Commit

Permalink
Fix URL generation (runatlantis#2021)
Browse files Browse the repository at this point in the history
* fix org generation

* fix key generation

* fix variable name

* add normalization vars

* encapsulate vars

Co-authored-by: Roman Pertsev <[email protected]>
  • Loading branch information
2 people authored and krrrr38 committed Dec 16, 2022
1 parent 68a7868 commit a8bdbfe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
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)
}

0 comments on commit a8bdbfe

Please sign in to comment.