create_deployment.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/commonutils"
  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 := commonutils.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.PRBranchFrom, request.ActionID)
  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(
  92. client *github.Client,
  93. env *models.Environment,
  94. branchFrom string,
  95. actionID uint,
  96. ) (*github.Deployment, error) {
  97. requiredContexts := []string{}
  98. deploymentRequest := github.DeploymentRequest{
  99. Ref: github.String(branchFrom),
  100. Environment: github.String(env.Name),
  101. AutoMerge: github.Bool(false),
  102. RequiredContexts: &requiredContexts,
  103. }
  104. deployment, _, err := client.Repositories.CreateDeployment(
  105. context.Background(),
  106. env.GitRepoOwner,
  107. env.GitRepoName,
  108. &deploymentRequest,
  109. )
  110. if err != nil {
  111. return nil, err
  112. }
  113. depID := deployment.GetID()
  114. // Create Deployment Status to indicate it's in progress
  115. state := "in_progress"
  116. log_url := fmt.Sprintf("https://github.com/%s/%s/actions/runs/%d", env.GitRepoOwner, env.GitRepoName, actionID)
  117. deploymentStatusRequest := github.DeploymentStatusRequest{
  118. State: &state,
  119. LogURL: &log_url, // link to actions tab
  120. }
  121. _, _, err = client.Repositories.CreateDeploymentStatus(
  122. context.Background(),
  123. env.GitRepoOwner,
  124. env.GitRepoName,
  125. depID,
  126. &deploymentStatusRequest,
  127. )
  128. if err != nil {
  129. return nil, err
  130. }
  131. return deployment, nil
  132. }