delete.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package environment
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "github.com/google/go-github/v41/github"
  8. "github.com/porter-dev/porter/api/server/authz"
  9. "github.com/porter-dev/porter/api/server/handlers"
  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/commonutils"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/integrations/ci/actions"
  16. "github.com/porter-dev/porter/internal/models"
  17. "github.com/porter-dev/porter/internal/models/integrations"
  18. "gorm.io/gorm"
  19. )
  20. type DeleteEnvironmentHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. authz.KubernetesAgentGetter
  23. }
  24. func NewDeleteEnvironmentHandler(
  25. config *config.Config,
  26. decoderValidator shared.RequestDecoderValidator,
  27. writer shared.ResultWriter,
  28. ) *DeleteEnvironmentHandler {
  29. return &DeleteEnvironmentHandler{
  30. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  31. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  32. }
  33. }
  34. func (c *DeleteEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  36. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  37. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  38. owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
  39. if !ok {
  40. return
  41. }
  42. // read the environment to get the environment id
  43. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID), owner, name)
  44. if err != nil {
  45. if errors.Is(err, gorm.ErrRecordNotFound) {
  46. c.HandleAPIError(w, r, apierrors.NewErrNotFound(fmt.Errorf("environment not found in cluster and project")))
  47. return
  48. }
  49. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  50. return
  51. }
  52. // delete all corresponding deployments
  53. agent, err := c.GetAgent(r, cluster, "")
  54. if err != nil {
  55. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  56. return
  57. }
  58. depls, err := c.Repo().Environment().ListDeployments(env.ID)
  59. if err != nil {
  60. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  61. return
  62. }
  63. for _, depl := range depls {
  64. agent.DeleteNamespace(depl.Namespace)
  65. }
  66. ghWebhookID := env.GithubWebhookID
  67. webhookUID := env.WebhookID
  68. // delete the environment
  69. env, err = c.Repo().Environment().DeleteEnvironment(env)
  70. if err != nil {
  71. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  72. return
  73. }
  74. client, err := getGithubClientFromEnvironment(c.Config(), env)
  75. if err != nil {
  76. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  77. return
  78. }
  79. // FIXME: ignore the return status codes for now, should be fixed when we start returning all non-fatal errors
  80. if ghWebhookID != 0 {
  81. client.Repositories.DeleteHook(context.Background(), owner, name, ghWebhookID)
  82. } else {
  83. webhookURL := getGithubWebhookURLFromUID(c.Config().ServerConf.ServerURL, string(webhookUID))
  84. // FIXME: should be cycling through all webhooks if pagination is needed
  85. hooks, _, err := client.Repositories.ListHooks(context.Background(), owner, name, &github.ListOptions{})
  86. if err == nil {
  87. for _, hook := range hooks {
  88. if hookURL, ok := hook.Config["url"]; ok {
  89. if hookURLStr, ok := hookURL.(string); ok {
  90. if hookURLStr == webhookURL {
  91. client.Repositories.DeleteHook(context.Background(), owner, name, hook.GetID())
  92. break
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. err = actions.DeleteEnv(&actions.EnvOpts{
  100. Client: client,
  101. ServerURL: c.Config().ServerConf.ServerURL,
  102. GitRepoOwner: env.GitRepoOwner,
  103. GitRepoName: env.GitRepoName,
  104. ProjectID: project.ID,
  105. ClusterID: cluster.ID,
  106. GitInstallationID: uint(ga.InstallationID),
  107. EnvironmentName: env.Name,
  108. InstanceName: c.Config().ServerConf.InstanceName,
  109. })
  110. if err != nil {
  111. if errors.Is(err, actions.ErrProtectedBranch) {
  112. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  113. fmt.Errorf("we were unable to delete the Porter Preview Environment workflow files for this "+
  114. "repository as the default branch is protected. Please manually delete them."), http.StatusConflict,
  115. ))
  116. return
  117. }
  118. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  119. return
  120. }
  121. c.WriteResult(w, r, env.ToEnvironmentType())
  122. }