create.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package environment
  2. import (
  3. "net/http"
  4. "strconv"
  5. ghinstallation "github.com/bradleyfalzon/ghinstallation/v2"
  6. "github.com/google/go-github/v41/github"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/auth/token"
  13. "github.com/porter-dev/porter/internal/integrations/ci/actions"
  14. "github.com/porter-dev/porter/internal/models"
  15. "github.com/porter-dev/porter/internal/models/integrations"
  16. )
  17. type CreateEnvironmentHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. func NewCreateEnvironmentHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *CreateEnvironmentHandler {
  25. return &CreateEnvironmentHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  30. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  31. user, _ := r.Context().Value(types.UserScope).(*models.User)
  32. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  33. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  34. // create the environment
  35. request := &types.CreateEnvironmentRequest{}
  36. if ok := c.DecodeAndValidate(w, r, request); !ok {
  37. return
  38. }
  39. env, err := c.Repo().Environment().CreateEnvironment(&models.Environment{
  40. ProjectID: project.ID,
  41. ClusterID: cluster.ID,
  42. GitInstallationID: uint(ga.InstallationID),
  43. Name: request.Name,
  44. GitRepoOwner: request.GitRepoOwner,
  45. GitRepoName: request.GitRepoName,
  46. })
  47. if err != nil {
  48. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  49. return
  50. }
  51. // write Github actions files to the repo
  52. client, err := getGithubClientFromEnvironment(c.Config(), env)
  53. if err != nil {
  54. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  55. return
  56. }
  57. // generate porter jwt token
  58. jwt, err := token.GetTokenForAPI(user.ID, project.ID)
  59. if err != nil {
  60. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  61. return
  62. }
  63. encoded, err := jwt.EncodeToken(c.Config().TokenConf)
  64. if err != nil {
  65. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  66. return
  67. }
  68. _, err = actions.SetupEnv(&actions.EnvOpts{
  69. Client: client,
  70. ServerURL: c.Config().ServerConf.ServerURL,
  71. PorterToken: encoded,
  72. GitRepoOwner: request.GitRepoOwner,
  73. GitRepoName: request.GitRepoName,
  74. ProjectID: project.ID,
  75. ClusterID: cluster.ID,
  76. GitInstallationID: uint(ga.InstallationID),
  77. EnvironmentName: request.Name,
  78. })
  79. if err != nil {
  80. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  81. return
  82. }
  83. c.WriteResult(w, r, env.ToEnvironmentType())
  84. }
  85. func getGithubClientFromEnvironment(config *config.Config, env *models.Environment) (*github.Client, error) {
  86. // get the github app client
  87. ghAppId, err := strconv.Atoi(config.ServerConf.GithubAppID)
  88. if err != nil {
  89. return nil, err
  90. }
  91. // authenticate as github app installation
  92. itr, err := ghinstallation.NewKeyFromFile(
  93. http.DefaultTransport,
  94. int64(ghAppId),
  95. int64(env.GitInstallationID),
  96. config.ServerConf.GithubAppSecretPath,
  97. )
  98. if err != nil {
  99. return nil, err
  100. }
  101. return github.NewClient(&http.Client{Transport: itr}), nil
  102. }