-
Notifications
You must be signed in to change notification settings - Fork 6
/
router.go
57 lines (51 loc) · 2.27 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package server
import (
"fmt"
"net/url"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
)
// Router can be used to retrieve Atlantis URLs. It acts as an intermediary
// between the underlying router and the rest of Atlantis that might need to
// construct URLs to different resources.
type Router struct {
// Underlying is the router that the routes have been constructed on.
Underlying *mux.Router
// 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
// LockViewRouteIDQueryParam is the query parameter needed to construct the
// lock view: underlying.Get(LockViewRouteName).URL(LockViewRouteIDQueryParam, "my id").
LockViewRouteIDQueryParam string
// AtlantisURL is the fully qualified URL that Atlantis is
// accessible from externally.
AtlantisURL *url.URL
}
// GenerateLockURL returns a fully qualified URL to view the lock at lockID.
func (r *Router) GenerateLockURL(lockID string) string {
lockURL, _ := r.Underlying.Get(r.LockViewRouteName).URL(r.LockViewRouteIDQueryParam, url.QueryEscape(lockID))
// At this point, lockURL will just be a path because r.Underlying isn't
// configured with host or scheme information. So to generate the fully
// qualified LockURL we just append the router's url to our base url.
// We're not doing anything fancy here with the actual url object because
// golang likes to double escape the lockURL path when using url.Parse().
return r.AtlantisURL.String() + lockURL.String()
}
func (r *Router) GenerateProjectJobURL(ctx models.ProjectCommandContext) (string, error) {
pull := ctx.Pull
projectIdentifier := models.GetProjectIdentifier(ctx.RepoRelDir, ctx.ProjectName)
jobURL, err := r.Underlying.Get(r.ProjectJobsViewRouteName).URL(
"org", pull.BaseRepo.Owner,
"repo", pull.BaseRepo.Name,
"pull", fmt.Sprintf("%d", pull.Num),
"project", projectIdentifier,
"workspace", ctx.Workspace,
)
if err != nil {
return "", errors.Wrapf(err, "creating job url for %s/%d/%s/%s", pull.BaseRepo.FullName, pull.Num, projectIdentifier, ctx.Workspace)
}
return r.AtlantisURL.String() + jobURL.String(), nil
}