get_gitlab_repo_procfile.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package project_integration
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "strings"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/commonutils"
  12. "github.com/porter-dev/porter/api/server/shared/config"
  13. "github.com/porter-dev/porter/api/server/shared/requestutils"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/oauth"
  17. "github.com/xanzy/go-gitlab"
  18. "gorm.io/gorm"
  19. )
  20. var procfileRegex = regexp.MustCompile("^([A-Za-z0-9_]+):\\s*(.+)$")
  21. type GetGitlabRepoProcfileHandler struct {
  22. handlers.PorterHandlerReadWriter
  23. }
  24. func NewGetGitlabRepoProcfileHandler(
  25. config *config.Config,
  26. decoderValidator shared.RequestDecoderValidator,
  27. writer shared.ResultWriter,
  28. ) *GetGitlabRepoProcfileHandler {
  29. return &GetGitlabRepoProcfileHandler{
  30. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  31. }
  32. }
  33. func (p *GetGitlabRepoProcfileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  34. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  35. user, _ := r.Context().Value(types.UserScope).(*models.User)
  36. request := &types.GetProcfileRequest{}
  37. ok := p.DecodeAndValidate(w, r, request)
  38. if !ok {
  39. return
  40. }
  41. integrationID, reqErr := requestutils.GetURLParamUint(r, "integration_id")
  42. if reqErr != nil {
  43. p.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  44. return
  45. }
  46. owner, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoOwner)
  47. if reqErr != nil {
  48. p.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  49. return
  50. }
  51. name, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoName)
  52. if reqErr != nil {
  53. p.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  54. return
  55. }
  56. branch, reqErr := requestutils.GetURLParamString(r, types.URLParamGitBranch)
  57. if reqErr != nil {
  58. p.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  59. return
  60. }
  61. gi, err := p.Repo().GitlabIntegration().ReadGitlabIntegration(project.ID, integrationID)
  62. if err != nil {
  63. if errors.Is(err, gorm.ErrRecordNotFound) {
  64. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("no gitlab integration with ID: %d", integrationID), http.StatusNotFound))
  65. return
  66. }
  67. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  68. return
  69. }
  70. giAppOAuth, err := p.Repo().GitlabAppOAuthIntegration().ReadGitlabAppOAuthIntegration(user.ID, project.ID, integrationID)
  71. if err != nil {
  72. if errors.Is(err, gorm.ErrRecordNotFound) {
  73. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unauthorized gitlab user"), http.StatusUnauthorized))
  74. return
  75. }
  76. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  77. return
  78. }
  79. accessToken, _, err := oauth.GetAccessToken(giAppOAuth.SharedOAuthModel, commonutils.GetGitlabOAuthConf(
  80. p.Config(), gi,
  81. ), oauth.MakeUpdateGitlabAppOAuthIntegrationFunction(giAppOAuth, p.Repo()))
  82. if err != nil {
  83. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("invalid gitlab access token"),
  84. http.StatusUnauthorized))
  85. return
  86. }
  87. client, err := gitlab.NewOAuthClient(accessToken, gitlab.WithBaseURL(gi.InstanceURL))
  88. if err != nil {
  89. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  90. return
  91. }
  92. file, resp, err := client.RepositoryFiles.GetRawFile(fmt.Sprintf("%s/%s", owner, name), request.Path,
  93. &gitlab.GetRawFileOptions{
  94. Ref: gitlab.String(branch),
  95. },
  96. )
  97. if resp.StatusCode == http.StatusUnauthorized {
  98. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unauthorized gitlab user"), http.StatusUnauthorized))
  99. return
  100. } else if resp.StatusCode == http.StatusNotFound {
  101. w.WriteHeader(http.StatusNotFound)
  102. p.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("no such procfile exists")))
  103. return
  104. }
  105. if err != nil {
  106. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  107. return
  108. }
  109. parsedContents := make(types.GetProcfileResponse)
  110. // parse the procfile information
  111. for _, line := range strings.Split(string(file), "\n") {
  112. if matches := procfileRegex.FindStringSubmatch(line); matches != nil {
  113. parsedContents[matches[1]] = matches[2]
  114. }
  115. }
  116. p.WriteResult(w, r, parsedContents)
  117. }