update_porter_app.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package stacks
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/authz"
  5. "github.com/porter-dev/porter/api/server/handlers"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/server/shared/requestutils"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. )
  13. type UpdatePorterAppHandler struct {
  14. handlers.PorterHandlerReadWriter
  15. authz.KubernetesAgentGetter
  16. }
  17. func NewUpdatePorterAppHandler(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *UpdatePorterAppHandler {
  22. return &UpdatePorterAppHandler{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  24. }
  25. }
  26. func (c *UpdatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. ctx := r.Context()
  28. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  29. name, _ := requestutils.GetURLParamString(r, types.URLParamReleaseName)
  30. porterApp, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, name)
  31. if err != nil {
  32. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  33. return
  34. }
  35. request := &types.UpdatePorterAppRequest{}
  36. ok := c.DecodeAndValidate(w, r, request)
  37. if !ok {
  38. return
  39. }
  40. if request.RepoName != "" {
  41. porterApp.RepoName = request.RepoName
  42. }
  43. if request.GitBranch != "" {
  44. porterApp.GitBranch = request.GitBranch
  45. }
  46. if request.BuildContext != "" {
  47. porterApp.BuildContext = request.BuildContext
  48. }
  49. if request.Builder != "" {
  50. porterApp.Builder = request.Builder
  51. }
  52. if request.Buildpacks != "" {
  53. porterApp.Buildpacks = request.Buildpacks
  54. }
  55. if request.Dockerfile != "" {
  56. porterApp.Dockerfile = request.Dockerfile
  57. }
  58. if request.ImageRepoURI != "" {
  59. porterApp.ImageRepoURI = request.ImageRepoURI
  60. }
  61. if request.PullRequestURL != "" {
  62. porterApp.PullRequestURL = request.PullRequestURL
  63. }
  64. updatedPorterApp, err := c.Repo().PorterApp().UpdatePorterApp(porterApp)
  65. if err != nil {
  66. return
  67. }
  68. c.WriteResult(w, r, updatedPorterApp.ToPorterAppType())
  69. }