git_installation.go 17 KB

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