git_installation.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/buildpack/detect ->
  130. // gitinstallation.NewGithubGetBuildpackHandler
  131. getBuildpackEndpoint := factory.NewAPIEndpoint(
  132. &types.APIRequestMetadata{
  133. Verb: types.APIVerbGet,
  134. Method: types.HTTPVerbGet,
  135. Path: &types.Path{
  136. Parent: basePath,
  137. RelativePath: fmt.Sprintf(
  138. "%s/repos/{%s}/{%s}/{%s}/{%s}/buildpack/detect",
  139. relPath,
  140. types.URLParamGitKind,
  141. types.URLParamGitRepoOwner,
  142. types.URLParamGitRepoName,
  143. types.URLParamGitBranch,
  144. ),
  145. },
  146. Scopes: []types.PermissionScope{
  147. types.UserScope,
  148. types.ProjectScope,
  149. types.GitInstallationScope,
  150. },
  151. },
  152. )
  153. getBuildpackHandler := gitinstallation.NewGithubGetBuildpackHandler(
  154. config,
  155. factory.GetDecoderValidator(),
  156. factory.GetResultWriter(),
  157. )
  158. routes = append(routes, &Route{
  159. Endpoint: getBuildpackEndpoint,
  160. Handler: getBuildpackHandler,
  161. Router: r,
  162. })
  163. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents ->
  164. // gitinstallation.NewGithubGetContentsHandler
  165. getContentsEndpoint := factory.NewAPIEndpoint(
  166. &types.APIRequestMetadata{
  167. Verb: types.APIVerbGet,
  168. Method: types.HTTPVerbGet,
  169. Path: &types.Path{
  170. Parent: basePath,
  171. RelativePath: fmt.Sprintf(
  172. "%s/repos/{%s}/{%s}/{%s}/{%s}/contents",
  173. relPath,
  174. types.URLParamGitKind,
  175. types.URLParamGitRepoOwner,
  176. types.URLParamGitRepoName,
  177. types.URLParamGitBranch,
  178. ),
  179. },
  180. Scopes: []types.PermissionScope{
  181. types.UserScope,
  182. types.ProjectScope,
  183. types.GitInstallationScope,
  184. },
  185. },
  186. )
  187. getContentsHandler := gitinstallation.NewGithubGetContentsHandler(
  188. config,
  189. factory.GetDecoderValidator(),
  190. factory.GetResultWriter(),
  191. )
  192. routes = append(routes, &Route{
  193. Endpoint: getContentsEndpoint,
  194. Handler: getContentsHandler,
  195. Router: r,
  196. })
  197. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile ->
  198. // gitinstallation.NewGithubGetProcfileHandler
  199. getProcfileEndpoint := factory.NewAPIEndpoint(
  200. &types.APIRequestMetadata{
  201. Verb: types.APIVerbGet,
  202. Method: types.HTTPVerbGet,
  203. Path: &types.Path{
  204. Parent: basePath,
  205. RelativePath: fmt.Sprintf(
  206. "%s/repos/{%s}/{%s}/{%s}/{%s}/procfile",
  207. relPath,
  208. types.URLParamGitKind,
  209. types.URLParamGitRepoOwner,
  210. types.URLParamGitRepoName,
  211. types.URLParamGitBranch,
  212. ),
  213. },
  214. Scopes: []types.PermissionScope{
  215. types.UserScope,
  216. types.ProjectScope,
  217. types.GitInstallationScope,
  218. },
  219. },
  220. )
  221. getProcfileHandler := gitinstallation.NewGithubGetProcfileHandler(
  222. config,
  223. factory.GetDecoderValidator(),
  224. factory.GetResultWriter(),
  225. )
  226. routes = append(routes, &Route{
  227. Endpoint: getProcfileEndpoint,
  228. Handler: getProcfileHandler,
  229. Router: r,
  230. })
  231. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url ->
  232. // gitinstallation.NewGithubGetTarballURLHandler
  233. getTarballURLEndpoint := factory.NewAPIEndpoint(
  234. &types.APIRequestMetadata{
  235. Verb: types.APIVerbGet,
  236. Method: types.HTTPVerbGet,
  237. Path: &types.Path{
  238. Parent: basePath,
  239. RelativePath: fmt.Sprintf(
  240. "%s/repos/{%s}/{%s}/{%s}/{%s}/tarball_url",
  241. relPath,
  242. types.URLParamGitKind,
  243. types.URLParamGitRepoOwner,
  244. types.URLParamGitRepoName,
  245. types.URLParamGitBranch,
  246. ),
  247. },
  248. Scopes: []types.PermissionScope{
  249. types.UserScope,
  250. types.ProjectScope,
  251. types.GitInstallationScope,
  252. },
  253. },
  254. )
  255. getTarballURLHandler := gitinstallation.NewGithubGetTarballURLHandler(
  256. config,
  257. factory.GetDecoderValidator(),
  258. factory.GetResultWriter(),
  259. )
  260. routes = append(routes, &Route{
  261. Endpoint: getTarballURLEndpoint,
  262. Handler: getTarballURLHandler,
  263. Router: r,
  264. })
  265. return routes, newPath
  266. }