delete.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. request := &types.FinalizeDeploymentRequest{}
  30. if ok := c.DecodeAndValidate(w, r, request); !ok {
  31. return
  32. }
  33. // read the environment to get the environment id
  34. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID))
  35. if err != nil {
  36. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  37. return
  38. }
  39. // delete Github actions files from the repo
  40. client, err := getGithubClientFromEnvironment(c.Config(), env)
  41. if err != nil {
  42. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  43. return
  44. }
  45. err = actions.DeleteEnv(&actions.EnvOpts{
  46. Client: client,
  47. ServerURL: c.Config().ServerConf.ServerURL,
  48. GitRepoOwner: env.GitRepoOwner,
  49. GitRepoName: env.GitRepoName,
  50. ProjectID: project.ID,
  51. ClusterID: cluster.ID,
  52. GitInstallationID: uint(ga.InstallationID),
  53. EnvironmentName: env.Name,
  54. })
  55. if err != nil {
  56. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  57. return
  58. }
  59. // delete the environment
  60. env, err = c.Repo().Environment().DeleteEnvironment(env)
  61. if err != nil {
  62. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  63. return
  64. }
  65. c.WriteResult(w, r, env.ToEnvironmentType())
  66. }