create_deployment.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package environment
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/google/go-github/v41/github"
  7. "github.com/porter-dev/porter/api/server/authz"
  8. "github.com/porter-dev/porter/api/server/handlers"
  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/models"
  14. "github.com/porter-dev/porter/internal/models/integrations"
  15. )
  16. type CreateDeploymentHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. authz.KubernetesAgentGetter
  19. }
  20. func NewCreateDeploymentHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *CreateDeploymentHandler {
  25. return &CreateDeploymentHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  28. }
  29. }
  30. func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  32. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  33. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  34. request := &types.CreateDeploymentRequest{}
  35. if ok := c.DecodeAndValidate(w, r, request); !ok {
  36. return
  37. }
  38. // read the environment to get the environment id
  39. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID))
  40. if err != nil {
  41. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  42. return
  43. }
  44. // create deployment on GitHub API
  45. client, err := getGithubClientFromEnvironment(c.Config(), env)
  46. if err != nil {
  47. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  48. return
  49. }
  50. ghDeployment, err := createDeployment(client, env, request.CreateGHDeploymentRequest)
  51. if err != nil {
  52. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  53. return
  54. }
  55. // create the deployment
  56. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  57. EnvironmentID: env.ID,
  58. Namespace: request.Namespace,
  59. Status: "creating",
  60. PullRequestID: request.PullRequestID,
  61. GitHubDeploymentID: ghDeployment.GetID(),
  62. })
  63. if err != nil {
  64. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  65. return
  66. }
  67. // create the backing namespace
  68. agent, err := c.GetAgent(r, cluster, "")
  69. if err != nil {
  70. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  71. return
  72. }
  73. _, err = agent.CreateNamespace(depl.Namespace)
  74. if err != nil {
  75. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  76. return
  77. }
  78. c.WriteResult(w, r, depl.ToDeploymentType())
  79. }
  80. func createDeployment(client *github.Client, env *models.Environment, request *types.CreateGHDeploymentRequest) (*github.Deployment, error) {
  81. branch := request.Branch
  82. envName := "Preview"
  83. automerge := false
  84. requiredContexts := []string{}
  85. deploymentRequest := github.DeploymentRequest{
  86. Ref: &branch,
  87. Environment: &envName,
  88. AutoMerge: &automerge,
  89. RequiredContexts: &requiredContexts,
  90. }
  91. deployment, _, err := client.Repositories.CreateDeployment(
  92. context.Background(),
  93. env.GitRepoOwner,
  94. env.GitRepoName,
  95. &deploymentRequest,
  96. )
  97. if err != nil {
  98. return nil, err
  99. }
  100. depID := deployment.GetID()
  101. // Create Deployment Status to indicate it's in progress
  102. state := "in_progress"
  103. log_url := fmt.Sprintf("https://github.com/%s/%s/runs/%d", env.GitRepoOwner, env.GitRepoName, request.ActionID)
  104. deploymentStatusRequest := github.DeploymentStatusRequest{
  105. State: &state,
  106. LogURL: &log_url, // link to actions tab
  107. }
  108. _, _, err = client.Repositories.CreateDeploymentStatus(
  109. context.Background(),
  110. env.GitRepoOwner,
  111. env.GitRepoName,
  112. depID,
  113. &deploymentStatusRequest,
  114. )
  115. if err != nil {
  116. return nil, err
  117. }
  118. return deployment, nil
  119. }