get_gitlab_repo_procfile.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package project_integration
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "regexp"
  8. "strings"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. "github.com/porter-dev/porter/api/server/shared"
  11. "github.com/porter-dev/porter/api/server/shared/apierrors"
  12. "github.com/porter-dev/porter/api/server/shared/commonutils"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/models"
  16. ints "github.com/porter-dev/porter/internal/models/integrations"
  17. "github.com/xanzy/go-gitlab"
  18. )
  19. var procfileRegex = regexp.MustCompile("^([A-Za-z0-9_]+):\\s*(.+)$")
  20. type GetGitlabRepoProcfileHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. }
  23. func NewGetGitlabRepoProcfileHandler(
  24. config *config.Config,
  25. decoderValidator shared.RequestDecoderValidator,
  26. writer shared.ResultWriter,
  27. ) *GetGitlabRepoProcfileHandler {
  28. return &GetGitlabRepoProcfileHandler{
  29. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  30. }
  31. }
  32. func (p *GetGitlabRepoProcfileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  33. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  34. user, _ := r.Context().Value(types.UserScope).(*models.User)
  35. gi, _ := r.Context().Value(types.GitlabIntegrationScope).(*ints.GitlabIntegration)
  36. request := &types.GetProcfileRequest{}
  37. ok := p.DecodeAndValidate(w, r, request)
  38. if !ok {
  39. return
  40. }
  41. owner, name, ok := commonutils.GetOwnerAndNameParams(p, w, r)
  42. if !ok {
  43. return
  44. }
  45. branch, ok := commonutils.GetBranchParam(p, w, r)
  46. if !ok {
  47. return
  48. }
  49. path, err := url.QueryUnescape(request.Path)
  50. if err != nil {
  51. p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("malformed query param path")))
  52. return
  53. }
  54. client, err := getGitlabClient(p.Repo(), user.ID, project.ID, gi, p.Config())
  55. if err != nil {
  56. if errors.Is(err, errUnauthorizedGitlabUser) {
  57. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(errUnauthorizedGitlabUser, http.StatusUnauthorized))
  58. }
  59. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  60. return
  61. }
  62. file, resp, err := client.RepositoryFiles.GetRawFile(fmt.Sprintf("%s/%s", owner, name),
  63. strings.TrimPrefix(path, "./"), &gitlab.GetRawFileOptions{
  64. Ref: gitlab.String(branch),
  65. },
  66. )
  67. if resp.StatusCode == http.StatusUnauthorized {
  68. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unauthorized gitlab user"), http.StatusUnauthorized))
  69. return
  70. } else if resp.StatusCode == http.StatusNotFound {
  71. w.WriteHeader(http.StatusNotFound)
  72. p.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("no such procfile exists")))
  73. return
  74. }
  75. if err != nil {
  76. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  77. return
  78. }
  79. parsedContents := make(types.GetProcfileResponse)
  80. // parse the procfile information
  81. for _, line := range strings.Split(string(file), "\n") {
  82. if matches := procfileRegex.FindStringSubmatch(line); matches != nil {
  83. parsedContents[matches[1]] = matches[2]
  84. }
  85. }
  86. p.WriteResult(w, r, parsedContents)
  87. }