finalize_deployment.go 3.0 KB

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