git_installation.go 16 KB

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