git_installation.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/types"
  7. )
  8. func NewGitInstallationScopedRegisterer(children ...*Registerer) *Registerer {
  9. return &Registerer{
  10. GetRoutes: GetGitInstallationScopedRoutes,
  11. Children: children,
  12. }
  13. }
  14. func GetGitInstallationScopedRoutes(
  15. r chi.Router,
  16. config *shared.Config,
  17. basePath *types.Path,
  18. factory shared.APIEndpointFactory,
  19. children ...*Registerer,
  20. ) []*Route {
  21. routes, projPath := getGitInstallationRoutes(r, config, basePath, factory)
  22. if len(children) > 0 {
  23. r.Route(projPath.RelativePath, func(r chi.Router) {
  24. for _, child := range children {
  25. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  26. routes = append(routes, childRoutes...)
  27. }
  28. })
  29. }
  30. return routes
  31. }
  32. func getGitInstallationRoutes(
  33. r chi.Router,
  34. config *shared.Config,
  35. basePath *types.Path,
  36. factory shared.APIEndpointFactory,
  37. ) ([]*Route, *types.Path) {
  38. relPath := "/gitrepos/{git_installation_id}"
  39. newPath := &types.Path{
  40. Parent: basePath,
  41. RelativePath: relPath,
  42. }
  43. routes := make([]*Route, 0)
  44. // GET /api/projects/{project_id}/gitrepos/{git_installation_id} -> gitinstallation.NewGitInstallationGetHandler
  45. getEndpoint := factory.NewAPIEndpoint(
  46. &types.APIRequestMetadata{
  47. Verb: types.APIVerbGet,
  48. Method: types.HTTPVerbGet,
  49. Path: &types.Path{
  50. Parent: basePath,
  51. RelativePath: relPath,
  52. },
  53. Scopes: []types.PermissionScope{
  54. types.UserScope,
  55. types.ProjectScope,
  56. types.GitInstallationScope,
  57. },
  58. },
  59. )
  60. getHandler := gitinstallation.NewGitInstallationGetHandler(
  61. config,
  62. factory.GetResultWriter(),
  63. )
  64. routes = append(routes, &Route{
  65. Endpoint: getEndpoint,
  66. Handler: getHandler,
  67. Router: r,
  68. })
  69. return routes, newPath
  70. }