finalize_deployment.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package environment
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/google/go-github/github"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/models/integrations"
  13. )
  14. type FinalizeDeploymentHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. func NewFinalizeDeploymentHandler(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *FinalizeDeploymentHandler {
  22. return &FinalizeDeploymentHandler{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  24. }
  25. }
  26. func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  28. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  29. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  30. request := &types.FinalizeDeploymentRequest{}
  31. if ok := c.DecodeAndValidate(w, r, request); !ok {
  32. return
  33. }
  34. // read the environment to get the environment id
  35. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID))
  36. if err != nil {
  37. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  38. return
  39. }
  40. // read the deployment
  41. depl, err := c.Repo().Environment().ReadDeployment(env.ID, request.Namespace)
  42. if err != nil {
  43. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  44. return
  45. }
  46. depl.Subdomain = request.Subdomain
  47. depl.Status = "created"
  48. // update the deployment
  49. depl, err = c.Repo().Environment().UpdateDeployment(depl)
  50. if err != nil {
  51. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  52. return
  53. }
  54. client, err := getGithubClientFromEnvironment(c.Config(), env)
  55. if err != nil {
  56. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  57. return
  58. }
  59. // write comment in PR
  60. commentBody := "(TENTATIVE) Porter is deploying this pull request..."
  61. prComment := github.IssueComment{
  62. Body: &commentBody,
  63. User: &github.User{},
  64. }
  65. _, _, err = client.Issues.CreateComment(
  66. context.Background(),
  67. env.GitRepoOwner,
  68. env.GitRepoName,
  69. int(depl.PullRequestID),
  70. &prComment,
  71. )
  72. if err != nil {
  73. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  74. return
  75. }
  76. c.WriteResult(w, r, depl.ToDeploymentType())
  77. }