finalize_deployment.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/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/models"
  14. "github.com/porter-dev/porter/internal/models/integrations"
  15. )
  16. type FinalizeDeploymentHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. func NewFinalizeDeploymentHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *FinalizeDeploymentHandler {
  24. return &FinalizeDeploymentHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  30. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  31. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  32. owner, name, ok := gitinstallation.GetOwnerAndNameParams(c, w, r)
  33. if !ok {
  34. return
  35. }
  36. request := &types.FinalizeDeploymentRequest{}
  37. if ok := c.DecodeAndValidate(w, r, request); !ok {
  38. return
  39. }
  40. // read the environment to get the environment id
  41. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID), owner, name)
  42. if err != nil {
  43. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  44. return
  45. }
  46. // read the deployment
  47. depl, err := c.Repo().Environment().ReadDeployment(env.ID, request.Namespace)
  48. if err != nil {
  49. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  50. return
  51. }
  52. depl.Subdomain = request.Subdomain
  53. depl.Status = types.DeploymentStatusCreated
  54. // update the deployment
  55. depl, err = c.Repo().Environment().UpdateDeployment(depl)
  56. if err != nil {
  57. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  58. return
  59. }
  60. client, err := getGithubClientFromEnvironment(c.Config(), env)
  61. if err != nil {
  62. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  63. return
  64. }
  65. // Create new deployment status to indicate deployment is ready
  66. state := "success"
  67. env_url := depl.Subdomain
  68. deploymentStatusRequest := github.DeploymentStatusRequest{
  69. State: &state,
  70. EnvironmentURL: &env_url,
  71. }
  72. _, _, err = client.Repositories.CreateDeploymentStatus(
  73. context.Background(),
  74. env.GitRepoOwner,
  75. env.GitRepoName,
  76. depl.GHDeploymentID,
  77. &deploymentStatusRequest,
  78. )
  79. if err != nil {
  80. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  81. return
  82. }
  83. // write comment in PR
  84. commentBody := fmt.Sprintf("Porter has deployed this pull request to the following URL:\n%s", depl.Subdomain)
  85. prComment := github.IssueComment{
  86. Body: &commentBody,
  87. User: &github.User{},
  88. }
  89. _, _, err = client.Issues.CreateComment(
  90. context.Background(),
  91. env.GitRepoOwner,
  92. env.GitRepoName,
  93. int(depl.PullRequestID),
  94. &prComment,
  95. )
  96. if err != nil {
  97. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  98. return
  99. }
  100. c.WriteResult(w, r, depl.ToDeploymentType())
  101. }