update_git_action_config.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package release
  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/models"
  10. "gorm.io/gorm"
  11. )
  12. type UpdateGitActionConfigHandler struct {
  13. handlers.PorterHandlerReadWriter
  14. }
  15. func NewUpdateGitActionConfigHandler(
  16. config *config.Config,
  17. decoderValidator shared.RequestDecoderValidator,
  18. writer shared.ResultWriter,
  19. ) *UpdateGitActionConfigHandler {
  20. return &UpdateGitActionConfigHandler{
  21. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  22. }
  23. }
  24. func (c *UpdateGitActionConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. release, _ := r.Context().Value(types.ReleaseScope).(*models.Release)
  26. request := &types.UpdateGitActionConfigRequest{}
  27. if ok := c.DecodeAndValidate(w, r, request); !ok {
  28. return
  29. }
  30. actionConfig, err := c.Repo().GitActionConfig().ReadGitActionConfig(release.GitActionConfig.ID)
  31. if err != nil {
  32. if err == gorm.ErrRecordNotFound {
  33. w.WriteHeader(http.StatusNotFound)
  34. return
  35. }
  36. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  37. }
  38. actionConfig.GitBranch = request.GitActionConfig.GitBranch
  39. if err := c.Repo().GitActionConfig().UpdateGitActionConfig(actionConfig); err != nil {
  40. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  41. return
  42. }
  43. w.WriteHeader(http.StatusOK)
  44. }