Skip to content

Commit

Permalink
refactor: move setRepoSHA func to beginning of package
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr committed May 25, 2020
1 parent 354f680 commit c05c013
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions pkg/proji/repo/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ type github struct {
repoSHA string
}

// setRepoSHA sets the repoSHA attribute equal to the SHA-1 of the last commit in the current branch
func (g *github) setRepoSHA() error {
// Send request for SHA-1 of branch
shaReq := g.apiBaseURI + g.userName + "/" + g.repoName + "/branches/" + g.branchName
response, err := repo.GetRequest(shaReq)
if err != nil {
return err
}

// Parse body and try to extract SHA
body, _ := ioutil.ReadAll(response.Body)
repoSHA := gjson.Get(string(body), "commit.sha")
defer response.Body.Close()
if !repoSHA.Exists() {
return fmt.Errorf("could not get commit sha-1 from body")
}
g.repoSHA = repoSHA.String()
return nil
}

// New creates a new github repo object
func New(repoURLPath string) (repo.Importer, error) {
// Parse URL
Expand Down Expand Up @@ -58,25 +78,6 @@ func (g *github) GetRepoName() string { return g.repoName }
// GetBranchName returns the branch name
func (g *github) GetBranchName() string { return g.branchName }

// setRepoSHA sets the repoSHA attribute equal to the SHA-1 of the last commit in the current branch
func (g *github) setRepoSHA() error {
// Send request for SHA-1 of branch
shaReq := g.apiBaseURI + g.userName + "/" + g.repoName + "/branches/" + g.branchName
response, err := repo.GetRequest(shaReq)
if err != nil {
return err
}

// Parse body and try to extract SHA
body, _ := ioutil.ReadAll(response.Body)
repoSHA := gjson.Get(string(body), "commit.sha")
defer response.Body.Close()
if !repoSHA.Exists() {
return fmt.Errorf("could not get commit sha-1 from body")
}
g.repoSHA = repoSHA.String()
return nil
}

// GetTreePathsAndTypes gets the paths and types of the repo tree
func (g *github) GetTreePathsAndTypes() ([]gjson.Result, []gjson.Result, error) {
Expand Down

0 comments on commit c05c013

Please sign in to comment.