git_installation.go 13 KB

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