Kaynağa Gözat

get branch head commit sha (#3421)

ianedwards 2 yıl önce
ebeveyn
işleme
356043e521

+ 91 - 0
api/server/handlers/gitinstallation/get_branch_head.go

@@ -0,0 +1,91 @@
+package gitinstallation
+
+import (
+	"net/http"
+
+	"github.com/porter-dev/porter/api/server/handlers"
+	"github.com/porter-dev/porter/api/server/shared"
+	"github.com/porter-dev/porter/api/server/shared/apierrors"
+	"github.com/porter-dev/porter/api/server/shared/commonutils"
+	"github.com/porter-dev/porter/api/server/shared/config"
+	"github.com/porter-dev/porter/internal/telemetry"
+)
+
+// GetBranchHeadHandler is the handler for the /{branch}/head endpoint
+type GetBranchHeadHandler struct {
+	handlers.PorterHandlerReadWriter
+}
+
+// NewGetBranchHeadHandler handles GET requests to /{branch}/head
+func NewGetBranchHeadHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *GetBranchHeadHandler {
+	return &GetBranchHeadHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+	}
+}
+
+// GetBranchHeadResponse is the response object for the /{branch}/head endpoint
+type GetBranchHeadResponse struct {
+	CommitSHA string `json:"commit_sha"`
+}
+
+// ServeHTTP retrieves the head commit sha for a branch
+func (c *GetBranchHeadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	ctx, span := telemetry.NewSpan(r.Context(), "serve-get-branch-head")
+	defer span.End()
+
+	owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
+
+	if !ok {
+		err := telemetry.Error(ctx, span, nil, "could not get owner and name from request")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
+		return
+	}
+
+	telemetry.WithAttributes(span,
+		telemetry.AttributeKV{Key: "owner", Value: owner},
+		telemetry.AttributeKV{Key: "name", Value: name},
+	)
+
+	branchName, ok := commonutils.GetBranchParam(c, w, r)
+	if !ok {
+		err := telemetry.Error(ctx, span, nil, "unable to get branch name")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
+		return
+	}
+
+	client, err := GetGithubAppClientFromRequest(c.Config(), r)
+	if err != nil {
+		err = telemetry.Error(ctx, span, err, "could not get github app client")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
+		return
+	}
+
+	branch, _, err := client.Repositories.GetBranch(ctx, owner, name, branchName, true)
+	if err != nil {
+		err = telemetry.Error(ctx, span, err, "could not get branch")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
+		return
+	}
+
+	if branch == nil {
+		err = telemetry.Error(ctx, span, nil, "branch does not exist")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusNotFound))
+		return
+	}
+
+	if branch.Commit == nil || branch.Commit.SHA == nil {
+		err = telemetry.Error(ctx, span, nil, "branch head does not exist")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusNotFound))
+		return
+	}
+
+	response := &GetBranchHeadResponse{
+		CommitSHA: *branch.Commit.SHA,
+	}
+
+	c.WriteResult(w, r, response)
+}

+ 37 - 0
api/server/router/git_installation.go

@@ -625,6 +625,43 @@ func getGitInstallationRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/head ->
+	// gitinstallation.NewGetBranchHeadHandler
+	getBranchHeadEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent: basePath,
+				RelativePath: fmt.Sprintf(
+					"%s/repos/{%s}/{%s}/{%s}/{%s}/head",
+					relPath,
+					types.URLParamGitKind,
+					types.URLParamGitRepoOwner,
+					types.URLParamGitRepoName,
+					types.URLParamGitBranch,
+				),
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+				types.GitInstallationScope,
+			},
+		},
+	)
+
+	getBranchHeadHandler := gitinstallation.NewGetBranchHeadHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &router.Route{
+		Endpoint: getBranchHeadEndpoint,
+		Handler:  getBranchHeadHandler,
+		Router:   r,
+	})
+
 	// GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile ->
 	// gitinstallation.NewGithubGetProcfileHandler
 	getProcfileEndpoint := factory.NewAPIEndpoint(