get_contents.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package gitinstallation
  2. import (
  3. "net/http"
  4. "github.com/google/go-github/v41/github"
  5. "github.com/porter-dev/porter/api/server/authz"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/commonutils"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/telemetry"
  13. )
  14. type GithubGetContentsHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. authz.KubernetesAgentGetter
  17. }
  18. func NewGithubGetContentsHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *GithubGetContentsHandler {
  23. return &GithubGetContentsHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (c *GithubGetContentsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. ctx, span := telemetry.NewSpan(r.Context(), "serve-github-get-contents")
  29. defer span.End()
  30. request := &types.GetContentsRequest{}
  31. if ok := c.DecodeAndValidate(w, r, request); !ok {
  32. err := telemetry.Error(ctx, span, nil, "invalid request")
  33. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  34. return
  35. }
  36. owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
  37. if !ok {
  38. err := telemetry.Error(ctx, span, nil, "owner and name params not found")
  39. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  40. return
  41. }
  42. branch, ok := commonutils.GetBranchParam(c, w, r)
  43. if !ok {
  44. err := telemetry.Error(ctx, span, nil, "branch param not found")
  45. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  46. return
  47. }
  48. client, err := GetGithubAppClientFromRequest(c.Config(), r)
  49. if err != nil {
  50. err = telemetry.Error(ctx, span, err, "could not get github app client from request")
  51. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  52. return
  53. }
  54. if request.ForceDefaultBranch {
  55. repo, _, err := client.Repositories.Get(ctx, owner, name)
  56. if err != nil {
  57. err = telemetry.Error(ctx, span, err, "could not get repo")
  58. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  59. return
  60. }
  61. if repo == nil || repo.DefaultBranch == nil {
  62. err := telemetry.Error(ctx, span, nil, "repo or default branch not found")
  63. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  64. return
  65. }
  66. branch = *repo.DefaultBranch
  67. }
  68. telemetry.WithAttributes(
  69. span,
  70. telemetry.AttributeKV{Key: "repo-owner", Value: owner},
  71. telemetry.AttributeKV{Key: "repo-name", Value: name},
  72. telemetry.AttributeKV{Key: "repo-branch", Value: branch},
  73. telemetry.AttributeKV{Key: "repo-path", Value: request.Dir},
  74. )
  75. repoContentOptions := github.RepositoryContentGetOptions{}
  76. repoContentOptions.Ref = branch
  77. _, directoryContents, resp, err := client.Repositories.GetContents(
  78. ctx,
  79. owner,
  80. name,
  81. request.Dir,
  82. &repoContentOptions,
  83. )
  84. if err != nil {
  85. err = telemetry.Error(ctx, span, err, "could not get contents from github")
  86. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, resp.StatusCode))
  87. return
  88. }
  89. res := make(types.GetContentsResponse, 0)
  90. for i := range directoryContents {
  91. res = append(res, types.GithubDirectoryItem{
  92. Path: *directoryContents[i].Path,
  93. Type: *directoryContents[i].Type,
  94. })
  95. }
  96. c.WriteResult(w, r, res)
  97. }