diff --git a/internal/app/app.go b/internal/app/app.go index fb616e0..843220d 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -9,6 +9,7 @@ import ( "os" "os/signal" "path/filepath" + "strings" "syscall" "time" @@ -302,21 +303,31 @@ func (a App) sbomsFromRepositoryInternal(ctx context.Context, repositoryURL stri }) if errors.Is(err, context.Canceled) { return - } else if err != nil { - // If error is not null, we try to get new token and assign it to github API token + } + + if err != nil { + if strings.Contains(err.Error(), "HEAD reference not found") { + log.WithError(err).Errorf("returning with error head not found %s", err.Error()) + return + } + log.WithError(err).Errorf("can't clone %s", repositoryURL) + token, errToken := internal.RegenerateGithubToken(a.organization) if errToken != nil { - log.WithError(errToken).Errorf("can't generate github token") + log.WithError(errToken).Error("can't generate github token") + return } + a.githubAPIToken = token repo, err = repository.New(ctx, repositoryURL, repository.Credentials{ Username: a.githubUsername, AccessToken: a.githubAPIToken, }) - // If err is still here after we attempt to regen, return + if err != nil { log.WithError(err).Errorf("could not fetch after regenerated token %s", repositoryURL) + return } } diff --git a/internal/requests.go b/internal/requests.go index 39abced..62307d3 100644 --- a/internal/requests.go +++ b/internal/requests.go @@ -152,6 +152,7 @@ func WalkRepositories(conf GetRepositoriesConfig, callback func(repositoryURLs [ var repositories []repositoryMapping var err error regenCount := 0 + repositoriesLen := 0 endpoint, err := url.Parse(conf.URL) if err != nil { @@ -166,6 +167,7 @@ func WalkRepositories(conf GetRepositoriesConfig, callback func(repositoryURLs [ conf.URL = endpoint.String() log.WithField("request github", endpoint.String()).Infof("Getting query for page %d", page) repositories, err = GetRepositories(conf) + repositoriesLen += len(repositories) if err != nil { if regenCount < 1 { token, errToken := RegenerateGithubToken(conf.Organization) @@ -205,9 +207,10 @@ func WalkRepositories(conf GetRepositoriesConfig, callback func(repositoryURLs [ } var repositoryURLs []string - for _, r := range repositories { + for _, r := range validRepositories { repositoryURLs = append(repositoryURLs, r.URL) } + log.Infof("total repository count scanned %d", repositoriesLen) callback(repositoryURLs, conf.APIToken) // reset regen count page++ diff --git a/pkg/dtrack/http.go b/pkg/dtrack/http.go index a63a9d0..baa18c7 100644 --- a/pkg/dtrack/http.go +++ b/pkg/dtrack/http.go @@ -129,8 +129,7 @@ func (d DependencyTrackClient) updateSBOMs(ctx context.Context, payload updateSB if err != nil { return fmt.Errorf(cantPerformHTTPRequest, requestURL, err) } - log.WithField("funcType", "updateSBOM").Debugf("CreateProject request response body: %s", resp.Body) - log.WithField("funcType", "updateSBOM").Debugf("CreateProject request response status code: %v", resp.StatusCode) + log.WithField("funcType", "updateSBOM").Debugf("Update project request response status code: %v", resp.StatusCode) defer func() { closeErr := resp.Body.Close() @@ -149,7 +148,7 @@ func (d DependencyTrackClient) updateSBOMs(ctx context.Context, payload updateSB log.WithField("updateNotOk", resp.StatusCode).Debugf("Update SBOM response code ( %v ) != 200: %s", resp, err) return err } - log.WithField("funcType", "updateSBOM").Debugf("SBOM Update finished: %s", err) + log.WithField("funcType", "updateSBOM").Debugf("SBOM Update finished: %d", resp.StatusCode) return err } diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go index bd582dd..bc22a9c 100644 --- a/pkg/repository/repository.go +++ b/pkg/repository/repository.go @@ -74,14 +74,22 @@ func getHeadReference(vcsURL string, credentials Credentials) (plumbing.Referenc return "", err } - return refs["HEAD"].Target(), nil + headRef, exists := refs["HEAD"] + if !exists { + return "", fmt.Errorf("HEAD reference not found") + } + + target := headRef.Target() + if target == "" { + return "", fmt.Errorf("HEAD reference has no target") + } + + return target, nil } unauthenticatedHEAD, err := obtainHEADRef(endpoint) if err != nil { - const warnMessage = "unable to obtain repo HEAD in an unauthenticated state, retrying with credentials" - log.WithField("error", err).Warn(warnMessage) - + log.Infof("unable to obtain repo unauthenticated head, %s", err.Error()) endpoint.User = credentials.Username endpoint.Password = credentials.AccessToken @@ -104,7 +112,7 @@ func New(ctx context.Context, vcsURL string, credentials Credentials) (*Reposito name := strings.TrimSuffix(urlPaths[len(urlPaths)-1], ".git") fsPath := filepath.Join(CheckoutsPath, name) - const cloneDepth = 100 // Clone only 100 most recent commits, this saves bandwidth & disk-space + const cloneDepth = 40 // Clone only 100 most recent commits, this saves bandwidth & disk-space headReference, err := getHeadReference(vcsURL, credentials) if err != nil { return nil, err