git_installation.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/environment"
  6. "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/types"
  10. )
  11. func NewGitInstallationScopedRegisterer(children ...*Registerer) *Registerer {
  12. return &Registerer{
  13. GetRoutes: GetGitInstallationScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetGitInstallationScopedRoutes(
  18. r chi.Router,
  19. config *config.Config,
  20. basePath *types.Path,
  21. factory shared.APIEndpointFactory,
  22. children ...*Registerer,
  23. ) []*Route {
  24. routes, projPath := getGitInstallationRoutes(r, config, basePath, factory)
  25. if len(children) > 0 {
  26. r.Route(projPath.RelativePath, func(r chi.Router) {
  27. for _, child := range children {
  28. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  29. routes = append(routes, childRoutes...)
  30. }
  31. })
  32. }
  33. return routes
  34. }
  35. func getGitInstallationRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*Route, *types.Path) {
  41. relPath := "/gitrepos/{git_installation_id}"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. routes := make([]*Route, 0)
  47. // GET /api/projects/{project_id}/gitrepos/{git_installation_id} -> gitinstallation.NewGitInstallationGetHandler
  48. getEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbGet,
  51. Method: types.HTTPVerbGet,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: relPath,
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. types.GitInstallationScope,
  60. },
  61. },
  62. )
  63. getHandler := gitinstallation.NewGitInstallationGetHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &Route{
  68. Endpoint: getEndpoint,
  69. Handler: getHandler,
  70. Router: r,
  71. })
  72. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id} ->
  73. // environment.NewCreateEnvironmentHandler
  74. createEnvironmentEndpoint := factory.NewAPIEndpoint(
  75. &types.APIRequestMetadata{
  76. Verb: types.APIVerbCreate,
  77. Method: types.HTTPVerbPost,
  78. Path: &types.Path{
  79. Parent: basePath,
  80. RelativePath: relPath + "/clusters/{cluster_id}/environment",
  81. },
  82. Scopes: []types.PermissionScope{
  83. types.UserScope,
  84. types.ProjectScope,
  85. types.GitInstallationScope,
  86. types.ClusterScope,
  87. },
  88. },
  89. )
  90. createEnvironmentHandler := environment.NewCreateEnvironmentHandler(
  91. config,
  92. factory.GetDecoderValidator(),
  93. factory.GetResultWriter(),
  94. )
  95. routes = append(routes, &Route{
  96. Endpoint: createEnvironmentEndpoint,
  97. Handler: createEnvironmentHandler,
  98. Router: r,
  99. })
  100. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/deployment ->
  101. // environment.NewCreateDeploymentHandler
  102. createDeploymentEndpoint := factory.NewAPIEndpoint(
  103. &types.APIRequestMetadata{
  104. Verb: types.APIVerbCreate,
  105. Method: types.HTTPVerbPost,
  106. Path: &types.Path{
  107. Parent: basePath,
  108. RelativePath: relPath + "/clusters/{cluster_id}/deployment",
  109. },
  110. Scopes: []types.PermissionScope{
  111. types.UserScope,
  112. types.ProjectScope,
  113. types.GitInstallationScope,
  114. types.ClusterScope,
  115. },
  116. },
  117. )
  118. createDeploymentHandler := environment.NewCreateDeploymentHandler(
  119. config,
  120. factory.GetDecoderValidator(),
  121. factory.GetResultWriter(),
  122. )
  123. routes = append(routes, &Route{
  124. Endpoint: createDeploymentEndpoint,
  125. Handler: createDeploymentHandler,
  126. Router: r,
  127. })
  128. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/deployment/finalize ->
  129. // environment.NewFinalizeDeploymentHandler
  130. finalizeDeploymentEndpoint := factory.NewAPIEndpoint(
  131. &types.APIRequestMetadata{
  132. Verb: types.APIVerbCreate,
  133. Method: types.HTTPVerbPost,
  134. Path: &types.Path{
  135. Parent: basePath,
  136. RelativePath: relPath + "/clusters/{cluster_id}/deployment/finalize",
  137. },
  138. Scopes: []types.PermissionScope{
  139. types.UserScope,
  140. types.ProjectScope,
  141. types.GitInstallationScope,
  142. types.ClusterScope,
  143. },
  144. },
  145. )
  146. finalizeDeploymentHandler := environment.NewFinalizeDeploymentHandler(
  147. config,
  148. factory.GetDecoderValidator(),
  149. factory.GetResultWriter(),
  150. )
  151. routes = append(routes, &Route{
  152. Endpoint: finalizeDeploymentEndpoint,
  153. Handler: finalizeDeploymentHandler,
  154. Router: r,
  155. })
  156. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/repos ->
  157. // gitinstallation.GithubListReposHandler
  158. listReposEndpoint := factory.NewAPIEndpoint(
  159. &types.APIRequestMetadata{
  160. Verb: types.APIVerbList,
  161. Method: types.HTTPVerbGet,
  162. Path: &types.Path{
  163. Parent: basePath,
  164. RelativePath: relPath + "/repos",
  165. },
  166. Scopes: []types.PermissionScope{
  167. types.UserScope,
  168. types.ProjectScope,
  169. types.GitInstallationScope,
  170. },
  171. },
  172. )
  173. listReposHandler := gitinstallation.NewGithubListReposHandler(
  174. config,
  175. factory.GetResultWriter(),
  176. )
  177. routes = append(routes, &Route{
  178. Endpoint: listReposEndpoint,
  179. Handler: listReposHandler,
  180. Router: r,
  181. })
  182. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/branches ->
  183. // gitinstallation.GithubListBranchesHandler
  184. listBranchesEndpoint := factory.NewAPIEndpoint(
  185. &types.APIRequestMetadata{
  186. Verb: types.APIVerbList,
  187. Method: types.HTTPVerbGet,
  188. Path: &types.Path{
  189. Parent: basePath,
  190. RelativePath: fmt.Sprintf(
  191. "%s/repos/{%s}/{%s}/{%s}/branches",
  192. relPath,
  193. types.URLParamGitKind,
  194. types.URLParamGitRepoOwner,
  195. types.URLParamGitRepoName,
  196. ),
  197. },
  198. Scopes: []types.PermissionScope{
  199. types.UserScope,
  200. types.ProjectScope,
  201. types.GitInstallationScope,
  202. },
  203. },
  204. )
  205. listBranchesHandler := gitinstallation.NewGithubListBranchesHandler(
  206. config,
  207. factory.GetResultWriter(),
  208. )
  209. routes = append(routes, &Route{
  210. Endpoint: listBranchesEndpoint,
  211. Handler: listBranchesHandler,
  212. Router: r,
  213. })
  214. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/buildpack/detect ->
  215. // gitinstallation.NewGithubGetBuildpackHandler
  216. getBuildpackEndpoint := factory.NewAPIEndpoint(
  217. &types.APIRequestMetadata{
  218. Verb: types.APIVerbGet,
  219. Method: types.HTTPVerbGet,
  220. Path: &types.Path{
  221. Parent: basePath,
  222. RelativePath: fmt.Sprintf(
  223. "%s/repos/{%s}/{%s}/{%s}/{%s}/buildpack/detect",
  224. relPath,
  225. types.URLParamGitKind,
  226. types.URLParamGitRepoOwner,
  227. types.URLParamGitRepoName,
  228. types.URLParamGitBranch,
  229. ),
  230. },
  231. Scopes: []types.PermissionScope{
  232. types.UserScope,
  233. types.ProjectScope,
  234. types.GitInstallationScope,
  235. },
  236. },
  237. )
  238. getBuildpackHandler := gitinstallation.NewGithubGetBuildpackHandler(
  239. config,
  240. factory.GetDecoderValidator(),
  241. factory.GetResultWriter(),
  242. )
  243. routes = append(routes, &Route{
  244. Endpoint: getBuildpackEndpoint,
  245. Handler: getBuildpackHandler,
  246. Router: r,
  247. })
  248. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents ->
  249. // gitinstallation.NewGithubGetContentsHandler
  250. getContentsEndpoint := factory.NewAPIEndpoint(
  251. &types.APIRequestMetadata{
  252. Verb: types.APIVerbGet,
  253. Method: types.HTTPVerbGet,
  254. Path: &types.Path{
  255. Parent: basePath,
  256. RelativePath: fmt.Sprintf(
  257. "%s/repos/{%s}/{%s}/{%s}/{%s}/contents",
  258. relPath,
  259. types.URLParamGitKind,
  260. types.URLParamGitRepoOwner,
  261. types.URLParamGitRepoName,
  262. types.URLParamGitBranch,
  263. ),
  264. },
  265. Scopes: []types.PermissionScope{
  266. types.UserScope,
  267. types.ProjectScope,
  268. types.GitInstallationScope,
  269. },
  270. },
  271. )
  272. getContentsHandler := gitinstallation.NewGithubGetContentsHandler(
  273. config,
  274. factory.GetDecoderValidator(),
  275. factory.GetResultWriter(),
  276. )
  277. routes = append(routes, &Route{
  278. Endpoint: getContentsEndpoint,
  279. Handler: getContentsHandler,
  280. Router: r,
  281. })
  282. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile ->
  283. // gitinstallation.NewGithubGetProcfileHandler
  284. getProcfileEndpoint := factory.NewAPIEndpoint(
  285. &types.APIRequestMetadata{
  286. Verb: types.APIVerbGet,
  287. Method: types.HTTPVerbGet,
  288. Path: &types.Path{
  289. Parent: basePath,
  290. RelativePath: fmt.Sprintf(
  291. "%s/repos/{%s}/{%s}/{%s}/{%s}/procfile",
  292. relPath,
  293. types.URLParamGitKind,
  294. types.URLParamGitRepoOwner,
  295. types.URLParamGitRepoName,
  296. types.URLParamGitBranch,
  297. ),
  298. },
  299. Scopes: []types.PermissionScope{
  300. types.UserScope,
  301. types.ProjectScope,
  302. types.GitInstallationScope,
  303. },
  304. },
  305. )
  306. getProcfileHandler := gitinstallation.NewGithubGetProcfileHandler(
  307. config,
  308. factory.GetDecoderValidator(),
  309. factory.GetResultWriter(),
  310. )
  311. routes = append(routes, &Route{
  312. Endpoint: getProcfileEndpoint,
  313. Handler: getProcfileHandler,
  314. Router: r,
  315. })
  316. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url ->
  317. // gitinstallation.NewGithubGetTarballURLHandler
  318. getTarballURLEndpoint := factory.NewAPIEndpoint(
  319. &types.APIRequestMetadata{
  320. Verb: types.APIVerbGet,
  321. Method: types.HTTPVerbGet,
  322. Path: &types.Path{
  323. Parent: basePath,
  324. RelativePath: fmt.Sprintf(
  325. "%s/repos/{%s}/{%s}/{%s}/{%s}/tarball_url",
  326. relPath,
  327. types.URLParamGitKind,
  328. types.URLParamGitRepoOwner,
  329. types.URLParamGitRepoName,
  330. types.URLParamGitBranch,
  331. ),
  332. },
  333. Scopes: []types.PermissionScope{
  334. types.UserScope,
  335. types.ProjectScope,
  336. types.GitInstallationScope,
  337. },
  338. },
  339. )
  340. getTarballURLHandler := gitinstallation.NewGithubGetTarballURLHandler(
  341. config,
  342. factory.GetDecoderValidator(),
  343. factory.GetResultWriter(),
  344. )
  345. routes = append(routes, &Route{
  346. Endpoint: getTarballURLEndpoint,
  347. Handler: getTarballURLHandler,
  348. Router: r,
  349. })
  350. return routes, newPath
  351. }