delete.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(errEnvironmentNotFound))
  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. if !isSystemNamespace(depl.Namespace) {
  65. agent.DeleteNamespace(depl.Namespace)
  66. }
  67. }
  68. ghWebhookID := env.GithubWebhookID
  69. webhookUID := env.WebhookID
  70. // delete the environment
  71. env, err = c.Repo().Environment().DeleteEnvironment(env)
  72. if err != nil {
  73. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  74. return
  75. }
  76. client, err := getGithubClientFromEnvironment(c.Config(), env)
  77. if err != nil {
  78. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  79. return
  80. }
  81. // FIXME: ignore the return status codes for now, should be fixed when we start returning all non-fatal errors
  82. if ghWebhookID != 0 {
  83. client.Repositories.DeleteHook(context.Background(), owner, name, ghWebhookID)
  84. } else {
  85. webhookURL := getGithubWebhookURLFromUID(c.Config().ServerConf.ServerURL, string(webhookUID))
  86. // FIXME: should be cycling through all webhooks if pagination is needed
  87. hooks, _, err := client.Repositories.ListHooks(context.Background(), owner, name, &github.ListOptions{})
  88. if err == nil {
  89. for _, hook := range hooks {
  90. if hookURL, ok := hook.Config["url"]; ok {
  91. if hookURLStr, ok := hookURL.(string); ok {
  92. if hookURLStr == webhookURL {
  93. client.Repositories.DeleteHook(context.Background(), owner, name, hook.GetID())
  94. break
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. err = actions.DeleteEnv(&actions.EnvOpts{
  102. Client: client,
  103. ServerURL: c.Config().ServerConf.ServerURL,
  104. GitRepoOwner: env.GitRepoOwner,
  105. GitRepoName: env.GitRepoName,
  106. ProjectID: project.ID,
  107. ClusterID: cluster.ID,
  108. GitInstallationID: uint(ga.InstallationID),
  109. EnvironmentName: env.Name,
  110. InstanceName: c.Config().ServerConf.InstanceName,
  111. })
  112. if err != nil {
  113. if errors.Is(err, actions.ErrProtectedBranch) {
  114. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  115. fmt.Errorf("we were unable to delete the Porter Preview Environment workflow files for this "+
  116. "repository as the default branch is protected. Please manually delete them."), http.StatusConflict,
  117. ))
  118. return
  119. }
  120. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  121. return
  122. }
  123. c.WriteResult(w, r, env.ToEnvironmentType())
  124. }