git_installation.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. )
  10. func NewGitInstallationScopedRegisterer(children ...*Registerer) *Registerer {
  11. return &Registerer{
  12. GetRoutes: GetGitInstallationScopedRoutes,
  13. Children: children,
  14. }
  15. }
  16. func GetGitInstallationScopedRoutes(
  17. r chi.Router,
  18. config *config.Config,
  19. basePath *types.Path,
  20. factory shared.APIEndpointFactory,
  21. children ...*Registerer,
  22. ) []*Route {
  23. routes, projPath := getGitInstallationRoutes(r, config, basePath, factory)
  24. if len(children) > 0 {
  25. r.Route(projPath.RelativePath, func(r chi.Router) {
  26. for _, child := range children {
  27. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  28. routes = append(routes, childRoutes...)
  29. }
  30. })
  31. }
  32. return routes
  33. }
  34. func getGitInstallationRoutes(
  35. r chi.Router,
  36. config *config.Config,
  37. basePath *types.Path,
  38. factory shared.APIEndpointFactory,
  39. ) ([]*Route, *types.Path) {
  40. relPath := "/gitrepos/{git_installation_id}"
  41. newPath := &types.Path{
  42. Parent: basePath,
  43. RelativePath: relPath,
  44. }
  45. routes := make([]*Route, 0)
  46. // GET /api/projects/{project_id}/gitrepos/{git_installation_id} -> gitinstallation.NewGitInstallationGetHandler
  47. getEndpoint := factory.NewAPIEndpoint(
  48. &types.APIRequestMetadata{
  49. Verb: types.APIVerbGet,
  50. Method: types.HTTPVerbGet,
  51. Path: &types.Path{
  52. Parent: basePath,
  53. RelativePath: relPath,
  54. },
  55. Scopes: []types.PermissionScope{
  56. types.UserScope,
  57. types.ProjectScope,
  58. types.GitInstallationScope,
  59. },
  60. },
  61. )
  62. getHandler := gitinstallation.NewGitInstallationGetHandler(
  63. config,
  64. factory.GetResultWriter(),
  65. )
  66. routes = append(routes, &Route{
  67. Endpoint: getEndpoint,
  68. Handler: getHandler,
  69. Router: r,
  70. })
  71. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/repos ->
  72. // gitinstallation.GithubListReposHandler
  73. listReposEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbList,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath + "/repos",
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.GitInstallationScope,
  85. },
  86. },
  87. )
  88. listReposHandler := gitinstallation.NewGithubListReposHandler(
  89. config,
  90. factory.GetResultWriter(),
  91. )
  92. routes = append(routes, &Route{
  93. Endpoint: listReposEndpoint,
  94. Handler: listReposHandler,
  95. Router: r,
  96. })
  97. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/branches ->
  98. // gitinstallation.GithubListBranchesHandler
  99. listBranchesEndpoint := factory.NewAPIEndpoint(
  100. &types.APIRequestMetadata{
  101. Verb: types.APIVerbList,
  102. Method: types.HTTPVerbGet,
  103. Path: &types.Path{
  104. Parent: basePath,
  105. RelativePath: fmt.Sprintf(
  106. "%s/repos/%s/%s/%s/branches",
  107. relPath,
  108. types.URLParamGitKind,
  109. types.URLParamGitRepoOwner,
  110. types.URLParamGitRepoName,
  111. ),
  112. },
  113. Scopes: []types.PermissionScope{
  114. types.UserScope,
  115. types.ProjectScope,
  116. types.GitInstallationScope,
  117. },
  118. },
  119. )
  120. listBranchesHandler := gitinstallation.NewGithubListBranchesHandler(
  121. config,
  122. factory.GetResultWriter(),
  123. )
  124. routes = append(routes, &Route{
  125. Endpoint: listBranchesEndpoint,
  126. Handler: listBranchesHandler,
  127. Router: r,
  128. })
  129. return routes, newPath
  130. }