Skip to content

Commit

Permalink
feat: adding branch scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
aschaef19 committed Nov 3, 2021
1 parent 259f6f5 commit f1f2710
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Usage of go-earlybird:
Output format [ console | json | csv ] (default "console")
-git string
Full URL to a git repo to scan e.g. github.com/user/repo
-git-branch string
Name of branch to be scanned
-git-commit-stream
Use stream IO of Git commit log as input instead of file(s) -- e.g., 'cat secrets.text > go-earlybird'
-git-project string
Expand Down
1 change: 1 addition & 0 deletions go-earlybird.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func main() {
gitcfg.Project = flag.String("git-project", "", "Full URL to a github organization to scan e.g. github.com/org")
gitcfg.Repo = flag.String("git", "", "Full URL to a git repo to scan e.g. github.com/user/repo")
gitcfg.RepoUser = flag.String("git-user", os.Getenv("gituser"), "If the git repository is private, enter an authorized username")
gitcfg.RepoBranch = flag.String("git-branch", "", "Name of branch to be scanned")

//Load CLI params and Earlybird config
eb.ConfigInit()
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ func GITScan(cfg cfgreader.EarlybirdConfig) http.HandlerFunc {
}

giturl := giturls[0]
gitbranch := r.URL.Query().Get("branch")
utils.GetGitURL(&giturl, &blank)
mycfg.SearchDir, err = git.CloneGitRepos([]string{giturl}, os.Getenv("gituser"), os.Getenv("gitpassword"), (cfg.OutputFormat == "json"))
mycfg.SearchDir, err = git.CloneGitRepos([]string{giturl}, os.Getenv("gituser"), os.Getenv("gitpassword"), gitbranch, (cfg.OutputFormat == "json"))
if err != nil {
if err == transport.ErrAuthenticationRequired {
http.Error(w, "Failed to clone, repository is private. Please enter a public repository URL.", http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func (eb *EarlybirdCfg) GitClone(ptr PTRGitConfig) {
}
var err error
if *ptr.RepoUser != "" { // use auth
eb.Config.SearchDir, err = git.CloneGitRepos(scanRepos, *ptr.RepoUser, gitPassword, (eb.Config.OutputFormat == "json"))
eb.Config.SearchDir, err = git.CloneGitRepos(scanRepos, *ptr.RepoUser, gitPassword, *ptr.RepoBranch, (eb.Config.OutputFormat == "json"))
} else {
eb.Config.SearchDir, err = git.CloneGitRepos(scanRepos, "", "", (eb.Config.OutputFormat == "json")) //Blank no auth
eb.Config.SearchDir, err = git.CloneGitRepos(scanRepos, "", "", *ptr.RepoBranch, (eb.Config.OutputFormat == "json")) //Blank no auth
}
if err != nil {
log.Println("Failed to clone repository:", err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/core/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ type PTRHTTPConfig struct {

//PTRGitConfig is the configuration definition for Earlybird git scans
type PTRGitConfig struct {
Repo *string
RepoUser *string
Project *string
Repo *string
RepoUser *string
RepoBranch *string
Project *string
}
11 changes: 10 additions & 1 deletion pkg/git/cloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package git

import (
"context"
"fmt"
"gopkg.in/src-d/go-git.v4/plumbing"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -76,7 +78,7 @@ func ReposPerProject(projectURL, username, password string) (scanRepos []string)
}

//CloneGitRepos Clones a Git repo into a random temporary folder
func CloneGitRepos(repoURLs []string, username, password string, json bool) (tmpDir string, err error) {
func CloneGitRepos(repoURLs []string, username, password string, branch string, json bool) (tmpDir string, err error) {
tmpDir, err = ioutil.TempDir("", "ebgit")
if err != nil {
return "", err
Expand All @@ -96,8 +98,15 @@ func CloneGitRepos(repoURLs []string, username, password string, json bool) (tmp
options.Auth = auth
}

if branch != "" {
options.ReferenceName = plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch))
}

if !json {
log.Println("Cloning Repository:", repo)
if branch != "" {
log.Println("Cloning Branch:", branch)
}
options.Progress = os.Stdout
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/git/cloner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestCloneGitRepos(t *testing.T) {
t.Skip("If test cases not running locally, skip cloning external repositories for CI/CD purposes.")
}

SearchDir, err := CloneGitRepos([]string{FakeRepo}, "", "", true)
SearchDir, err := CloneGitRepos([]string{FakeRepo}, "", "", "", true)
if err != nil {
t.Errorf("Failed to clone repository: %s", FakeRepo)
}
Expand Down

0 comments on commit f1f2710

Please sign in to comment.