git_installation.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/permissions -> gitinstallation.NewGithubGetPermissionsHandler
  73. getPermissionsEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbGet,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath + "/permissions",
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.GitInstallationScope,
  85. },
  86. },
  87. )
  88. getPermissionsHandler := gitinstallation.NewGithubGetPermissionsHandler(
  89. config,
  90. factory.GetDecoderValidator(),
  91. factory.GetResultWriter(),
  92. )
  93. routes = append(routes, &Route{
  94. Endpoint: getPermissionsEndpoint,
  95. Handler: getPermissionsHandler,
  96. Router: r,
  97. })
  98. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id} ->
  99. // environment.NewCreateEnvironmentHandler
  100. createEnvironmentEndpoint := factory.NewAPIEndpoint(
  101. &types.APIRequestMetadata{
  102. Verb: types.APIVerbCreate,
  103. Method: types.HTTPVerbPost,
  104. Path: &types.Path{
  105. Parent: basePath,
  106. RelativePath: fmt.Sprintf(
  107. "%s/{%s}/{%s}/clusters/{cluster_id}/environment",
  108. relPath,
  109. types.URLParamGitRepoOwner,
  110. types.URLParamGitRepoName,
  111. ),
  112. },
  113. Scopes: []types.PermissionScope{
  114. types.UserScope,
  115. types.ProjectScope,
  116. types.GitInstallationScope,
  117. types.ClusterScope,
  118. },
  119. },
  120. )
  121. createEnvironmentHandler := environment.NewCreateEnvironmentHandler(
  122. config,
  123. factory.GetDecoderValidator(),
  124. factory.GetResultWriter(),
  125. )
  126. routes = append(routes, &Route{
  127. Endpoint: createEnvironmentEndpoint,
  128. Handler: createEnvironmentHandler,
  129. Router: r,
  130. })
  131. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployment ->
  132. // environment.NewCreateDeploymentHandler
  133. createDeploymentEndpoint := factory.NewAPIEndpoint(
  134. &types.APIRequestMetadata{
  135. Verb: types.APIVerbCreate,
  136. Method: types.HTTPVerbPost,
  137. Path: &types.Path{
  138. Parent: basePath,
  139. RelativePath: fmt.Sprintf(
  140. "%s/{%s}/{%s}/clusters/{cluster_id}/deployment",
  141. relPath,
  142. types.URLParamGitRepoOwner,
  143. types.URLParamGitRepoName,
  144. ),
  145. },
  146. Scopes: []types.PermissionScope{
  147. types.UserScope,
  148. types.ProjectScope,
  149. types.GitInstallationScope,
  150. types.ClusterScope,
  151. },
  152. },
  153. )
  154. createDeploymentHandler := environment.NewCreateDeploymentHandler(
  155. config,
  156. factory.GetDecoderValidator(),
  157. factory.GetResultWriter(),
  158. )
  159. routes = append(routes, &Route{
  160. Endpoint: createDeploymentEndpoint,
  161. Handler: createDeploymentHandler,
  162. Router: r,
  163. })
  164. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployment ->
  165. // environment.NewCreateDeploymentHandler
  166. getDeploymentEndpoint := factory.NewAPIEndpoint(
  167. &types.APIRequestMetadata{
  168. Verb: types.APIVerbGet,
  169. Method: types.HTTPVerbGet,
  170. Path: &types.Path{
  171. Parent: basePath,
  172. RelativePath: fmt.Sprintf(
  173. "%s/{%s}/{%s}/clusters/{cluster_id}/deployment",
  174. relPath,
  175. types.URLParamGitRepoOwner,
  176. types.URLParamGitRepoName,
  177. ),
  178. },
  179. Scopes: []types.PermissionScope{
  180. types.UserScope,
  181. types.ProjectScope,
  182. types.GitInstallationScope,
  183. types.ClusterScope,
  184. },
  185. },
  186. )
  187. getDeploymentHandler := environment.NewGetDeploymentHandler(
  188. config,
  189. factory.GetDecoderValidator(),
  190. factory.GetResultWriter(),
  191. )
  192. routes = append(routes, &Route{
  193. Endpoint: getDeploymentEndpoint,
  194. Handler: getDeploymentHandler,
  195. Router: r,
  196. })
  197. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployments ->
  198. // environment.NewCreateDeploymentHandler
  199. listDeploymentsEndpoint := 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/{%s}/{%s}/clusters/{cluster_id}/deployments",
  207. relPath,
  208. types.URLParamGitRepoOwner,
  209. types.URLParamGitRepoName,
  210. ),
  211. },
  212. Scopes: []types.PermissionScope{
  213. types.UserScope,
  214. types.ProjectScope,
  215. types.GitInstallationScope,
  216. types.ClusterScope,
  217. },
  218. },
  219. )
  220. listDeploymentsHandler := environment.NewListDeploymentsHandler(
  221. config,
  222. factory.GetDecoderValidator(),
  223. factory.GetResultWriter(),
  224. )
  225. routes = append(routes, &Route{
  226. Endpoint: listDeploymentsEndpoint,
  227. Handler: listDeploymentsHandler,
  228. Router: r,
  229. })
  230. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployment/finalize ->
  231. // environment.NewFinalizeDeploymentHandler
  232. finalizeDeploymentEndpoint := factory.NewAPIEndpoint(
  233. &types.APIRequestMetadata{
  234. Verb: types.APIVerbCreate,
  235. Method: types.HTTPVerbPost,
  236. Path: &types.Path{
  237. Parent: basePath,
  238. RelativePath: fmt.Sprintf(
  239. "%s/{%s}/{%s}/clusters/{cluster_id}/deployment/finalize",
  240. relPath,
  241. types.URLParamGitRepoOwner,
  242. types.URLParamGitRepoName,
  243. ),
  244. },
  245. Scopes: []types.PermissionScope{
  246. types.UserScope,
  247. types.ProjectScope,
  248. types.GitInstallationScope,
  249. types.ClusterScope,
  250. },
  251. },
  252. )
  253. finalizeDeploymentHandler := environment.NewFinalizeDeploymentHandler(
  254. config,
  255. factory.GetDecoderValidator(),
  256. factory.GetResultWriter(),
  257. )
  258. routes = append(routes, &Route{
  259. Endpoint: finalizeDeploymentEndpoint,
  260. Handler: finalizeDeploymentHandler,
  261. Router: r,
  262. })
  263. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployment/update ->
  264. // environment.NewFinalizeDeploymentHandler
  265. updateDeploymentEndpoint := factory.NewAPIEndpoint(
  266. &types.APIRequestMetadata{
  267. Verb: types.APIVerbUpdate,
  268. Method: types.HTTPVerbPost,
  269. Path: &types.Path{
  270. Parent: basePath,
  271. RelativePath: fmt.Sprintf(
  272. "%s/{%s}/{%s}/clusters/{cluster_id}/deployment/update",
  273. relPath,
  274. types.URLParamGitRepoOwner,
  275. types.URLParamGitRepoName,
  276. ),
  277. },
  278. Scopes: []types.PermissionScope{
  279. types.UserScope,
  280. types.ProjectScope,
  281. types.GitInstallationScope,
  282. types.ClusterScope,
  283. },
  284. },
  285. )
  286. updateDeploymentHandler := environment.NewUpdateDeploymentHandler(
  287. config,
  288. factory.GetDecoderValidator(),
  289. factory.GetResultWriter(),
  290. )
  291. routes = append(routes, &Route{
  292. Endpoint: updateDeploymentEndpoint,
  293. Handler: updateDeploymentHandler,
  294. Router: r,
  295. })
  296. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployment/update/status ->
  297. // environment.NewUpdateDeploymentStatusHandler
  298. updateDeploymentStatusEndpoint := factory.NewAPIEndpoint(
  299. &types.APIRequestMetadata{
  300. Verb: types.APIVerbUpdate,
  301. Method: types.HTTPVerbPost,
  302. Path: &types.Path{
  303. Parent: basePath,
  304. RelativePath: fmt.Sprintf(
  305. "%s/{%s}/{%s}/clusters/{cluster_id}/deployment/update/status",
  306. relPath,
  307. types.URLParamGitRepoOwner,
  308. types.URLParamGitRepoName,
  309. ),
  310. },
  311. Scopes: []types.PermissionScope{
  312. types.UserScope,
  313. types.ProjectScope,
  314. types.GitInstallationScope,
  315. types.ClusterScope,
  316. },
  317. },
  318. )
  319. updateDeploymentStatusHandler := environment.NewUpdateDeploymentStatusHandler(
  320. config,
  321. factory.GetDecoderValidator(),
  322. factory.GetResultWriter(),
  323. )
  324. routes = append(routes, &Route{
  325. Endpoint: updateDeploymentStatusEndpoint,
  326. Handler: updateDeploymentStatusHandler,
  327. Router: r,
  328. })
  329. // DELETE /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/environment ->
  330. // environment.NewDeleteEnvironmentHandler
  331. deleteEnvironmentEndpoint := factory.NewAPIEndpoint(
  332. &types.APIRequestMetadata{
  333. Verb: types.APIVerbDelete,
  334. Method: types.HTTPVerbDelete,
  335. Path: &types.Path{
  336. Parent: basePath,
  337. RelativePath: fmt.Sprintf(
  338. "%s/{%s}/{%s}/clusters/{cluster_id}/environment",
  339. relPath,
  340. types.URLParamGitRepoOwner,
  341. types.URLParamGitRepoName,
  342. ),
  343. },
  344. Scopes: []types.PermissionScope{
  345. types.UserScope,
  346. types.ProjectScope,
  347. types.GitInstallationScope,
  348. types.ClusterScope,
  349. },
  350. },
  351. )
  352. deleteEnvironmentHandler := environment.NewDeleteEnvironmentHandler(
  353. config,
  354. factory.GetDecoderValidator(),
  355. factory.GetResultWriter(),
  356. )
  357. routes = append(routes, &Route{
  358. Endpoint: deleteEnvironmentEndpoint,
  359. Handler: deleteEnvironmentHandler,
  360. Router: r,
  361. })
  362. // DELETE /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployment ->
  363. // environment.NewDeleteDeploymentHandler
  364. deleteDeploymentEndpoint := factory.NewAPIEndpoint(
  365. &types.APIRequestMetadata{
  366. Verb: types.APIVerbDelete,
  367. Method: types.HTTPVerbDelete,
  368. Path: &types.Path{
  369. Parent: basePath,
  370. RelativePath: fmt.Sprintf(
  371. "%s/{%s}/{%s}/clusters/{cluster_id}/deployment",
  372. relPath,
  373. types.URLParamGitRepoOwner,
  374. types.URLParamGitRepoName,
  375. ),
  376. },
  377. Scopes: []types.PermissionScope{
  378. types.UserScope,
  379. types.ProjectScope,
  380. types.GitInstallationScope,
  381. types.ClusterScope,
  382. },
  383. },
  384. )
  385. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  386. config,
  387. factory.GetDecoderValidator(),
  388. factory.GetResultWriter(),
  389. )
  390. routes = append(routes, &Route{
  391. Endpoint: deleteDeploymentEndpoint,
  392. Handler: deleteDeploymentHandler,
  393. Router: r,
  394. })
  395. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/repos ->
  396. // gitinstallation.GithubListReposHandler
  397. listReposEndpoint := factory.NewAPIEndpoint(
  398. &types.APIRequestMetadata{
  399. Verb: types.APIVerbList,
  400. Method: types.HTTPVerbGet,
  401. Path: &types.Path{
  402. Parent: basePath,
  403. RelativePath: relPath + "/repos",
  404. },
  405. Scopes: []types.PermissionScope{
  406. types.UserScope,
  407. types.ProjectScope,
  408. types.GitInstallationScope,
  409. },
  410. },
  411. )
  412. listReposHandler := gitinstallation.NewGithubListReposHandler(
  413. config,
  414. factory.GetResultWriter(),
  415. )
  416. routes = append(routes, &Route{
  417. Endpoint: listReposEndpoint,
  418. Handler: listReposHandler,
  419. Router: r,
  420. })
  421. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/branches ->
  422. // gitinstallation.GithubListBranchesHandler
  423. listBranchesEndpoint := factory.NewAPIEndpoint(
  424. &types.APIRequestMetadata{
  425. Verb: types.APIVerbList,
  426. Method: types.HTTPVerbGet,
  427. Path: &types.Path{
  428. Parent: basePath,
  429. RelativePath: fmt.Sprintf(
  430. "%s/repos/{%s}/{%s}/{%s}/branches",
  431. relPath,
  432. types.URLParamGitKind,
  433. types.URLParamGitRepoOwner,
  434. types.URLParamGitRepoName,
  435. ),
  436. },
  437. Scopes: []types.PermissionScope{
  438. types.UserScope,
  439. types.ProjectScope,
  440. types.GitInstallationScope,
  441. },
  442. },
  443. )
  444. listBranchesHandler := gitinstallation.NewGithubListBranchesHandler(
  445. config,
  446. factory.GetResultWriter(),
  447. )
  448. routes = append(routes, &Route{
  449. Endpoint: listBranchesEndpoint,
  450. Handler: listBranchesHandler,
  451. Router: r,
  452. })
  453. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/buildpack/detect ->
  454. // gitinstallation.NewGithubGetBuildpackHandler
  455. getBuildpackEndpoint := factory.NewAPIEndpoint(
  456. &types.APIRequestMetadata{
  457. Verb: types.APIVerbGet,
  458. Method: types.HTTPVerbGet,
  459. Path: &types.Path{
  460. Parent: basePath,
  461. RelativePath: fmt.Sprintf(
  462. "%s/repos/{%s}/{%s}/{%s}/{%s}/buildpack/detect",
  463. relPath,
  464. types.URLParamGitKind,
  465. types.URLParamGitRepoOwner,
  466. types.URLParamGitRepoName,
  467. types.URLParamGitBranch,
  468. ),
  469. },
  470. Scopes: []types.PermissionScope{
  471. types.UserScope,
  472. types.ProjectScope,
  473. types.GitInstallationScope,
  474. },
  475. },
  476. )
  477. getBuildpackHandler := gitinstallation.NewGithubGetBuildpackHandler(
  478. config,
  479. factory.GetDecoderValidator(),
  480. factory.GetResultWriter(),
  481. )
  482. routes = append(routes, &Route{
  483. Endpoint: getBuildpackEndpoint,
  484. Handler: getBuildpackHandler,
  485. Router: r,
  486. })
  487. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents ->
  488. // gitinstallation.NewGithubGetContentsHandler
  489. getContentsEndpoint := factory.NewAPIEndpoint(
  490. &types.APIRequestMetadata{
  491. Verb: types.APIVerbGet,
  492. Method: types.HTTPVerbGet,
  493. Path: &types.Path{
  494. Parent: basePath,
  495. RelativePath: fmt.Sprintf(
  496. "%s/repos/{%s}/{%s}/{%s}/{%s}/contents",
  497. relPath,
  498. types.URLParamGitKind,
  499. types.URLParamGitRepoOwner,
  500. types.URLParamGitRepoName,
  501. types.URLParamGitBranch,
  502. ),
  503. },
  504. Scopes: []types.PermissionScope{
  505. types.UserScope,
  506. types.ProjectScope,
  507. types.GitInstallationScope,
  508. },
  509. },
  510. )
  511. getContentsHandler := gitinstallation.NewGithubGetContentsHandler(
  512. config,
  513. factory.GetDecoderValidator(),
  514. factory.GetResultWriter(),
  515. )
  516. routes = append(routes, &Route{
  517. Endpoint: getContentsEndpoint,
  518. Handler: getContentsHandler,
  519. Router: r,
  520. })
  521. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile ->
  522. // gitinstallation.NewGithubGetProcfileHandler
  523. getProcfileEndpoint := factory.NewAPIEndpoint(
  524. &types.APIRequestMetadata{
  525. Verb: types.APIVerbGet,
  526. Method: types.HTTPVerbGet,
  527. Path: &types.Path{
  528. Parent: basePath,
  529. RelativePath: fmt.Sprintf(
  530. "%s/repos/{%s}/{%s}/{%s}/{%s}/procfile",
  531. relPath,
  532. types.URLParamGitKind,
  533. types.URLParamGitRepoOwner,
  534. types.URLParamGitRepoName,
  535. types.URLParamGitBranch,
  536. ),
  537. },
  538. Scopes: []types.PermissionScope{
  539. types.UserScope,
  540. types.ProjectScope,
  541. types.GitInstallationScope,
  542. },
  543. },
  544. )
  545. getProcfileHandler := gitinstallation.NewGithubGetProcfileHandler(
  546. config,
  547. factory.GetDecoderValidator(),
  548. factory.GetResultWriter(),
  549. )
  550. routes = append(routes, &Route{
  551. Endpoint: getProcfileEndpoint,
  552. Handler: getProcfileHandler,
  553. Router: r,
  554. })
  555. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url ->
  556. // gitinstallation.NewGithubGetTarballURLHandler
  557. getTarballURLEndpoint := factory.NewAPIEndpoint(
  558. &types.APIRequestMetadata{
  559. Verb: types.APIVerbGet,
  560. Method: types.HTTPVerbGet,
  561. Path: &types.Path{
  562. Parent: basePath,
  563. RelativePath: fmt.Sprintf(
  564. "%s/repos/{%s}/{%s}/{%s}/{%s}/tarball_url",
  565. relPath,
  566. types.URLParamGitKind,
  567. types.URLParamGitRepoOwner,
  568. types.URLParamGitRepoName,
  569. types.URLParamGitBranch,
  570. ),
  571. },
  572. Scopes: []types.PermissionScope{
  573. types.UserScope,
  574. types.ProjectScope,
  575. types.GitInstallationScope,
  576. },
  577. },
  578. )
  579. getTarballURLHandler := gitinstallation.NewGithubGetTarballURLHandler(
  580. config,
  581. factory.GetDecoderValidator(),
  582. factory.GetResultWriter(),
  583. )
  584. routes = append(routes, &Route{
  585. Endpoint: getTarballURLEndpoint,
  586. Handler: getTarballURLHandler,
  587. Router: r,
  588. })
  589. return routes, newPath
  590. }