2
0

project_integration.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  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. // GET /api/projects/{project_id}/integrations/azure -> project_integration.NewListAzureHandler
  195. listAzureEndpoint := factory.NewAPIEndpoint(
  196. &types.APIRequestMetadata{
  197. Verb: types.APIVerbGet,
  198. Method: types.HTTPVerbGet,
  199. Path: &types.Path{
  200. Parent: basePath,
  201. RelativePath: relPath + "/azure",
  202. },
  203. Scopes: []types.PermissionScope{
  204. types.UserScope,
  205. types.ProjectScope,
  206. },
  207. },
  208. )
  209. listAzureHandler := project_integration.NewListAzureHandler(
  210. config,
  211. factory.GetResultWriter(),
  212. )
  213. routes = append(routes, &router.Route{
  214. Endpoint: listAzureEndpoint,
  215. Handler: listAzureHandler,
  216. Router: r,
  217. })
  218. // POST /api/projects/{project_id}/integrations/gcp -> project_integration.NewCreateGCPHandler
  219. createGCPEndpoint := factory.NewAPIEndpoint(
  220. &types.APIRequestMetadata{
  221. Verb: types.APIVerbCreate,
  222. Method: types.HTTPVerbPost,
  223. Path: &types.Path{
  224. Parent: basePath,
  225. RelativePath: relPath + "/gcp",
  226. },
  227. Scopes: []types.PermissionScope{
  228. types.UserScope,
  229. types.ProjectScope,
  230. },
  231. },
  232. )
  233. createGCPHandler := project_integration.NewCreateGCPHandler(
  234. config,
  235. factory.GetDecoderValidator(),
  236. factory.GetResultWriter(),
  237. )
  238. routes = append(routes, &router.Route{
  239. Endpoint: createGCPEndpoint,
  240. Handler: createGCPHandler,
  241. Router: r,
  242. })
  243. // GET /api/projects/{project_id}/integrations/gcp -> project_integration.NewListGCPHandler
  244. listGCPEndpoint := factory.NewAPIEndpoint(
  245. &types.APIRequestMetadata{
  246. Verb: types.APIVerbGet,
  247. Method: types.HTTPVerbGet,
  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. listGCPHandler := project_integration.NewListGCPHandler(
  259. config,
  260. factory.GetResultWriter(),
  261. )
  262. routes = append(routes, &router.Route{
  263. Endpoint: listGCPEndpoint,
  264. Handler: listGCPHandler,
  265. Router: r,
  266. })
  267. // POST /api/projects/{project_id}/integrations/azure -> project_integration.NewCreateAzureHandler
  268. createAzureEndpoint := factory.NewAPIEndpoint(
  269. &types.APIRequestMetadata{
  270. Verb: types.APIVerbCreate,
  271. Method: types.HTTPVerbPost,
  272. Path: &types.Path{
  273. Parent: basePath,
  274. RelativePath: relPath + "/azure",
  275. },
  276. Scopes: []types.PermissionScope{
  277. types.UserScope,
  278. types.ProjectScope,
  279. },
  280. },
  281. )
  282. createAzureHandler := project_integration.NewCreateAzureHandler(
  283. config,
  284. factory.GetDecoderValidator(),
  285. factory.GetResultWriter(),
  286. )
  287. routes = append(routes, &router.Route{
  288. Endpoint: createAzureEndpoint,
  289. Handler: createAzureHandler,
  290. Router: r,
  291. })
  292. // GET /api/projects/{project_id}/integrations/gitlab
  293. listGitlabEndpoint := factory.NewAPIEndpoint(
  294. &types.APIRequestMetadata{
  295. Verb: types.APIVerbGet,
  296. Method: types.HTTPVerbGet,
  297. Path: &types.Path{
  298. Parent: basePath,
  299. RelativePath: relPath + "/gitlab",
  300. },
  301. Scopes: []types.PermissionScope{
  302. types.UserScope,
  303. types.ProjectScope,
  304. },
  305. },
  306. )
  307. listGitlabHandler := project_integration.NewListGitlabHandler(
  308. config,
  309. factory.GetResultWriter(),
  310. )
  311. routes = append(routes, &router.Route{
  312. Endpoint: listGitlabEndpoint,
  313. Handler: listGitlabHandler,
  314. Router: r,
  315. })
  316. // POST /api/projects/{project_id}/integrations/gitlab
  317. createGitlabEndpoint := factory.NewAPIEndpoint(
  318. &types.APIRequestMetadata{
  319. Verb: types.APIVerbCreate,
  320. Method: types.HTTPVerbPost,
  321. Path: &types.Path{
  322. Parent: basePath,
  323. RelativePath: relPath + "/gitlab",
  324. },
  325. Scopes: []types.PermissionScope{
  326. types.UserScope,
  327. types.ProjectScope,
  328. },
  329. },
  330. )
  331. createGitlabHandler := project_integration.NewCreateGitlabIntegration(
  332. config,
  333. factory.GetDecoderValidator(),
  334. factory.GetResultWriter(),
  335. )
  336. routes = append(routes, &router.Route{
  337. Endpoint: createGitlabEndpoint,
  338. Handler: createGitlabHandler,
  339. Router: r,
  340. })
  341. // PATCH /api/projects/{project_id}/integrations/gitlab/{integration_id}
  342. // DELETE /api/projects/{project_id}/integrations/gitlab/{integration_id}
  343. // GET /api/projects/{project_id}/integrations/git
  344. listGitIntegrationsEndpoint := factory.NewAPIEndpoint(
  345. &types.APIRequestMetadata{
  346. Verb: types.APIVerbGet,
  347. Method: types.HTTPVerbGet,
  348. Path: &types.Path{
  349. Parent: basePath,
  350. RelativePath: relPath + "/git",
  351. },
  352. Scopes: []types.PermissionScope{
  353. types.UserScope,
  354. types.ProjectScope,
  355. },
  356. },
  357. )
  358. listGitIntegrationsHandler := project_integration.NewListGitIntegrationHandler(
  359. config,
  360. factory.GetDecoderValidator(),
  361. factory.GetResultWriter(),
  362. )
  363. routes = append(routes, &router.Route{
  364. Endpoint: listGitIntegrationsEndpoint,
  365. Handler: listGitIntegrationsHandler,
  366. Router: r,
  367. })
  368. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos
  369. listGitlabReposEndpoint := factory.NewAPIEndpoint(
  370. &types.APIRequestMetadata{
  371. Verb: types.APIVerbGet,
  372. Method: types.HTTPVerbGet,
  373. Path: &types.Path{
  374. Parent: basePath,
  375. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos", relPath, types.URLParamIntegrationID),
  376. },
  377. Scopes: []types.PermissionScope{
  378. types.UserScope,
  379. types.ProjectScope,
  380. types.GitlabIntegrationScope,
  381. },
  382. },
  383. )
  384. listGitlabReposHandler := project_integration.NewListGitlabReposHandler(
  385. config,
  386. factory.GetDecoderValidator(),
  387. factory.GetResultWriter(),
  388. )
  389. routes = append(routes, &router.Route{
  390. Endpoint: listGitlabReposEndpoint,
  391. Handler: listGitlabReposHandler,
  392. Router: r,
  393. })
  394. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/{owner}/{name}/branches
  395. listGitlabRepoBranchesEndpoint := factory.NewAPIEndpoint(
  396. &types.APIRequestMetadata{
  397. Verb: types.APIVerbGet,
  398. Method: types.HTTPVerbGet,
  399. Path: &types.Path{
  400. Parent: basePath,
  401. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/{%s}/{%s}/branches",
  402. relPath, types.URLParamIntegrationID, types.URLParamGitRepoOwner, types.URLParamGitRepoName),
  403. },
  404. Scopes: []types.PermissionScope{
  405. types.UserScope,
  406. types.ProjectScope,
  407. types.GitlabIntegrationScope,
  408. },
  409. },
  410. )
  411. listGitlabRepoBranchesHandler := project_integration.NewListGitlabRepoBranchesHandler(
  412. config,
  413. factory.GetDecoderValidator(),
  414. factory.GetResultWriter(),
  415. )
  416. routes = append(routes, &router.Route{
  417. Endpoint: listGitlabRepoBranchesEndpoint,
  418. Handler: listGitlabRepoBranchesHandler,
  419. Router: r,
  420. })
  421. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/{owner}/{name}/{branch}/contents
  422. getGitlabRepoContentsEndpoint := factory.NewAPIEndpoint(
  423. &types.APIRequestMetadata{
  424. Verb: types.APIVerbGet,
  425. Method: types.HTTPVerbGet,
  426. Path: &types.Path{
  427. Parent: basePath,
  428. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/{%s}/{%s}/{%s}/contents", relPath,
  429. types.URLParamIntegrationID, types.URLParamGitRepoOwner,
  430. types.URLParamGitRepoName, types.URLParamGitBranch),
  431. },
  432. Scopes: []types.PermissionScope{
  433. types.UserScope,
  434. types.ProjectScope,
  435. types.GitlabIntegrationScope,
  436. },
  437. },
  438. )
  439. getGitlabRepoContentsHandler := project_integration.NewGetGitlabRepoContentsHandler(
  440. config,
  441. factory.GetDecoderValidator(),
  442. factory.GetResultWriter(),
  443. )
  444. routes = append(routes, &router.Route{
  445. Endpoint: getGitlabRepoContentsEndpoint,
  446. Handler: getGitlabRepoContentsHandler,
  447. Router: r,
  448. })
  449. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/{owner}/{name}/{branch}/buildpack/detect
  450. getGitlabRepoBuildpackEndpoint := factory.NewAPIEndpoint(
  451. &types.APIRequestMetadata{
  452. Verb: types.APIVerbGet,
  453. Method: types.HTTPVerbGet,
  454. Path: &types.Path{
  455. Parent: basePath,
  456. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/{%s}/{%s}/{%s}/buildpack/detect", relPath,
  457. types.URLParamIntegrationID, types.URLParamGitRepoOwner,
  458. types.URLParamGitRepoName, types.URLParamGitBranch),
  459. },
  460. Scopes: []types.PermissionScope{
  461. types.UserScope,
  462. types.ProjectScope,
  463. types.GitlabIntegrationScope,
  464. },
  465. },
  466. )
  467. getGitlabRepoBuildpackHandler := project_integration.NewGetGitlabRepoBuildpackHandler(
  468. config,
  469. factory.GetDecoderValidator(),
  470. factory.GetResultWriter(),
  471. )
  472. routes = append(routes, &router.Route{
  473. Endpoint: getGitlabRepoBuildpackEndpoint,
  474. Handler: getGitlabRepoBuildpackHandler,
  475. Router: r,
  476. })
  477. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/{owner}/{name}/{branch}/procfile
  478. getGitlabRepoProcfileEndpoint := factory.NewAPIEndpoint(
  479. &types.APIRequestMetadata{
  480. Verb: types.APIVerbGet,
  481. Method: types.HTTPVerbGet,
  482. Path: &types.Path{
  483. Parent: basePath,
  484. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/{%s}/{%s}/{%s}/procfile", relPath,
  485. types.URLParamIntegrationID, types.URLParamGitRepoOwner,
  486. types.URLParamGitRepoName, types.URLParamGitBranch),
  487. },
  488. Scopes: []types.PermissionScope{
  489. types.UserScope,
  490. types.ProjectScope,
  491. types.GitlabIntegrationScope,
  492. },
  493. },
  494. )
  495. getGitlabRepoProcfileHandler := project_integration.NewGetGitlabRepoProcfileHandler(
  496. config,
  497. factory.GetDecoderValidator(),
  498. factory.GetResultWriter(),
  499. )
  500. routes = append(routes, &router.Route{
  501. Endpoint: getGitlabRepoProcfileEndpoint,
  502. Handler: getGitlabRepoProcfileHandler,
  503. Router: r,
  504. })
  505. return routes, newPath
  506. }