delete.go 4.2 KB

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