| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package environment
- import (
- "context"
- "errors"
- "fmt"
- "net/http"
- "github.com/google/go-github/v41/github"
- "github.com/porter-dev/porter/api/server/authz"
- "github.com/porter-dev/porter/api/server/handlers"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/apierrors"
- "github.com/porter-dev/porter/api/server/shared/commonutils"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/internal/integrations/ci/actions"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/models/integrations"
- )
- type DeleteEnvironmentHandler struct {
- handlers.PorterHandlerReadWriter
- authz.KubernetesAgentGetter
- }
- func NewDeleteEnvironmentHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *DeleteEnvironmentHandler {
- return &DeleteEnvironmentHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
- }
- }
- func (c *DeleteEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
- project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
- cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
- owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
- if !ok {
- return
- }
- // read the environment to get the environment id
- env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID), owner, name)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // delete Github actions files from the repo
- client, err := getGithubClientFromEnvironment(c.Config(), env)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // delete all corresponding deployments
- agent, err := c.GetAgent(r, cluster, "")
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- depls, err := c.Repo().Environment().ListDeployments(env.ID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- for _, depl := range depls {
- agent.DeleteNamespace(depl.Namespace)
- }
- ghWebhookID := env.GithubWebhookID
- webhookUID := env.WebhookID
- // delete the environment
- env, err = c.Repo().Environment().DeleteEnvironment(env)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // FIXME: ignore the return status codes for now, should be fixed when we start returning all non-fatal errors
- if ghWebhookID != 0 {
- client.Repositories.DeleteHook(context.Background(), owner, name, ghWebhookID)
- } else {
- webhookURL := getGithubWebhookURLFromUID(c.Config().ServerConf.ServerURL, string(webhookUID))
- // FIXME: should be cycling through all webhooks if pagination is needed
- hooks, _, err := client.Repositories.ListHooks(context.Background(), owner, name, &github.ListOptions{})
- if err == nil {
- for _, hook := range hooks {
- if hookURL, ok := hook.Config["url"]; ok {
- if hookURLStr, ok := hookURL.(string); ok {
- if hookURLStr == webhookURL {
- client.Repositories.DeleteHook(context.Background(), owner, name, hook.GetID())
- break
- }
- }
- }
- }
- }
- }
- err = actions.DeleteEnv(&actions.EnvOpts{
- Client: client,
- ServerURL: c.Config().ServerConf.ServerURL,
- GitRepoOwner: env.GitRepoOwner,
- GitRepoName: env.GitRepoName,
- ProjectID: project.ID,
- ClusterID: cluster.ID,
- GitInstallationID: uint(ga.InstallationID),
- EnvironmentName: env.Name,
- })
- if err != nil {
- if errors.Is(err, actions.ErrProtectedBranch) {
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
- fmt.Errorf("We were unable to delete the Porter Preview Environment workflow files for this "+
- "repository as the default branch is protected. Please manually delete them."), http.StatusConflict,
- ))
- return
- }
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- c.WriteResult(w, r, env.ToEnvironmentType())
- }
|