create.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package environment
  2. import (
  3. "net/http"
  4. "strconv"
  5. "github.com/bradleyfalzon/ghinstallation"
  6. "github.com/google/go-github/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/models"
  13. "github.com/porter-dev/porter/internal/models/integrations"
  14. )
  15. type CreateEnvironmentHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. func NewCreateEnvironmentHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *CreateEnvironmentHandler {
  23. return &CreateEnvironmentHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  29. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  30. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  31. // create the environment
  32. request := &types.CreateEnvironmentRequest{}
  33. if ok := c.DecodeAndValidate(w, r, request); !ok {
  34. return
  35. }
  36. env, err := c.Repo().Environment().CreateEnvironment(&models.Environment{
  37. ProjectID: project.ID,
  38. ClusterID: cluster.ID,
  39. GitInstallationID: uint(ga.InstallationID),
  40. Name: request.Name,
  41. GitRepoOwner: request.GitRepoOwner,
  42. GitRepoName: request.GitRepoName,
  43. })
  44. if err != nil {
  45. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  46. return
  47. }
  48. // TODO: upon environment creation, write Github actions files to the repo
  49. // client, err := getGithubClientFromEnvironment(c.Config(), env)
  50. // if err != nil {
  51. // c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  52. // return
  53. // }
  54. c.WriteResult(w, r, env.ToEnvironmentType())
  55. }
  56. func getGithubClientFromEnvironment(config *config.Config, env *models.Environment) (*github.Client, error) {
  57. // get the github app client
  58. ghAppId, err := strconv.Atoi(config.ServerConf.GithubAppID)
  59. if err != nil {
  60. return nil, err
  61. }
  62. // authenticate as github app installation
  63. itr, err := ghinstallation.NewKeyFromFile(
  64. http.DefaultTransport,
  65. int64(ghAppId),
  66. int64(env.GitInstallationID),
  67. config.ServerConf.GithubAppSecretPath,
  68. )
  69. if err != nil {
  70. return nil, err
  71. }
  72. return github.NewClient(&http.Client{Transport: itr}), nil
  73. }