diff --git a/pkg/proji/repo/github/github.go b/pkg/proji/repo/github/github.go index 1ac97c59..335368a0 100644 --- a/pkg/proji/repo/github/github.go +++ b/pkg/proji/repo/github/github.go @@ -41,16 +41,16 @@ func (g *github) setRepoSHA() error { } // New creates a new github repo object -func New(repoURLPath string) (repo.Importer, error) { +func New(URL string) (repo.Importer, error) { // Parse URL // Examples: - // - https://github.com/[nikoksr]/[proji] -> extracts user and repo name; no branch name - // - https://github.com/[nikoksr]/[proji]/tree/[master] -> extracts user, repo and branch name + // - [https://github.com/[nikoksr]/[proji]] -> extracts base uri, user and repo name; no branch name + // - [https://github.com/[nikoksr]/[proji]]/tree/[master] -> extracts base uri, user, repo and branch name r := regexp.MustCompile(`((?:(?:http|https)://)?github.com/([^/]+)/([^/]+))(?:/tree/([^/]+))?`) specs := r.FindStringSubmatch(URL) if specs == nil || len(specs) < 5 { - return nil, fmt.Errorf("could not parse url path") + return nil, fmt.Errorf("could not parse url") } baseURI := specs[1] @@ -67,7 +67,14 @@ func New(repoURLPath string) (repo.Importer, error) { branchName = "master" } - g := &github{apiBaseURI: "https://api.github.com/repos/", userName: userName, repoName: repoName, branchName: branchName, repoSHA: ""} + g := &github{ + baseURI: baseURI, + apiBaseURI: "https://api.github.com/repos/", + userName: userName, + repoName: repoName, + branchName: branchName, + repoSHA: "", + } return g, g.setRepoSHA() } @@ -96,9 +103,8 @@ func (g *github) GetRepoName() string { return g.repoName } // GetBranchName returns the branch name func (g *github) GetBranchName() string { return g.branchName } - -// GetTreePathsAndTypes gets the paths and types of the repo tree -func (g *github) GetTreePathsAndTypes() ([]gjson.Result, []gjson.Result, error) { +// GetTree gets the paths and types of the repo tree +func (g *github) GetTree(filters []*regexp.Regexp) ([]gjson.Result, []gjson.Result, error) { // Request repo tree treeReq := g.apiBaseURI + g.userName + "/" + g.repoName + "/git/trees/" + g.repoSHA + "?recursive=1" response, err := repo.GetRequest(treeReq) @@ -109,7 +115,7 @@ func (g *github) GetTreePathsAndTypes() ([]gjson.Result, []gjson.Result, error) // Check if response was truncated if gjson.Get(string(body), "truncated").Bool() == true { - return nil, nil, fmt.Errorf("the response was truncated by Github, which means that the number of items in the tree array exceeded the maximum limit.\n\nClone the repo manually with git and use 'proji class import --directory /path/to/repo' to import the local instance of that repo") + return nil, nil, fmt.Errorf("the response was truncated by Github, which means that the number of items in the tree array exceeded the maximum limit.\n\nClone the repo manually with git and use 'proji class import --directory /path/to/repo' to import your local copy of that repo") } // Parse the tree @@ -117,5 +123,8 @@ func (g *github) GetTreePathsAndTypes() ([]gjson.Result, []gjson.Result, error) defer response.Body.Close() paths := treeResponse[0].Array() types := treeResponse[1].Array() + + // Filter paths + paths, types = repo.FilterPathsNTypes(paths, types, filters) return paths, types, nil }