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. // DELETE /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployment ->
  364. // environment.NewDeleteDeploymentHandler
  365. deleteDeploymentEndpoint := factory.NewAPIEndpoint(
  366. &types.APIRequestMetadata{
  367. Verb: types.APIVerbDelete,
  368. Method: types.HTTPVerbDelete,
  369. Path: &types.Path{
  370. Parent: basePath,
  371. RelativePath: fmt.Sprintf(
  372. "%s/{%s}/{%s}/clusters/{cluster_id}/deployment",
  373. relPath,
  374. types.URLParamGitRepoOwner,
  375. types.URLParamGitRepoName,
  376. ),
  377. },
  378. Scopes: []types.PermissionScope{
  379. types.UserScope,
  380. types.ProjectScope,
  381. types.GitInstallationScope,
  382. types.ClusterScope,
  383. },
  384. },
  385. )
  386. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  387. config,
  388. factory.GetDecoderValidator(),
  389. factory.GetResultWriter(),
  390. )
  391. routes = append(routes, &Route{
  392. Endpoint: deleteDeploymentEndpoint,
  393. Handler: deleteDeploymentHandler,
  394. Router: r,
  395. })
  396. }
  397. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/repos ->
  398. // gitinstallation.GithubListReposHandler
  399. listReposEndpoint := factory.NewAPIEndpoint(
  400. &types.APIRequestMetadata{
  401. Verb: types.APIVerbList,
  402. Method: types.HTTPVerbGet,
  403. Path: &types.Path{
  404. Parent: basePath,
  405. RelativePath: relPath + "/repos",
  406. },
  407. Scopes: []types.PermissionScope{
  408. types.UserScope,
  409. types.ProjectScope,
  410. types.GitInstallationScope,
  411. },
  412. },
  413. )
  414. listReposHandler := gitinstallation.NewGithubListReposHandler(
  415. config,
  416. factory.GetResultWriter(),
  417. )
  418. routes = append(routes, &Route{
  419. Endpoint: listReposEndpoint,
  420. Handler: listReposHandler,
  421. Router: r,
  422. })
  423. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/branches ->
  424. // gitinstallation.GithubListBranchesHandler
  425. listBranchesEndpoint := factory.NewAPIEndpoint(
  426. &types.APIRequestMetadata{
  427. Verb: types.APIVerbList,
  428. Method: types.HTTPVerbGet,
  429. Path: &types.Path{
  430. Parent: basePath,
  431. RelativePath: fmt.Sprintf(
  432. "%s/repos/{%s}/{%s}/{%s}/branches",
  433. relPath,
  434. types.URLParamGitKind,
  435. types.URLParamGitRepoOwner,
  436. types.URLParamGitRepoName,
  437. ),
  438. },
  439. Scopes: []types.PermissionScope{
  440. types.UserScope,
  441. types.ProjectScope,
  442. types.GitInstallationScope,
  443. },
  444. },
  445. )
  446. listBranchesHandler := gitinstallation.NewGithubListBranchesHandler(
  447. config,
  448. factory.GetResultWriter(),
  449. )
  450. routes = append(routes, &Route{
  451. Endpoint: listBranchesEndpoint,
  452. Handler: listBranchesHandler,
  453. Router: r,
  454. })
  455. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/buildpack/detect ->
  456. // gitinstallation.NewGithubGetBuildpackHandler
  457. getBuildpackEndpoint := factory.NewAPIEndpoint(
  458. &types.APIRequestMetadata{
  459. Verb: types.APIVerbGet,
  460. Method: types.HTTPVerbGet,
  461. Path: &types.Path{
  462. Parent: basePath,
  463. RelativePath: fmt.Sprintf(
  464. "%s/repos/{%s}/{%s}/{%s}/{%s}/buildpack/detect",
  465. relPath,
  466. types.URLParamGitKind,
  467. types.URLParamGitRepoOwner,
  468. types.URLParamGitRepoName,
  469. types.URLParamGitBranch,
  470. ),
  471. },
  472. Scopes: []types.PermissionScope{
  473. types.UserScope,
  474. types.ProjectScope,
  475. types.GitInstallationScope,
  476. },
  477. },
  478. )
  479. getBuildpackHandler := gitinstallation.NewGithubGetBuildpackHandler(
  480. config,
  481. factory.GetDecoderValidator(),
  482. factory.GetResultWriter(),
  483. )
  484. routes = append(routes, &Route{
  485. Endpoint: getBuildpackEndpoint,
  486. Handler: getBuildpackHandler,
  487. Router: r,
  488. })
  489. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents ->
  490. // gitinstallation.NewGithubGetContentsHandler
  491. getContentsEndpoint := factory.NewAPIEndpoint(
  492. &types.APIRequestMetadata{
  493. Verb: types.APIVerbGet,
  494. Method: types.HTTPVerbGet,
  495. Path: &types.Path{
  496. Parent: basePath,
  497. RelativePath: fmt.Sprintf(
  498. "%s/repos/{%s}/{%s}/{%s}/{%s}/contents",
  499. relPath,
  500. types.URLParamGitKind,
  501. types.URLParamGitRepoOwner,
  502. types.URLParamGitRepoName,
  503. types.URLParamGitBranch,
  504. ),
  505. },
  506. Scopes: []types.PermissionScope{
  507. types.UserScope,
  508. types.ProjectScope,
  509. types.GitInstallationScope,
  510. },
  511. },
  512. )
  513. getContentsHandler := gitinstallation.NewGithubGetContentsHandler(
  514. config,
  515. factory.GetDecoderValidator(),
  516. factory.GetResultWriter(),
  517. )
  518. routes = append(routes, &Route{
  519. Endpoint: getContentsEndpoint,
  520. Handler: getContentsHandler,
  521. Router: r,
  522. })
  523. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile ->
  524. // gitinstallation.NewGithubGetProcfileHandler
  525. getProcfileEndpoint := factory.NewAPIEndpoint(
  526. &types.APIRequestMetadata{
  527. Verb: types.APIVerbGet,
  528. Method: types.HTTPVerbGet,
  529. Path: &types.Path{
  530. Parent: basePath,
  531. RelativePath: fmt.Sprintf(
  532. "%s/repos/{%s}/{%s}/{%s}/{%s}/procfile",
  533. relPath,
  534. types.URLParamGitKind,
  535. types.URLParamGitRepoOwner,
  536. types.URLParamGitRepoName,
  537. types.URLParamGitBranch,
  538. ),
  539. },
  540. Scopes: []types.PermissionScope{
  541. types.UserScope,
  542. types.ProjectScope,
  543. types.GitInstallationScope,
  544. },
  545. },
  546. )
  547. getProcfileHandler := gitinstallation.NewGithubGetProcfileHandler(
  548. config,
  549. factory.GetDecoderValidator(),
  550. factory.GetResultWriter(),
  551. )
  552. routes = append(routes, &Route{
  553. Endpoint: getProcfileEndpoint,
  554. Handler: getProcfileHandler,
  555. Router: r,
  556. })
  557. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url ->
  558. // gitinstallation.NewGithubGetTarballURLHandler
  559. getTarballURLEndpoint := factory.NewAPIEndpoint(
  560. &types.APIRequestMetadata{
  561. Verb: types.APIVerbGet,
  562. Method: types.HTTPVerbGet,
  563. Path: &types.Path{
  564. Parent: basePath,
  565. RelativePath: fmt.Sprintf(
  566. "%s/repos/{%s}/{%s}/{%s}/{%s}/tarball_url",
  567. relPath,
  568. types.URLParamGitKind,
  569. types.URLParamGitRepoOwner,
  570. types.URLParamGitRepoName,
  571. types.URLParamGitBranch,
  572. ),
  573. },
  574. Scopes: []types.PermissionScope{
  575. types.UserScope,
  576. types.ProjectScope,
  577. types.GitInstallationScope,
  578. },
  579. },
  580. )
  581. getTarballURLHandler := gitinstallation.NewGithubGetTarballURLHandler(
  582. config,
  583. factory.GetDecoderValidator(),
  584. factory.GetResultWriter(),
  585. )
  586. routes = append(routes, &Route{
  587. Endpoint: getTarballURLEndpoint,
  588. Handler: getTarballURLHandler,
  589. Router: r,
  590. })
  591. return routes, newPath
  592. }