git_installation.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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}/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: relPath + "/clusters/{cluster_id}/environment",
  81. },
  82. Scopes: []types.PermissionScope{
  83. types.UserScope,
  84. types.ProjectScope,
  85. types.GitInstallationScope,
  86. types.ClusterScope,
  87. },
  88. },
  89. )
  90. createEnvironmentHandler := environment.NewCreateEnvironmentHandler(
  91. config,
  92. factory.GetDecoderValidator(),
  93. factory.GetResultWriter(),
  94. )
  95. routes = append(routes, &Route{
  96. Endpoint: createEnvironmentEndpoint,
  97. Handler: createEnvironmentHandler,
  98. Router: r,
  99. })
  100. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/deployment ->
  101. // environment.NewCreateDeploymentHandler
  102. createDeploymentEndpoint := factory.NewAPIEndpoint(
  103. &types.APIRequestMetadata{
  104. Verb: types.APIVerbCreate,
  105. Method: types.HTTPVerbPost,
  106. Path: &types.Path{
  107. Parent: basePath,
  108. RelativePath: relPath + "/clusters/{cluster_id}/deployment",
  109. },
  110. Scopes: []types.PermissionScope{
  111. types.UserScope,
  112. types.ProjectScope,
  113. types.GitInstallationScope,
  114. types.ClusterScope,
  115. },
  116. },
  117. )
  118. createDeploymentHandler := environment.NewCreateDeploymentHandler(
  119. config,
  120. factory.GetDecoderValidator(),
  121. factory.GetResultWriter(),
  122. )
  123. routes = append(routes, &Route{
  124. Endpoint: createDeploymentEndpoint,
  125. Handler: createDeploymentHandler,
  126. Router: r,
  127. })
  128. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/deployment ->
  129. // environment.NewCreateDeploymentHandler
  130. getDeploymentEndpoint := factory.NewAPIEndpoint(
  131. &types.APIRequestMetadata{
  132. Verb: types.APIVerbGet,
  133. Method: types.HTTPVerbGet,
  134. Path: &types.Path{
  135. Parent: basePath,
  136. RelativePath: relPath + "/clusters/{cluster_id}/deployment",
  137. },
  138. Scopes: []types.PermissionScope{
  139. types.UserScope,
  140. types.ProjectScope,
  141. types.GitInstallationScope,
  142. types.ClusterScope,
  143. },
  144. },
  145. )
  146. getDeploymentHandler := environment.NewGetDeploymentHandler(
  147. config,
  148. factory.GetDecoderValidator(),
  149. factory.GetResultWriter(),
  150. )
  151. routes = append(routes, &Route{
  152. Endpoint: getDeploymentEndpoint,
  153. Handler: getDeploymentHandler,
  154. Router: r,
  155. })
  156. // POST /api/projects/{project_id}/gitrepos/{git_installation_id}/clusters/{cluster_id}/deployment/finalize ->
  157. // environment.NewFinalizeDeploymentHandler
  158. finalizeDeploymentEndpoint := factory.NewAPIEndpoint(
  159. &types.APIRequestMetadata{
  160. Verb: types.APIVerbCreate,
  161. Method: types.HTTPVerbPost,
  162. Path: &types.Path{
  163. Parent: basePath,
  164. RelativePath: relPath + "/clusters/{cluster_id}/deployment/finalize",
  165. },
  166. Scopes: []types.PermissionScope{
  167. types.UserScope,
  168. types.ProjectScope,
  169. types.GitInstallationScope,
  170. types.ClusterScope,
  171. },
  172. },
  173. )
  174. finalizeDeploymentHandler := environment.NewFinalizeDeploymentHandler(
  175. config,
  176. factory.GetDecoderValidator(),
  177. factory.GetResultWriter(),
  178. )
  179. routes = append(routes, &Route{
  180. Endpoint: finalizeDeploymentEndpoint,
  181. Handler: finalizeDeploymentHandler,
  182. Router: r,
  183. })
  184. // GET /api/projects/{project_id}/gitrepos/{git_installation_id}/repos ->
  185. // gitinstallation.GithubListReposHandler
  186. listReposEndpoint := factory.NewAPIEndpoint(
  187. &types.APIRequestMetadata{
  188. Verb: types.APIVerbList,
  189. Method: types.HTTPVerbGet,
  190. Path: &types.Path{
  191. Parent: basePath,
  192. RelativePath: relPath + "/repos",
  193. },
  194. Scopes: []types.PermissionScope{
  195. types.UserScope,
  196. types.ProjectScope,
  197. types.GitInstallationScope,
  198. },
  199. },
  200. )
  201. listReposHandler := gitinstallation.NewGithubListReposHandler(
  202. config,
  203. factory.GetResultWriter(),
  204. )
  205. routes = append(routes, &Route{
  206. Endpoint: listReposEndpoint,
  207. Handler: listReposHandler,
  208. Router: r,
  209. })
  210. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/branches ->
  211. // gitinstallation.GithubListBranchesHandler
  212. listBranchesEndpoint := factory.NewAPIEndpoint(
  213. &types.APIRequestMetadata{
  214. Verb: types.APIVerbList,
  215. Method: types.HTTPVerbGet,
  216. Path: &types.Path{
  217. Parent: basePath,
  218. RelativePath: fmt.Sprintf(
  219. "%s/repos/{%s}/{%s}/{%s}/branches",
  220. relPath,
  221. types.URLParamGitKind,
  222. types.URLParamGitRepoOwner,
  223. types.URLParamGitRepoName,
  224. ),
  225. },
  226. Scopes: []types.PermissionScope{
  227. types.UserScope,
  228. types.ProjectScope,
  229. types.GitInstallationScope,
  230. },
  231. },
  232. )
  233. listBranchesHandler := gitinstallation.NewGithubListBranchesHandler(
  234. config,
  235. factory.GetResultWriter(),
  236. )
  237. routes = append(routes, &Route{
  238. Endpoint: listBranchesEndpoint,
  239. Handler: listBranchesHandler,
  240. Router: r,
  241. })
  242. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/buildpack/detect ->
  243. // gitinstallation.NewGithubGetBuildpackHandler
  244. getBuildpackEndpoint := factory.NewAPIEndpoint(
  245. &types.APIRequestMetadata{
  246. Verb: types.APIVerbGet,
  247. Method: types.HTTPVerbGet,
  248. Path: &types.Path{
  249. Parent: basePath,
  250. RelativePath: fmt.Sprintf(
  251. "%s/repos/{%s}/{%s}/{%s}/{%s}/buildpack/detect",
  252. relPath,
  253. types.URLParamGitKind,
  254. types.URLParamGitRepoOwner,
  255. types.URLParamGitRepoName,
  256. types.URLParamGitBranch,
  257. ),
  258. },
  259. Scopes: []types.PermissionScope{
  260. types.UserScope,
  261. types.ProjectScope,
  262. types.GitInstallationScope,
  263. },
  264. },
  265. )
  266. getBuildpackHandler := gitinstallation.NewGithubGetBuildpackHandler(
  267. config,
  268. factory.GetDecoderValidator(),
  269. factory.GetResultWriter(),
  270. )
  271. routes = append(routes, &Route{
  272. Endpoint: getBuildpackEndpoint,
  273. Handler: getBuildpackHandler,
  274. Router: r,
  275. })
  276. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents ->
  277. // gitinstallation.NewGithubGetContentsHandler
  278. getContentsEndpoint := factory.NewAPIEndpoint(
  279. &types.APIRequestMetadata{
  280. Verb: types.APIVerbGet,
  281. Method: types.HTTPVerbGet,
  282. Path: &types.Path{
  283. Parent: basePath,
  284. RelativePath: fmt.Sprintf(
  285. "%s/repos/{%s}/{%s}/{%s}/{%s}/contents",
  286. relPath,
  287. types.URLParamGitKind,
  288. types.URLParamGitRepoOwner,
  289. types.URLParamGitRepoName,
  290. types.URLParamGitBranch,
  291. ),
  292. },
  293. Scopes: []types.PermissionScope{
  294. types.UserScope,
  295. types.ProjectScope,
  296. types.GitInstallationScope,
  297. },
  298. },
  299. )
  300. getContentsHandler := gitinstallation.NewGithubGetContentsHandler(
  301. config,
  302. factory.GetDecoderValidator(),
  303. factory.GetResultWriter(),
  304. )
  305. routes = append(routes, &Route{
  306. Endpoint: getContentsEndpoint,
  307. Handler: getContentsHandler,
  308. Router: r,
  309. })
  310. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile ->
  311. // gitinstallation.NewGithubGetProcfileHandler
  312. getProcfileEndpoint := factory.NewAPIEndpoint(
  313. &types.APIRequestMetadata{
  314. Verb: types.APIVerbGet,
  315. Method: types.HTTPVerbGet,
  316. Path: &types.Path{
  317. Parent: basePath,
  318. RelativePath: fmt.Sprintf(
  319. "%s/repos/{%s}/{%s}/{%s}/{%s}/procfile",
  320. relPath,
  321. types.URLParamGitKind,
  322. types.URLParamGitRepoOwner,
  323. types.URLParamGitRepoName,
  324. types.URLParamGitBranch,
  325. ),
  326. },
  327. Scopes: []types.PermissionScope{
  328. types.UserScope,
  329. types.ProjectScope,
  330. types.GitInstallationScope,
  331. },
  332. },
  333. )
  334. getProcfileHandler := gitinstallation.NewGithubGetProcfileHandler(
  335. config,
  336. factory.GetDecoderValidator(),
  337. factory.GetResultWriter(),
  338. )
  339. routes = append(routes, &Route{
  340. Endpoint: getProcfileEndpoint,
  341. Handler: getProcfileHandler,
  342. Router: r,
  343. })
  344. // GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url ->
  345. // gitinstallation.NewGithubGetTarballURLHandler
  346. getTarballURLEndpoint := factory.NewAPIEndpoint(
  347. &types.APIRequestMetadata{
  348. Verb: types.APIVerbGet,
  349. Method: types.HTTPVerbGet,
  350. Path: &types.Path{
  351. Parent: basePath,
  352. RelativePath: fmt.Sprintf(
  353. "%s/repos/{%s}/{%s}/{%s}/{%s}/tarball_url",
  354. relPath,
  355. types.URLParamGitKind,
  356. types.URLParamGitRepoOwner,
  357. types.URLParamGitRepoName,
  358. types.URLParamGitBranch,
  359. ),
  360. },
  361. Scopes: []types.PermissionScope{
  362. types.UserScope,
  363. types.ProjectScope,
  364. types.GitInstallationScope,
  365. },
  366. },
  367. )
  368. getTarballURLHandler := gitinstallation.NewGithubGetTarballURLHandler(
  369. config,
  370. factory.GetDecoderValidator(),
  371. factory.GetResultWriter(),
  372. )
  373. routes = append(routes, &Route{
  374. Endpoint: getTarballURLEndpoint,
  375. Handler: getTarballURLHandler,
  376. Router: r,
  377. })
  378. return routes, newPath
  379. }