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

Support Log Streaming URLs for project without a project name #140

Merged
merged 4 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 30 additions & 11 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"fmt"
"net/url"
"strings"

"github.com/gorilla/mux"
"github.com/pkg/errors"
Expand All @@ -18,8 +19,11 @@ type Router struct {
// LockViewRouteName is the named route for the lock view that can be Get'd
// from the Underlying router.
LockViewRouteName string
// ProjectJobsViewRouteName is the named route for the projects active jobs
ProjectJobsViewRouteName string
// ProjectBasedJobsViewRouteName is the named route for the projects active jobs
ProjectBasedJobsViewRouteName string
// DirWorkspaceBasedJobsViewRouteName is the named route for the projects active jobs
// which does not specify the project name. Repo Relative Dir and Workspace is used.
DirWorkspaceBasedJobsViewRouteName string
// LockViewRouteIDQueryParam is the query parameter needed to construct the
// lock view: underlying.Get(LockViewRouteName).URL(LockViewRouteIDQueryParam, "my id").
LockViewRouteIDQueryParam string
Expand All @@ -42,15 +46,30 @@ func (r *Router) GenerateLockURL(lockID string) string {
func (r *Router) GenerateProjectJobURL(ctx models.ProjectCommandContext) (string, error) {
pull := ctx.Pull

jobURL, err := r.Underlying.Get(r.ProjectJobsViewRouteName).URL(
"org", pull.BaseRepo.Owner,
"repo", pull.BaseRepo.Name,
"pull", fmt.Sprintf("%d", pull.Num),
"project", ctx.ProjectName,
)

if err != nil {
return "", errors.Wrapf(err, "creating job url for %s/%d/%s", pull.BaseRepo.FullName, pull.Num, ctx.ProjectName)
var jobURL *url.URL
var err error
if ctx.ProjectName != "" {
jobURL, err = r.Underlying.Get(r.ProjectBasedJobsViewRouteName).URL(
"org", pull.BaseRepo.Owner,
"repo", pull.BaseRepo.Name,
"pull", fmt.Sprintf("%d", pull.Num),
"project", ctx.ProjectName,
)
if err != nil {
return "", errors.Wrapf(err, "creating job url for %s/%d/%s", pull.BaseRepo.FullName, pull.Num, ctx.ProjectName)
}
} else {
directory := strings.ReplaceAll(ctx.RepoRelDir, "/", "-")
jobURL, err = r.Underlying.Get(r.DirWorkspaceBasedJobsViewRouteName).URL(
"org", pull.BaseRepo.Owner,
"repo", pull.BaseRepo.Name,
"pull", fmt.Sprintf("%d", pull.Num),
"directory", directory,
"workspace", ctx.Workspace,
)
if err != nil {
return "", errors.Wrapf(err, "creating job url for %s/%d/%s/%s", pull.BaseRepo.FullName, pull.Num, directory, ctx.Workspace)
}
}

return r.AtlantisURL.String() + jobURL.String(), nil
Expand Down
62 changes: 62 additions & 0 deletions server/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gorilla/mux"
"github.com/runatlantis/atlantis/server"
"github.com/runatlantis/atlantis/server/events/models"
. "github.com/runatlantis/atlantis/testing"
)

Expand Down Expand Up @@ -60,3 +61,64 @@ func TestRouter_GenerateLockURL(t *testing.T) {
})
}
}

func TestGenerateProjectJobURL_ShouldGenerateURLWithProjectNameWhenProjectNameSpecified(t *testing.T) {

atlantisURL, err := server.ParseAtlantisURL("http://localhost:4141")
Ok(t, err)

underlyingRouter := mux.NewRouter()
underlyingRouter.HandleFunc("/jobs/{org}/{repo}/{pull}/{project}", func(_ http.ResponseWriter, _ *http.Request) {}).Methods("GET").Name("project-jobs-detail")

router := server.Router{
AtlantisURL: atlantisURL,
Underlying: underlyingRouter,
ProjectBasedJobsViewRouteName: "project-jobs-detail",
}

ctx := models.ProjectCommandContext{
Pull: models.PullRequest{
BaseRepo: models.Repo{
Owner: "test-owner",
Name: "test-repo",
},
Num: 1,
},
ProjectName: "test-project",
}
expectedURL := "http://localhost:4141/jobs/test-owner/test-repo/1/test-project"
gotURL, err := router.GenerateProjectJobURL(ctx)
Ok(t, err)

Equals(t, expectedURL, gotURL)
}

