2
0

get_contents.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. telemetry.WithAttributes(
  55. span,
  56. telemetry.AttributeKV{Key: "repo-owner", Value: owner},
  57. telemetry.AttributeKV{Key: "repo-name", Value: name},
  58. telemetry.AttributeKV{Key: "repo-branch", Value: branch},
  59. )
  60. repoContentOptions := github.RepositoryContentGetOptions{}
  61. repoContentOptions.Ref = branch
  62. _, directoryContents, resp, err := client.Repositories.GetContents(
  63. ctx,
  64. owner,
  65. name,
  66. request.Dir,
  67. &repoContentOptions,
  68. )
  69. if err != nil {
  70. err = telemetry.Error(ctx, span, err, "could not get contents from github")
  71. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, resp.StatusCode))
  72. return
  73. }
  74. res := make(types.GetContentsResponse, 0)
  75. for i := range directoryContents {
  76. res = append(res, types.GithubDirectoryItem{
  77. Path: *directoryContents[i].Path,
  78. Type: *directoryContents[i].Type,
  79. })
  80. }
  81. c.WriteResult(w, r, res)
  82. }