Alexander Belanger 4 лет назад
Родитель
Сommit
7770b40468

+ 86 - 0
api/server/handlers/gitinstallation/get_contents.go

@@ -0,0 +1,86 @@
+package gitinstallation
+
+import (
+	"context"
+	"net/http"
+
+	"github.com/google/go-github/github"
+	"github.com/porter-dev/porter/api/server/authz"
+	"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/config"
+	"github.com/porter-dev/porter/api/server/shared/requestutils"
+	"github.com/porter-dev/porter/api/types"
+)
+
+type GithubGetContentsHandler struct {
+	handlers.PorterHandlerReadWriter
+	authz.KubernetesAgentGetter
+}
+
+func NewGithubGetContentsHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *GithubGetContentsHandler {
+	return &GithubGetContentsHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+	}
+}
+
+func (c *GithubGetContentsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	request := &types.GetContentsRequest{}
+
+	ok := c.DecodeAndValidate(w, r, request)
+
+	if !ok {
+		return
+	}
+
+	owner, name, ok := GetOwnerAndNameParams(c, w, r)
+
+	if !ok {
+		return
+	}
+
+	branch, reqErr := requestutils.GetURLParamString(r, types.URLParamGitBranch)
+
+	if reqErr != nil {
+		c.HandleAPIError(w, r, reqErr)
+		return
+	}
+
+	client, err := GetGithubAppClientFromRequest(c.Config(), r)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	repoContentOptions := github.RepositoryContentGetOptions{}
+	repoContentOptions.Ref = branch
+	_, directoryContents, _, err := client.Repositories.GetContents(
+		context.Background(),
+		owner,
+		name,
+		request.Dir,
+		&repoContentOptions,
+	)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	res := make(types.GetContentsResponse, 0)
+
+	for i := range directoryContents {
+		res = append(res, types.GithubDirectoryItem{
+			Path: *directoryContents[i].Path,
+			Type: *directoryContents[i].Type,
+		})
+	}
+
+	c.WriteResult(w, r, res)
+}

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

@@ -183,5 +183,42 @@ func getGitInstallationRoutes(
 		Router:   r,
 	})
 
+	//   GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents ->
+	// gitinstallation.NewGithubGetContentsHandler
+	getContentsEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent: basePath,
+				RelativePath: fmt.Sprintf(
+					"%s/repos/{%s}/{%s}/{%s}/{%s}/contents",
+					relPath,
+					types.URLParamGitKind,
+					types.URLParamGitRepoOwner,
+					types.URLParamGitRepoName,
+					types.URLParamGitBranch,
+				),
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+				types.GitInstallationScope,
+			},
+		},
+	)
+
+	getContentsHandler := gitinstallation.NewGithubGetContentsHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: getContentsEndpoint,
+		Handler:  getContentsHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 16 - 1
api/types/git_installation.go

@@ -31,11 +31,26 @@ const (
 
 type ListRepoBranchesResponse []string
 
-type GetBuildpackRequest struct {
+type GithubDirectoryRequest struct {
 	Dir string `schema:"dir" form:"required"`
 }
 
+type GetBuildpackRequest struct {
+	GithubDirectoryRequest
+}
+
 type GetBuildpackResponse struct {
 	Valid bool   `json:"valid"`
 	Name  string `json:"name"`
 }
+
+type GetContentsRequest struct {
+	GithubDirectoryRequest
+}
+
+type GithubDirectoryItem struct {
+	Path string `json:"path"`
+	Type string `json:"type"`
+}
+
+type GetContentsResponse []GithubDirectoryItem

+ 1 - 1
docs/developing/backend-refactor-status.md

@@ -54,7 +54,7 @@
 | <li>- [X] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos`                                                 | AB          |                 |             |                  |
 | <li>- [X] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/branches`                  | AB          |                 |             |                  |
 | <li>- [X] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/buildpack/detect` | AB          |                 |             |                  |
-| <li>- [ ] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents`         |             |                 |             |                  |
+| <li>- [X] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents`         | AB          |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile`         |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url`      |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/helmrepos`                                                                        |             |                 |             |                  |