create_deployment.go 4.3 KB

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