create.go 3.5 KB

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