get_contents.go 3.0 KB

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