delete.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package environment
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/integrations/ci/actions"
  10. "github.com/porter-dev/porter/internal/models"
  11. "github.com/porter-dev/porter/internal/models/integrations"
  12. )
  13. type DeleteEnvironmentHandler struct {
  14. handlers.PorterHandlerReadWriter
  15. }
  16. func NewDeleteEnvironmentHandler(
  17. config *config.Config,
  18. decoderValidator shared.RequestDecoderValidator,
  19. writer shared.ResultWriter,
  20. ) *DeleteEnvironmentHandler {
  21. return &DeleteEnvironmentHandler{
  22. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  23. }
  24. }
  25. func (c *DeleteEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  26. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  27. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  28. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  29. // read the environment to get the environment id
  30. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID))
  31. if err != nil {
  32. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  33. return
  34. }
  35. // delete Github actions files from the repo
  36. client, err := getGithubClientFromEnvironment(c.Config(), env)
  37. if err != nil {
  38. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  39. return
  40. }
  41. err = actions.DeleteEnv(&actions.EnvOpts{
  42. Client: client,
  43. ServerURL: c.Config().ServerConf.ServerURL,
  44. GitRepoOwner: env.GitRepoOwner,
  45. GitRepoName: env.GitRepoName,
  46. ProjectID: project.ID,
  47. ClusterID: cluster.ID,
  48. GitInstallationID: uint(ga.InstallationID),
  49. EnvironmentName: env.Name,
  50. })
  51. if err != nil {
  52. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  53. return
  54. }
  55. // delete the environment
  56. env, err = c.Repo().Environment().DeleteEnvironment(env)
  57. if err != nil {
  58. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  59. return
  60. }
  61. c.WriteResult(w, r, env.ToEnvironmentType())
  62. }