git_installation.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. // DELETE /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/environment ->
  271. // environment.NewDeleteEnvironmentHandler
  272. deleteEnvironmentEndpoint := factory.NewAPIEndpoint(
  273. &types.APIRequestMetadata{
  274. Verb: types.APIVerbDelete,
  275. Method: types.HTTPVerbDelete,
  276. Path: &types.Path{
  277. Parent: basePath,
  278. RelativePath: fmt.Sprintf(
  279. "%s/{%s}/{%s}/clusters/{cluster_id}/environment",
  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. deleteEnvironmentHandler := environment.NewDeleteEnvironmentHandler(
  294. config,
  295. factory.GetDecoderValidator(),
  296. factory.GetResultWriter(),
  297. )
  298. routes = append(routes, &Route{
  299. Endpoint: deleteEnvironmentEndpoint,
  300. Handler: deleteEnvironmentHandler,
  301. Router: r,
  302. })
  303. // DELETE /api/projects/{project_id}/gitrepos/{git_installation_id}/{owner}/{name}/clusters/{cluster_id}/deployment ->
  304. // environment.NewDeleteDeploymentHandler
  305. deleteDeploymentEndpoint := 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}/deployment",
  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. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  327. config,
  328. factory.GetDecoderValidator(),
  329. factory.GetResultWriter(),
  330. )
  331. routes = append(routes, &Route{
  332. Endpoint: deleteDeploymentEndpoint,
  333. Handler: deleteDeploymentHandler,
  334. Router: r,
  335. })
  336. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/repos ->
  337. // gitinstallation.GithubListReposHandler
  338. listReposEndpoint := factory.NewAPIEndpoint(
  339. &types.APIRequestMetadata{
  340. Verb: types.APIVerbList,
  341. Method: types.HTTPVerbGet,
  342. Path: &types.Path{
  343. Parent: basePath,
  344. RelativePath: relPath + "/repos",
  345. },
  346. Scopes: []types.PermissionScope{
  347. types.UserScope,
  348. types.ProjectScope,
  349. types.GitInstallationScope,
  350. },
  351. },
  352. )
  353. listReposHandler := gitinstallation.NewGithubListReposHandler(
  354. config,
  355. factory.GetResultWriter(),
  356. )
  357. routes = append(routes, &Route{
  358. Endpoint: listReposEndpoint,
  359. Handler: listReposHandler,
  360. Router: r,
  361. })
  362. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/branches ->
  363. // gitinstallation.GithubListBranchesHandler
  364. listBranchesEndpoint := factory.NewAPIEndpoint(
  365. &types.APIRequestMetadata{
  366. Verb: types.APIVerbList,
  367. Method: types.HTTPVerbGet,
  368. Path: &types.Path{
  369. Parent: basePath,
  370. RelativePath: fmt.Sprintf(
  371. "%s/repos/{%s}/{%s}/{%s}/branches",
  372. relPath,
  373. types.URLParamGitKind,
  374. types.URLParamGitRepoOwner,
  375. types.URLParamGitRepoName,
  376. ),
  377. },
  378. Scopes: []types.PermissionScope{
  379. types.UserScope,
  380. types.ProjectScope,
  381. types.GitInstallationScope,
  382. },
  383. },
  384. )
  385. listBranchesHandler := gitinstallation.NewGithubListBranchesHandler(
  386. config,
  387. factory.GetResultWriter(),
  388. )
  389. routes = append(routes, &Route{
  390. Endpoint: listBranchesEndpoint,
  391. Handler: listBranchesHandler,
  392. Router: r,
  393. })
  394. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/buildpack/detect ->
  395. // gitinstallation.NewGithubGetBuildpackHandler
  396. getBuildpackEndpoint := factory.NewAPIEndpoint(
  397. &types.APIRequestMetadata{
  398. Verb: types.APIVerbGet,
  399. Method: types.HTTPVerbGet,
  400. Path: &types.Path{
  401. Parent: basePath,
  402. RelativePath: fmt.Sprintf(
  403. "%s/repos/{%s}/{%s}/{%s}/{%s}/buildpack/detect",
  404. relPath,
  405. types.URLParamGitKind,
  406. types.URLParamGitRepoOwner,
  407. types.URLParamGitRepoName,
  408. types.URLParamGitBranch,
  409. ),
  410. },
  411. Scopes: []types.PermissionScope{
  412. types.UserScope,
  413. types.ProjectScope,
  414. types.GitInstallationScope,
  415. },
  416. },
  417. )
  418. getBuildpackHandler := gitinstallation.NewGithubGetBuildpackHandler(
  419. config,
  420. factory.GetDecoderValidator(),
  421. factory.GetResultWriter(),
  422. )
  423. routes = append(routes, &Route{
  424. Endpoint: getBuildpackEndpoint,
  425. Handler: getBuildpackHandler,
  426. Router: r,
  427. })
  428. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents ->
  429. // gitinstallation.NewGithubGetContentsHandler
  430. getContentsEndpoint := factory.NewAPIEndpoint(
  431. &types.APIRequestMetadata{
  432. Verb: types.APIVerbGet,
  433. Method: types.HTTPVerbGet,
  434. Path: &types.Path{
  435. Parent: basePath,
  436. RelativePath: fmt.Sprintf(
  437. "%s/repos/{%s}/{%s}/{%s}/{%s}/contents",
  438. relPath,
  439. types.URLParamGitKind,
  440. types.URLParamGitRepoOwner,
  441. types.URLParamGitRepoName,
  442. types.URLParamGitBranch,
  443. ),
  444. },
  445. Scopes: []types.PermissionScope{
  446. types.UserScope,
  447. types.ProjectScope,
  448. types.GitInstallationScope,
  449. },
  450. },
  451. )
  452. getContentsHandler := gitinstallation.NewGithubGetContentsHandler(
  453. config,
  454. factory.GetDecoderValidator(),
  455. factory.GetResultWriter(),
  456. )
  457. routes = append(routes, &Route{
  458. Endpoint: getContentsEndpoint,
  459. Handler: getContentsHandler,
  460. Router: r,
  461. })
  462. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile ->
  463. // gitinstallation.NewGithubGetProcfileHandler
  464. getProcfileEndpoint := factory.NewAPIEndpoint(
  465. &types.APIRequestMetadata{
  466. Verb: types.APIVerbGet,
  467. Method: types.HTTPVerbGet,
  468. Path: &types.Path{
  469. Parent: basePath,
  470. RelativePath: fmt.Sprintf(
  471. "%s/repos/{%s}/{%s}/{%s}/{%s}/procfile",
  472. relPath,
  473. types.URLParamGitKind,
  474. types.URLParamGitRepoOwner,
  475. types.URLParamGitRepoName,
  476. types.URLParamGitBranch,
  477. ),
  478. },
  479. Scopes: []types.PermissionScope{
  480. types.UserScope,
  481. types.ProjectScope,
  482. types.GitInstallationScope,
  483. },
  484. },
  485. )
  486. getProcfileHandler := gitinstallation.NewGithubGetProcfileHandler(
  487. config,
  488. factory.GetDecoderValidator(),
  489. factory.GetResultWriter(),
  490. )
  491. routes = append(routes, &Route{
  492. Endpoint: getProcfileEndpoint,
  493. Handler: getProcfileHandler,
  494. Router: r,
  495. })
  496. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url ->
  497. // gitinstallation.NewGithubGetTarballURLHandler
  498. getTarballURLEndpoint := factory.NewAPIEndpoint(
  499. &types.APIRequestMetadata{
  500. Verb: types.APIVerbGet,
  501. Method: types.HTTPVerbGet,
  502. Path: &types.Path{
  503. Parent: basePath,
  504. RelativePath: fmt.Sprintf(
  505. "%s/repos/{%s}/{%s}/{%s}/{%s}/tarball_url",
  506. relPath,
  507. types.URLParamGitKind,
  508. types.URLParamGitRepoOwner,
  509. types.URLParamGitRepoName,
  510. types.URLParamGitBranch,
  511. ),
  512. },
  513. Scopes: []types.PermissionScope{
  514. types.UserScope,
  515. types.ProjectScope,
  516. types.GitInstallationScope,
  517. },
  518. },
  519. )
  520. getTarballURLHandler := gitinstallation.NewGithubGetTarballURLHandler(
  521. config,
  522. factory.GetDecoderValidator(),
  523. factory.GetResultWriter(),
  524. )
  525. routes = append(routes, &Route{
  526. Endpoint: getTarballURLEndpoint,
  527. Handler: getTarballURLHandler,
  528. Router: r,
  529. })
  530. return routes, newPath
  531. }