Skip to content

Commit

Permalink
refactor: update some function and parameter names
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr committed May 25, 2020
1 parent d14738e commit d8779ee
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions pkg/proji/repo/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
}

Expand Down Expand Up @@ -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)
Expand All @@ -109,13 +115,16 @@ 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
treeResponse := gjson.GetMany(string(body), "tree.#.path", "tree.#.type")
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
}

0 comments on commit d8779ee

Please sign in to comment.