project_integration.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. project_integration "github.com/porter-dev/porter/api/server/handlers/project_integration"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/server/shared/router"
  9. "github.com/porter-dev/porter/api/types"
  10. )
  11. func NewProjectIntegrationScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  12. return &router.Registerer{
  13. GetRoutes: GetProjectIntegrationScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetProjectIntegrationScopedRoutes(
  18. r chi.Router,
  19. config *config.Config,
  20. basePath *types.Path,
  21. factory shared.APIEndpointFactory,
  22. children ...*router.Registerer,
  23. ) []*router.Route {
  24. routes, projPath := getProjectIntegrationRoutes(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 getProjectIntegrationRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*router.Route, *types.Path) {
  41. relPath := "/integrations"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. routes := make([]*router.Route, 0)
  47. // GET /api/projects/{project_id}/integrations/oauth -> project_integration.NewListOAuthHandler
  48. listOAuthEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbGet,
  51. Method: types.HTTPVerbGet,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: relPath + "/oauth",
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. },
  60. },
  61. )
  62. listOAuthHandler := project_integration.NewListOAuthHandler(
  63. config,
  64. factory.GetResultWriter(),
  65. )
  66. routes = append(routes, &router.Route{
  67. Endpoint: listOAuthEndpoint,
  68. Handler: listOAuthHandler,
  69. Router: r,
  70. })
  71. // GET /api/projects/{project_id}/integrations/do -> project_integration.NewListDOHandler
  72. listDOEndpoint := factory.NewAPIEndpoint(
  73. &types.APIRequestMetadata{
  74. Verb: types.APIVerbGet,
  75. Method: types.HTTPVerbGet,
  76. Path: &types.Path{
  77. Parent: basePath,
  78. RelativePath: relPath + "/do",
  79. },
  80. Scopes: []types.PermissionScope{
  81. types.UserScope,
  82. types.ProjectScope,
  83. },
  84. },
  85. )
  86. listDOHandler := project_integration.NewListDOHandler(
  87. config,
  88. factory.GetResultWriter(),
  89. )
  90. routes = append(routes, &router.Route{
  91. Endpoint: listDOEndpoint,
  92. Handler: listDOHandler,
  93. Router: r,
  94. })
  95. // POST /api/projects/{project_id}/integrations/basic -> project_integration.NewCreateBasicHandler
  96. createBasicEndpoint := factory.NewAPIEndpoint(
  97. &types.APIRequestMetadata{
  98. Verb: types.APIVerbCreate,
  99. Method: types.HTTPVerbPost,
  100. Path: &types.Path{
  101. Parent: basePath,
  102. RelativePath: relPath + "/basic",
  103. },
  104. Scopes: []types.PermissionScope{
  105. types.UserScope,
  106. types.ProjectScope,
  107. },
  108. },
  109. )
  110. createBasicHandler := project_integration.NewCreateBasicHandler(
  111. config,
  112. factory.GetDecoderValidator(),
  113. factory.GetResultWriter(),
  114. )
  115. routes = append(routes, &router.Route{
  116. Endpoint: createBasicEndpoint,
  117. Handler: createBasicHandler,
  118. Router: r,
  119. })
  120. // POST /api/projects/{project_id}/integrations/aws -> project_integration.NewCreateAWSHandler
  121. createAWSEndpoint := factory.NewAPIEndpoint(
  122. &types.APIRequestMetadata{
  123. Verb: types.APIVerbCreate,
  124. Method: types.HTTPVerbPost,
  125. Path: &types.Path{
  126. Parent: basePath,
  127. RelativePath: relPath + "/aws",
  128. },
  129. Scopes: []types.PermissionScope{
  130. types.UserScope,
  131. types.ProjectScope,
  132. },
  133. },
  134. )
  135. createAWSHandler := project_integration.NewCreateAWSHandler(
  136. config,
  137. factory.GetDecoderValidator(),
  138. factory.GetResultWriter(),
  139. )
  140. routes = append(routes, &router.Route{
  141. Endpoint: createAWSEndpoint,
  142. Handler: createAWSHandler,
  143. Router: r,
  144. })
  145. // GET /api/projects/{project_id}/integrations/aws -> project_integration.NewListAWSHandler
  146. listAWSEndpoint := factory.NewAPIEndpoint(
  147. &types.APIRequestMetadata{
  148. Verb: types.APIVerbGet,
  149. Method: types.HTTPVerbGet,
  150. Path: &types.Path{
  151. Parent: basePath,
  152. RelativePath: relPath + "/aws",
  153. },
  154. Scopes: []types.PermissionScope{
  155. types.UserScope,
  156. types.ProjectScope,
  157. },
  158. },
  159. )
  160. listAWSHandler := project_integration.NewListAWSHandler(
  161. config,
  162. factory.GetResultWriter(),
  163. )
  164. routes = append(routes, &router.Route{
  165. Endpoint: listAWSEndpoint,
  166. Handler: listAWSHandler,
  167. Router: r,
  168. })
  169. // POST /api/projects/{project_id}/integrations/aws/overwrite -> project_integration.NewOverwriteAWSHandler
  170. overwriteAWSEndpoint := factory.NewAPIEndpoint(
  171. &types.APIRequestMetadata{
  172. Verb: types.APIVerbCreate,
  173. Method: types.HTTPVerbPost,
  174. Path: &types.Path{
  175. Parent: basePath,
  176. RelativePath: relPath + "/aws/overwrite",
  177. },
  178. Scopes: []types.PermissionScope{
  179. types.UserScope,
  180. types.ProjectScope,
  181. },
  182. },
  183. )
  184. overwriteAWSHandler := project_integration.NewOverwriteAWSHandler(
  185. config,
  186. factory.GetDecoderValidator(),
  187. factory.GetResultWriter(),
  188. )
  189. routes = append(routes, &router.Route{
  190. Endpoint: overwriteAWSEndpoint,
  191. Handler: overwriteAWSHandler,
  192. Router: r,
  193. })
  194. // POST /api/projects/{project_id}/integrations/preflightcheck -> project_integration.NewCreatePreflightCheckHandler
  195. preflightCheckEndpoint := factory.NewAPIEndpoint(
  196. &types.APIRequestMetadata{
  197. Verb: types.APIVerbCreate,
  198. Method: types.HTTPVerbPost,
  199. Path: &types.Path{
  200. Parent: basePath,
  201. RelativePath: relPath + "/preflightcheck",
  202. },
  203. Scopes: []types.PermissionScope{
  204. types.UserScope,
  205. types.ProjectScope,
  206. },
  207. },
  208. )
  209. preflightCheckHandler := project_integration.NewCreatePreflightCheckHandler(
  210. config,
  211. factory.GetDecoderValidator(),
  212. factory.GetResultWriter(),
  213. )
  214. routes = append(routes, &router.Route{
  215. Endpoint: preflightCheckEndpoint,
  216. Handler: preflightCheckHandler,
  217. Router: r,
  218. })
  219. // GET /api/projects/{project_id}/integrations/azure -> project_integration.NewListAzureHandler
  220. listAzureEndpoint := factory.NewAPIEndpoint(
  221. &types.APIRequestMetadata{
  222. Verb: types.APIVerbGet,
  223. Method: types.HTTPVerbGet,
  224. Path: &types.Path{
  225. Parent: basePath,
  226. RelativePath: relPath + "/azure",
  227. },
  228. Scopes: []types.PermissionScope{
  229. types.UserScope,
  230. types.ProjectScope,
  231. },
  232. },
  233. )
  234. listAzureHandler := project_integration.NewListAzureHandler(
  235. config,
  236. factory.GetResultWriter(),
  237. )
  238. routes = append(routes, &router.Route{
  239. Endpoint: listAzureEndpoint,
  240. Handler: listAzureHandler,
  241. Router: r,
  242. })
  243. // POST /api/projects/{project_id}/integrations/gcp -> project_integration.NewCreateGCPHandler
  244. createGCPEndpoint := factory.NewAPIEndpoint(
  245. &types.APIRequestMetadata{
  246. Verb: types.APIVerbCreate,
  247. Method: types.HTTPVerbPost,
  248. Path: &types.Path{
  249. Parent: basePath,
  250. RelativePath: relPath + "/gcp",
  251. },
  252. Scopes: []types.PermissionScope{
  253. types.UserScope,
  254. types.ProjectScope,
  255. },
  256. },
  257. )
  258. createGCPHandler := project_integration.NewCreateGCPHandler(
  259. config,
  260. factory.GetDecoderValidator(),
  261. factory.GetResultWriter(),
  262. )
  263. routes = append(routes, &router.Route{
  264. Endpoint: createGCPEndpoint,
  265. Handler: createGCPHandler,
  266. Router: r,
  267. })
  268. // GET /api/projects/{project_id}/integrations/gcp -> project_integration.NewListGCPHandler
  269. listGCPEndpoint := factory.NewAPIEndpoint(
  270. &types.APIRequestMetadata{
  271. Verb: types.APIVerbGet,
  272. Method: types.HTTPVerbGet,
  273. Path: &types.Path{
  274. Parent: basePath,
  275. RelativePath: relPath + "/gcp",
  276. },
  277. Scopes: []types.PermissionScope{
  278. types.UserScope,
  279. types.ProjectScope,
  280. },
  281. },
  282. )
  283. listGCPHandler := project_integration.NewListGCPHandler(
  284. config,
  285. factory.GetResultWriter(),
  286. )
  287. routes = append(routes, &router.Route{
  288. Endpoint: listGCPEndpoint,
  289. Handler: listGCPHandler,
  290. Router: r,
  291. })
  292. // POST /api/projects/{project_id}/integrations/azure -> project_integration.NewCreateAzureHandler
  293. createAzureEndpoint := factory.NewAPIEndpoint(
  294. &types.APIRequestMetadata{
  295. Verb: types.APIVerbCreate,
  296. Method: types.HTTPVerbPost,
  297. Path: &types.Path{
  298. Parent: basePath,
  299. RelativePath: relPath + "/azure",
  300. },
  301. Scopes: []types.PermissionScope{
  302. types.UserScope,
  303. types.ProjectScope,
  304. },
  305. },
  306. )
  307. createAzureHandler := project_integration.NewCreateAzureHandler(
  308. config,
  309. factory.GetDecoderValidator(),
  310. factory.GetResultWriter(),
  311. )
  312. routes = append(routes, &router.Route{
  313. Endpoint: createAzureEndpoint,
  314. Handler: createAzureHandler,
  315. Router: r,
  316. })
  317. // GET /api/projects/{project_id}/integrations/gitlab
  318. listGitlabEndpoint := factory.NewAPIEndpoint(
  319. &types.APIRequestMetadata{
  320. Verb: types.APIVerbGet,
  321. Method: types.HTTPVerbGet,
  322. Path: &types.Path{
  323. Parent: basePath,
  324. RelativePath: relPath + "/gitlab",
  325. },
  326. Scopes: []types.PermissionScope{
  327. types.UserScope,
  328. types.ProjectScope,
  329. },
  330. },
  331. )
  332. listGitlabHandler := project_integration.NewListGitlabHandler(
  333. config,
  334. factory.GetResultWriter(),
  335. )
  336. routes = append(routes, &router.Route{
  337. Endpoint: listGitlabEndpoint,
  338. Handler: listGitlabHandler,
  339. Router: r,
  340. })
  341. // POST /api/projects/{project_id}/integrations/gitlab
  342. createGitlabEndpoint := factory.NewAPIEndpoint(
  343. &types.APIRequestMetadata{
  344. Verb: types.APIVerbCreate,
  345. Method: types.HTTPVerbPost,
  346. Path: &types.Path{
  347. Parent: basePath,
  348. RelativePath: relPath + "/gitlab",
  349. },
  350. Scopes: []types.PermissionScope{
  351. types.UserScope,
  352. types.ProjectScope,
  353. },
  354. },
  355. )
  356. createGitlabHandler := project_integration.NewCreateGitlabIntegration(
  357. config,
  358. factory.GetDecoderValidator(),
  359. factory.GetResultWriter(),
  360. )
  361. routes = append(routes, &router.Route{
  362. Endpoint: createGitlabEndpoint,
  363. Handler: createGitlabHandler,
  364. Router: r,
  365. })
  366. // PATCH /api/projects/{project_id}/integrations/gitlab/{integration_id}
  367. // DELETE /api/projects/{project_id}/integrations/gitlab/{integration_id}
  368. deleteGitlabEndpoint := factory.NewAPIEndpoint(
  369. &types.APIRequestMetadata{
  370. Verb: types.APIVerbDelete,
  371. Method: types.HTTPVerbDelete,
  372. Path: &types.Path{
  373. Parent: basePath,
  374. RelativePath: relPath + "/gitlab/{integration_id}",
  375. },
  376. Scopes: []types.PermissionScope{
  377. types.UserScope,
  378. types.ProjectScope,
  379. types.GitlabIntegrationScope,
  380. },
  381. },
  382. )
  383. deleteGitlabHandler := project_integration.NewDeleteGitlabIntegrationHandler(
  384. config,
  385. factory.GetDecoderValidator(),
  386. factory.GetResultWriter(),
  387. )
  388. routes = append(routes, &router.Route{
  389. Endpoint: deleteGitlabEndpoint,
  390. Handler: deleteGitlabHandler,
  391. Router: r,
  392. })
  393. // GET /api/projects/{project_id}/integrations/git
  394. listGitIntegrationsEndpoint := factory.NewAPIEndpoint(
  395. &types.APIRequestMetadata{
  396. Verb: types.APIVerbGet,
  397. Method: types.HTTPVerbGet,
  398. Path: &types.Path{
  399. Parent: basePath,
  400. RelativePath: relPath + "/git",
  401. },
  402. Scopes: []types.PermissionScope{
  403. types.UserScope,
  404. types.ProjectScope,
  405. },
  406. },
  407. )
  408. listGitIntegrationsHandler := project_integration.NewListGitIntegrationHandler(
  409. config,
  410. factory.GetDecoderValidator(),
  411. factory.GetResultWriter(),
  412. )
  413. routes = append(routes, &router.Route{
  414. Endpoint: listGitIntegrationsEndpoint,
  415. Handler: listGitIntegrationsHandler,
  416. Router: r,
  417. })
  418. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos
  419. listGitlabReposEndpoint := factory.NewAPIEndpoint(
  420. &types.APIRequestMetadata{
  421. Verb: types.APIVerbGet,
  422. Method: types.HTTPVerbGet,
  423. Path: &types.Path{
  424. Parent: basePath,
  425. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos", relPath, types.URLParamIntegrationID),
  426. },
  427. Scopes: []types.PermissionScope{
  428. types.UserScope,
  429. types.ProjectScope,
  430. types.GitlabIntegrationScope,
  431. },
  432. },
  433. )
  434. listGitlabReposHandler := project_integration.NewListGitlabReposHandler(
  435. config,
  436. factory.GetDecoderValidator(),
  437. factory.GetResultWriter(),
  438. )
  439. routes = append(routes, &router.Route{
  440. Endpoint: listGitlabReposEndpoint,
  441. Handler: listGitlabReposHandler,
  442. Router: r,
  443. })
  444. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/branches
  445. listGitlabRepoBranchesEndpoint := factory.NewAPIEndpoint(
  446. &types.APIRequestMetadata{
  447. Verb: types.APIVerbGet,
  448. Method: types.HTTPVerbGet,
  449. Path: &types.Path{
  450. Parent: basePath,
  451. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/branches",
  452. relPath, types.URLParamIntegrationID),
  453. },
  454. Scopes: []types.PermissionScope{
  455. types.UserScope,
  456. types.ProjectScope,
  457. types.GitlabIntegrationScope,
  458. },
  459. },
  460. )
  461. listGitlabRepoBranchesHandler := project_integration.NewListGitlabRepoBranchesHandler(
  462. config,
  463. factory.GetDecoderValidator(),
  464. factory.GetResultWriter(),
  465. )
  466. routes = append(routes, &router.Route{
  467. Endpoint: listGitlabRepoBranchesEndpoint,
  468. Handler: listGitlabRepoBranchesHandler,
  469. Router: r,
  470. })
  471. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/contents
  472. getGitlabRepoContentsEndpoint := factory.NewAPIEndpoint(
  473. &types.APIRequestMetadata{
  474. Verb: types.APIVerbGet,
  475. Method: types.HTTPVerbGet,
  476. Path: &types.Path{
  477. Parent: basePath,
  478. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/contents", relPath,
  479. types.URLParamIntegrationID),
  480. },
  481. Scopes: []types.PermissionScope{
  482. types.UserScope,
  483. types.ProjectScope,
  484. types.GitlabIntegrationScope,
  485. },
  486. },
  487. )
  488. getGitlabRepoContentsHandler := project_integration.NewGetGitlabRepoContentsHandler(
  489. config,
  490. factory.GetDecoderValidator(),
  491. factory.GetResultWriter(),
  492. )
  493. routes = append(routes, &router.Route{
  494. Endpoint: getGitlabRepoContentsEndpoint,
  495. Handler: getGitlabRepoContentsHandler,
  496. Router: r,
  497. })
  498. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/buildpack/detect
  499. getGitlabRepoBuildpackEndpoint := factory.NewAPIEndpoint(
  500. &types.APIRequestMetadata{
  501. Verb: types.APIVerbGet,
  502. Method: types.HTTPVerbGet,
  503. Path: &types.Path{
  504. Parent: basePath,
  505. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/buildpack/detect", relPath,
  506. types.URLParamIntegrationID),
  507. },
  508. Scopes: []types.PermissionScope{
  509. types.UserScope,
  510. types.ProjectScope,
  511. types.GitlabIntegrationScope,
  512. },
  513. },
  514. )
  515. getGitlabRepoBuildpackHandler := project_integration.NewGetGitlabRepoBuildpackHandler(
  516. config,
  517. factory.GetDecoderValidator(),
  518. factory.GetResultWriter(),
  519. )
  520. routes = append(routes, &router.Route{
  521. Endpoint: getGitlabRepoBuildpackEndpoint,
  522. Handler: getGitlabRepoBuildpackHandler,
  523. Router: r,
  524. })
  525. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/procfile
  526. getGitlabRepoProcfileEndpoint := factory.NewAPIEndpoint(
  527. &types.APIRequestMetadata{
  528. Verb: types.APIVerbGet,
  529. Method: types.HTTPVerbGet,
  530. Path: &types.Path{
  531. Parent: basePath,
  532. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/procfile", relPath,
  533. types.URLParamIntegrationID),
  534. },
  535. Scopes: []types.PermissionScope{
  536. types.UserScope,
  537. types.ProjectScope,
  538. types.GitlabIntegrationScope,
  539. },
  540. },
  541. )
  542. getGitlabRepoProcfileHandler := project_integration.NewGetGitlabRepoProcfileHandler(
  543. config,
  544. factory.GetDecoderValidator(),
  545. factory.GetResultWriter(),
  546. )
  547. routes = append(routes, &router.Route{
  548. Endpoint: getGitlabRepoProcfileEndpoint,
  549. Handler: getGitlabRepoProcfileHandler,
  550. Router: r,
  551. })
  552. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/porteryaml
  553. getGitlabRepoPorterYamlContentsEndpoint := factory.NewAPIEndpoint(
  554. &types.APIRequestMetadata{
  555. Verb: types.APIVerbGet,
  556. Method: types.HTTPVerbGet,
  557. Path: &types.Path{
  558. Parent: basePath,
  559. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/porteryaml", relPath,
  560. types.URLParamIntegrationID),
  561. },
  562. Scopes: []types.PermissionScope{
  563. types.UserScope,
  564. types.ProjectScope,
  565. types.GitlabIntegrationScope,
  566. },
  567. },
  568. )
  569. getGitlabRepoPorterYamlContentsHandler := project_integration.NewGetGitlabRepoPorterYamlContentsHandler(
  570. config,
  571. factory.GetDecoderValidator(),
  572. factory.GetResultWriter(),
  573. )
  574. routes = append(routes, &router.Route{
  575. Endpoint: getGitlabRepoPorterYamlContentsEndpoint,
  576. Handler: getGitlabRepoPorterYamlContentsHandler,
  577. Router: r,
  578. })
  579. return routes, newPath
  580. }