update_environment_settings.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package environment
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "strings"
  9. "github.com/porter-dev/porter/internal/telemetry"
  10. "github.com/porter-dev/porter/api/server/authz"
  11. "github.com/porter-dev/porter/api/server/handlers"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/apierrors"
  14. "github.com/porter-dev/porter/api/server/shared/config"
  15. "github.com/porter-dev/porter/api/server/shared/requestutils"
  16. "github.com/porter-dev/porter/api/types"
  17. "github.com/porter-dev/porter/internal/models"
  18. "gorm.io/gorm"
  19. )
  20. type UpdateEnvironmentSettingsHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. authz.KubernetesAgentGetter
  23. }
  24. func NewUpdateEnvironmentSettingsHandler(
  25. config *config.Config,
  26. decoderValidator shared.RequestDecoderValidator,
  27. writer shared.ResultWriter,
  28. ) *UpdateEnvironmentSettingsHandler {
  29. return &UpdateEnvironmentSettingsHandler{
  30. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  31. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  32. }
  33. }
  34. func (c *UpdateEnvironmentSettingsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. ctx, span := telemetry.NewSpan(r.Context(), "serve-update-environment-settings")
  36. defer span.End()
  37. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  38. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  39. telemetry.WithAttributes(span,
  40. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  41. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  42. )
  43. envID, reqErr := requestutils.GetURLParamUint(r, "environment_id")
  44. if reqErr != nil {
  45. _ = telemetry.Error(ctx, span, reqErr, "could not get environment id from url")
  46. c.HandleAPIError(w, r, reqErr)
  47. return
  48. }
  49. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "environment-id", Value: envID})
  50. request := &types.UpdateEnvironmentSettingsRequest{}
  51. if ok := c.DecodeAndValidate(w, r, request); !ok {
  52. _ = telemetry.Error(ctx, span, nil, "could not decode and validate request")
  53. return
  54. }
  55. telemetry.WithAttributes(span,
  56. telemetry.AttributeKV{Key: "mode", Value: request.Mode},
  57. telemetry.AttributeKV{Key: "git-repo-branches", Value: request.GitRepoBranches},
  58. telemetry.AttributeKV{Key: "git-deploy-branches", Value: request.GitDeployBranches},
  59. )
  60. env, err := c.Repo().Environment().ReadEnvironmentByID(project.ID, cluster.ID, envID)
  61. if err != nil {
  62. err = telemetry.Error(ctx, span, err, "could not read environment by id")
  63. if errors.Is(err, gorm.ErrRecordNotFound) {
  64. c.HandleAPIError(w, r, apierrors.NewErrNotFound(fmt.Errorf("no such environment with ID: %d", envID)))
  65. return
  66. }
  67. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  68. return
  69. }
  70. var newBranches []string
  71. for _, br := range request.GitRepoBranches {
  72. name := strings.TrimSpace(br)
  73. if len(name) > 0 {
  74. newBranches = append(newBranches, name)
  75. }
  76. }
  77. changed := !reflect.DeepEqual(env.ToEnvironmentType().GitRepoBranches, newBranches)
  78. if changed {
  79. env.GitRepoBranches = strings.Join(request.GitRepoBranches, ",")
  80. }
  81. newBranches = []string{}
  82. for _, br := range request.GitDeployBranches {
  83. name := strings.TrimSpace(br)
  84. if len(name) > 0 {
  85. newBranches = append(newBranches, name)
  86. }
  87. }
  88. changed = !reflect.DeepEqual(env.ToEnvironmentType().GitDeployBranches, newBranches)
  89. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "changed", Value: changed})
  90. if changed {
  91. // let us check if the webhook has access to the "push" event
  92. client, err := getGithubClientFromEnvironment(c.Config(), env)
  93. if err != nil {
  94. err = telemetry.Error(ctx, span, err, "could not get github client")
  95. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  96. return
  97. }
  98. hook, _, err := client.Repositories.GetHook(
  99. context.Background(), env.GitRepoOwner, env.GitRepoName, env.GithubWebhookID,
  100. )
  101. if err != nil {
  102. err = telemetry.Error(ctx, span, err, "could not get hook")
  103. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  104. return
  105. }
  106. found := false
  107. for _, ev := range hook.Events {
  108. if ev == "push" {
  109. found = true
  110. break
  111. }
  112. }
  113. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "found", Value: found})
  114. if !found {
  115. hook.Events = append(hook.Events, "push")
  116. _, _, err := client.Repositories.EditHook(
  117. context.Background(), env.GitRepoOwner, env.GitRepoName, env.GithubWebhookID, hook,
  118. )
  119. if err != nil {
  120. err = telemetry.Error(ctx, span, err, "could not edit hook")
  121. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  122. return
  123. }
  124. }
  125. env.GitDeployBranches = strings.Join(request.GitDeployBranches, ",")
  126. if len(request.GitDeployBranches) > 0 && c.Config().ServerConf.EnableAutoPreviewBranchDeploy {
  127. errs := autoDeployBranch(env, c.Config(), request.GitDeployBranches, true)
  128. if len(errs) > 0 {
  129. errString := errs[0].Error()
  130. for _, e := range errs {
  131. errString += ": " + e.Error()
  132. }
  133. _ = telemetry.Error(ctx, span, errors.New(errString), "could not auto deploy branch")
  134. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  135. fmt.Errorf("error auto deploying preview branches: %s", errString), http.StatusConflict),
  136. )
  137. return
  138. }
  139. }
  140. }
  141. if request.DisableNewComments != env.NewCommentsDisabled {
  142. env.NewCommentsDisabled = request.DisableNewComments
  143. changed = true
  144. }
  145. if request.Mode != env.Mode {
  146. env.Mode = request.Mode
  147. changed = true
  148. }
  149. if len(request.NamespaceLabels) > 0 {
  150. var labels []string
  151. for k, v := range request.NamespaceLabels {
  152. labels = append(labels, fmt.Sprintf("%s=%s", k, v))
  153. }
  154. env.NamespaceLabels = []byte(strings.Join(labels, ","))
  155. changed = true
  156. } else {
  157. env.NamespaceLabels = []byte{}
  158. changed = true
  159. }
  160. if changed {
  161. env, err = c.Repo().Environment().UpdateEnvironment(env)
  162. if err != nil {
  163. err = telemetry.Error(ctx, span, err, "could not update environment")
  164. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  165. return
  166. }
  167. }
  168. c.WriteResult(w, r, env.ToEnvironmentType())
  169. }