get_branch_head.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package gitinstallation
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/commonutils"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/internal/telemetry"
  10. )
  11. // GetBranchHeadHandler is the handler for the /{branch}/head endpoint
  12. type GetBranchHeadHandler struct {
  13. handlers.PorterHandlerReadWriter
  14. }
  15. // NewGetBranchHeadHandler handles GET requests to /{branch}/head
  16. func NewGetBranchHeadHandler(
  17. config *config.Config,
  18. decoderValidator shared.RequestDecoderValidator,
  19. writer shared.ResultWriter,
  20. ) *GetBranchHeadHandler {
  21. return &GetBranchHeadHandler{
  22. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  23. }
  24. }
  25. // GetBranchHeadResponse is the response object for the /{branch}/head endpoint
  26. type GetBranchHeadResponse struct {
  27. CommitSHA string `json:"commit_sha"`
  28. }
  29. // ServeHTTP retrieves the head commit sha for a branch
  30. func (c *GetBranchHeadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. ctx, span := telemetry.NewSpan(r.Context(), "serve-get-branch-head")
  32. defer span.End()
  33. owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
  34. if !ok {
  35. err := telemetry.Error(ctx, span, nil, "could not get owner and name from request")
  36. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  37. return
  38. }
  39. telemetry.WithAttributes(span,
  40. telemetry.AttributeKV{Key: "owner", Value: owner},
  41. telemetry.AttributeKV{Key: "name", Value: name},
  42. )
  43. branchName, ok := commonutils.GetBranchParam(c, w, r)
  44. if !ok {
  45. err := telemetry.Error(ctx, span, nil, "unable to get branch name")
  46. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  47. return
  48. }
  49. client, err := GetGithubAppClientFromRequest(c.Config(), r)
  50. if err != nil {
  51. err = telemetry.Error(ctx, span, err, "could not get github app client")
  52. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  53. return
  54. }
  55. branch, _, err := client.Repositories.GetBranch(ctx, owner, name, branchName, true)
  56. if err != nil {
  57. err = telemetry.Error(ctx, span, err, "could not get branch")
  58. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  59. return
  60. }
  61. if branch == nil {
  62. err = telemetry.Error(ctx, span, nil, "branch does not exist")
  63. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusNotFound))
  64. return
  65. }
  66. if branch.Commit == nil || branch.Commit.SHA == nil {
  67. err = telemetry.Error(ctx, span, nil, "branch head does not exist")
  68. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusNotFound))
  69. return
  70. }
  71. response := &GetBranchHeadResponse{
  72. CommitSHA: *branch.Commit.SHA,
  73. }
  74. c.WriteResult(w, r, response)
  75. }