git_installation.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/deployment ->
  129. // environment.NewCreateDeploymentHandler
  130. getDeploymentEndpoint := factory.NewAPIEndpoint(
  131. &types.APIRequestMetadata{
  132. Verb: types.APIVerbGet,
  133. Method: types.HTTPVerbGet,
  134. Path: &types.Path{
  135. Parent: basePath,
  136. RelativePath: relPath + "/clusters/{cluster_id}/deployment",
  137. },
  138. Scopes: []types.PermissionScope{
  139. types.UserScope,
  140. types.ProjectScope,
  141. types.GitInstallationScope,
  142. types.ClusterScope,
  143. },
  144. },
  145. )
  146. getDeploymentHandler := environment.NewGetDeploymentHandler(
  147. config,
  148. factory.GetDecoderValidator(),
  149. factory.GetResultWriter(),
  150. )
  151. routes = append(routes, &Route{
  152. Endpoint: getDeploymentEndpoint,
  153. Handler: getDeploymentHandler,
  154. Router: r,
  155. })
  156. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/deployment/finalize ->
  157. // environment.NewFinalizeDeploymentHandler
  158. finalizeDeploymentEndpoint := factory.NewAPIEndpoint(
  159. &types.APIRequestMetadata{
  160. Verb: types.APIVerbCreate,
  161. Method: types.HTTPVerbPost,
  162. Path: &types.Path{
  163. Parent: basePath,
  164. RelativePath: relPath + "/clusters/{cluster_id}/deployment/finalize",
  165. },
  166. Scopes: []types.PermissionScope{
  167. types.UserScope,
  168. types.ProjectScope,
  169. types.GitInstallationScope,
  170. types.ClusterScope,
  171. },
  172. },
  173. )
  174. finalizeDeploymentHandler := environment.NewFinalizeDeploymentHandler(
  175. config,
  176. factory.GetDecoderValidator(),
  177. factory.GetResultWriter(),
  178. )
  179. routes = append(routes, &Route{
  180. Endpoint: finalizeDeploymentEndpoint,
  181. Handler: finalizeDeploymentHandler,
  182. Router: r,
  183. })
  184. // DELETE /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/environment ->
  185. // environment.NewDeleteEnvironmentHandler
  186. deleteEnvironmentEndpoint := factory.NewAPIEndpoint(
  187. &types.APIRequestMetadata{
  188. Verb: types.APIVerbDelete,
  189. Method: types.HTTPVerbDelete,
  190. Path: &types.Path{
  191. Parent: basePath,
  192. RelativePath: relPath + "/clusters/{cluster_id}/environment",
  193. },
  194. Scopes: []types.PermissionScope{
  195. types.UserScope,
  196. types.ProjectScope,
  197. types.GitInstallationScope,
  198. types.ClusterScope,
  199. },
  200. },
  201. )
  202. deleteEnvironmentHandler := environment.NewDeleteEnvironmentHandler(
  203. config,
  204. factory.GetDecoderValidator(),
  205. factory.GetResultWriter(),
  206. )
  207. routes = append(routes, &Route{
  208. Endpoint: deleteEnvironmentEndpoint,
  209. Handler: deleteEnvironmentHandler,
  210. Router: r,
  211. })
  212. // DELETE /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/deployment ->
  213. // environment.NewDeleteDeploymentHandler
  214. deleteDeploymentEndpoint := factory.NewAPIEndpoint(
  215. &types.APIRequestMetadata{
  216. Verb: types.APIVerbDelete,
  217. Method: types.HTTPVerbDelete,
  218. Path: &types.Path{
  219. Parent: basePath,
  220. RelativePath: relPath + "/clusters/{cluster_id}/deployment",
  221. },
  222. Scopes: []types.PermissionScope{
  223. types.UserScope,
  224. types.ProjectScope,
  225. types.GitInstallationScope,
  226. types.ClusterScope,
  227. },
  228. },
  229. )
  230. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  231. config,
  232. factory.GetDecoderValidator(),
  233. factory.GetResultWriter(),
  234. )
  235. routes = append(routes, &Route{
  236. Endpoint: deleteDeploymentEndpoint,
  237. Handler: deleteDeploymentHandler,
  238. Router: r,
  239. })
  240. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/repos ->
  241. // gitinstallation.GithubListReposHandler
  242. listReposEndpoint := factory.NewAPIEndpoint(
  243. &types.APIRequestMetadata{
  244. Verb: types.APIVerbList,
  245. Method: types.HTTPVerbGet,
  246. Path: &types.Path{
  247. Parent: basePath,
  248. RelativePath: relPath + "/repos",
  249. },
  250. Scopes: []types.PermissionScope{
  251. types.UserScope,
  252. types.ProjectScope,
  253. types.GitInstallationScope,
  254. },
  255. },
  256. )
  257. listReposHandler := gitinstallation.NewGithubListReposHandler(
  258. config,
  259. factory.GetResultWriter(),
  260. )
  261. routes = append(routes, &Route{
  262. Endpoint: listReposEndpoint,
  263. Handler: listReposHandler,
  264. Router: r,
  265. })
  266. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/branches ->
  267. // gitinstallation.GithubListBranchesHandler
  268. listBranchesEndpoint := factory.NewAPIEndpoint(
  269. &types.APIRequestMetadata{
  270. Verb: types.APIVerbList,
  271. Method: types.HTTPVerbGet,
  272. Path: &types.Path{
  273. Parent: basePath,
  274. RelativePath: fmt.Sprintf(
  275. "%s/repos/{%s}/{%s}/{%s}/branches",
  276. relPath,
  277. types.URLParamGitKind,
  278. types.URLParamGitRepoOwner,
  279. types.URLParamGitRepoName,
  280. ),
  281. },
  282. Scopes: []types.PermissionScope{
  283. types.UserScope,
  284. types.ProjectScope,
  285. types.GitInstallationScope,
  286. },
  287. },
  288. )
  289. listBranchesHandler := gitinstallation.NewGithubListBranchesHandler(
  290. config,
  291. factory.GetResultWriter(),
  292. )
  293. routes = append(routes, &Route{
  294. Endpoint: listBranchesEndpoint,
  295. Handler: listBranchesHandler,
  296. Router: r,
  297. })
  298. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/buildpack/detect ->
  299. // gitinstallation.NewGithubGetBuildpackHandler
  300. getBuildpackEndpoint := factory.NewAPIEndpoint(
  301. &types.APIRequestMetadata{
  302. Verb: types.APIVerbGet,
  303. Method: types.HTTPVerbGet,
  304. Path: &types.Path{
  305. Parent: basePath,
  306. RelativePath: fmt.Sprintf(
  307. "%s/repos/{%s}/{%s}/{%s}/{%s}/buildpack/detect",
  308. relPath,
  309. types.URLParamGitKind,
  310. types.URLParamGitRepoOwner,
  311. types.URLParamGitRepoName,
  312. types.URLParamGitBranch,
  313. ),
  314. },
  315. Scopes: []types.PermissionScope{
  316. types.UserScope,
  317. types.ProjectScope,
  318. types.GitInstallationScope,
  319. },
  320. },
  321. )
  322. getBuildpackHandler := gitinstallation.NewGithubGetBuildpackHandler(
  323. config,
  324. factory.GetDecoderValidator(),
  325. factory.GetResultWriter(),
  326. )
  327. routes = append(routes, &Route{
  328. Endpoint: getBuildpackEndpoint,
  329. Handler: getBuildpackHandler,
  330. Router: r,
  331. })
  332. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents ->
  333. // gitinstallation.NewGithubGetContentsHandler
  334. getContentsEndpoint := factory.NewAPIEndpoint(
  335. &types.APIRequestMetadata{
  336. Verb: types.APIVerbGet,
  337. Method: types.HTTPVerbGet,
  338. Path: &types.Path{
  339. Parent: basePath,
  340. RelativePath: fmt.Sprintf(
  341. "%s/repos/{%s}/{%s}/{%s}/{%s}/contents",
  342. relPath,
  343. types.URLParamGitKind,
  344. types.URLParamGitRepoOwner,
  345. types.URLParamGitRepoName,
  346. types.URLParamGitBranch,
  347. ),
  348. },
  349. Scopes: []types.PermissionScope{
  350. types.UserScope,
  351. types.ProjectScope,
  352. types.GitInstallationScope,
  353. },
  354. },
  355. )
  356. getContentsHandler := gitinstallation.NewGithubGetContentsHandler(
  357. config,
  358. factory.GetDecoderValidator(),
  359. factory.GetResultWriter(),
  360. )
  361. routes = append(routes, &Route{
  362. Endpoint: getContentsEndpoint,
  363. Handler: getContentsHandler,
  364. Router: r,
  365. })
  366. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile ->
  367. // gitinstallation.NewGithubGetProcfileHandler
  368. getProcfileEndpoint := factory.NewAPIEndpoint(
  369. &types.APIRequestMetadata{
  370. Verb: types.APIVerbGet,
  371. Method: types.HTTPVerbGet,
  372. Path: &types.Path{
  373. Parent: basePath,
  374. RelativePath: fmt.Sprintf(
  375. "%s/repos/{%s}/{%s}/{%s}/{%s}/procfile",
  376. relPath,
  377. types.URLParamGitKind,
  378. types.URLParamGitRepoOwner,
  379. types.URLParamGitRepoName,
  380. types.URLParamGitBranch,
  381. ),
  382. },
  383. Scopes: []types.PermissionScope{
  384. types.UserScope,
  385. types.ProjectScope,
  386. types.GitInstallationScope,
  387. },
  388. },
  389. )
  390. getProcfileHandler := gitinstallation.NewGithubGetProcfileHandler(
  391. config,
  392. factory.GetDecoderValidator(),
  393. factory.GetResultWriter(),
  394. )
  395. routes = append(routes, &Route{
  396. Endpoint: getProcfileEndpoint,
  397. Handler: getProcfileHandler,
  398. Router: r,
  399. })
  400. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url ->
  401. // gitinstallation.NewGithubGetTarballURLHandler
  402. getTarballURLEndpoint := factory.NewAPIEndpoint(
  403. &types.APIRequestMetadata{
  404. Verb: types.APIVerbGet,
  405. Method: types.HTTPVerbGet,
  406. Path: &types.Path{
  407. Parent: basePath,
  408. RelativePath: fmt.Sprintf(
  409. "%s/repos/{%s}/{%s}/{%s}/{%s}/tarball_url",
  410. relPath,
  411. types.URLParamGitKind,
  412. types.URLParamGitRepoOwner,
  413. types.URLParamGitRepoName,
  414. types.URLParamGitBranch,
  415. ),
  416. },
  417. Scopes: []types.PermissionScope{
  418. types.UserScope,
  419. types.ProjectScope,
  420. types.GitInstallationScope,
  421. },
  422. },
  423. )
  424. getTarballURLHandler := gitinstallation.NewGithubGetTarballURLHandler(
  425. config,
  426. factory.GetDecoderValidator(),
  427. factory.GetResultWriter(),
  428. )
  429. routes = append(routes, &Route{
  430. Endpoint: getTarballURLEndpoint,
  431. Handler: getTarballURLHandler,
  432. Router: r,
  433. })
  434. return routes, newPath
  435. }