func TestGenerateProjectJobURL_ShouldGenerateURLWithDirectoryAndWorkspaceWhenProjectNameNotSpecified(t *testing.T) {
atlantisURL, err := server.ParseAtlantisURL("http://localhost:4141")
Ok(t, err)

underlyingRouter := mux.NewRouter()
underlyingRouter.HandleFunc("/jobs/{org}/{repo}/{pull}/{directory}/{workspace}", func(_ http.ResponseWriter, _ *http.Request) {}).Methods("GET").Name("directory-workspace-jobs-detail")

router := server.Router{
AtlantisURL: atlantisURL,
Underlying: underlyingRouter,
DirWorkspaceBasedJobsViewRouteName: "directory-workspace-jobs-detail",
}
ctx := models.ProjectCommandContext{
Pull: models.PullRequest{
BaseRepo: models.Repo{
Owner: "test-owner",
Name: "test-repo",
},
Num: 1,
},
RepoRelDir: "ops/terraform/test-root",
Workspace: "test-workspace",
}
expectedURL := "http://localhost:4141/jobs/test-owner/test-repo/1/ops-terraform-test-root/test-workspace"
gotURL, err := router.GenerateProjectJobURL(ctx)
Ok(t, err)

Equals(t, expectedURL, gotURL)
}
25 changes: 16 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ const (
// route. ex:
// mux.Router.Get(LockViewRouteName).URL(LockViewRouteIDQueryParam, "my id")
LockViewRouteIDQueryParam = "id"
// ProjectJobsViewRouteName is the named route in mux.Router for the log stream view.
// Can be retrieved by mux.Router.Get(ProjectJobsViewRouteName)
ProjectJobsViewRouteName = "project-jobs-detail"
// ProjectBasedJobsViewRouteName is the named route in mux.Router for projects with a project name in
// the log stream view. Can be retrieved by mux.Router.Get(ProjectBasedJobsViewRouteName)
ProjectBasedJobsViewRouteName = "project-jobs-detail"
// DirWorkspaceBasedJobsViewRouteName is the named route in mux.Router for for projects without a
// project name in the log stream view. The relative directory and the workspace is used instead.
DirWorkspaceBasedJobsViewRouteName = "directory-workspace-jobs-detail"
// binDirName is the name of the directory inside our data dir where
// we download binaries.
BinDirName = "bin"
Expand Down Expand Up @@ -316,11 +319,12 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {

underlyingRouter := mux.NewRouter()
router := &Router{
AtlantisURL: parsedURL,
LockViewRouteIDQueryParam: LockViewRouteIDQueryParam,
LockViewRouteName: LockViewRouteName,
ProjectJobsViewRouteName: ProjectJobsViewRouteName,
Underlying: underlyingRouter,
AtlantisURL: parsedURL,
LockViewRouteIDQueryParam: LockViewRouteIDQueryParam,
LockViewRouteName: LockViewRouteName,
ProjectBasedJobsViewRouteName: ProjectBasedJobsViewRouteName,
DirWorkspaceBasedJobsViewRouteName: DirWorkspaceBasedJobsViewRouteName,
Underlying: underlyingRouter,
}

projectCmdOutput := make(chan *models.ProjectCmdOutputLine)
Expand Down Expand Up @@ -830,8 +834,11 @@ func (s *Server) Start() error {
s.Router.HandleFunc("/locks", s.LocksController.DeleteLock).Methods("DELETE").Queries("id", "{id:.*}")
s.Router.HandleFunc("/lock", s.LocksController.GetLock).Methods("GET").
Queries(LockViewRouteIDQueryParam, fmt.Sprintf("{%s}", LockViewRouteIDQueryParam)).Name(LockViewRouteName)
s.Router.HandleFunc("/jobs/{org}/{repo}/{pull}/{project}", s.JobsController.GetProjectJobs).Methods("GET").Name(ProjectJobsViewRouteName)
s.Router.HandleFunc("/jobs/{org}/{repo}/{pull}/{project}", s.JobsController.GetProjectJobs).Methods("GET").Name(ProjectBasedJobsViewRouteName)
s.Router.HandleFunc("/jobs/{org}/{repo}/{pull}/{project}/ws", s.JobsController.GetProjectJobsWS).Methods("GET")
s.Router.HandleFunc("/jobs/{org}/{repo}/{pull}/{directory}/{workspace}", s.JobsController.GetProjectJobs).Methods("GET").Name(DirWorkspaceBasedJobsViewRouteName)
s.Router.HandleFunc("/jobs/{org}/{repo}/{pull}/{directory}/{workspace}/ws", s.JobsController.GetProjectJobsWS).Methods("GET")

n := negroni.New(&negroni.Recovery{
Logger: log.New(os.Stdout, "", log.LstdFlags),
PrintStack: false,
Expand Down