git_installation.go 16 KB

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