Skip to content

Commit

Permalink
Fix git head ref crash
Browse files Browse the repository at this point in the history
  • Loading branch information
ysebyy committed Aug 14, 2024
1 parent 694eef3 commit 75fe9ca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
19 changes: 15 additions & 4 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -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

Check failure on line 327 in internal/app/app.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
if err != nil {
log.WithError(err).Errorf("could not fetch after regenerated token %s", repositoryURL)
return
}
}

Expand Down
5 changes: 4 additions & 1 deletion internal/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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++
Expand Down
5 changes: 2 additions & 3 deletions pkg/dtrack/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 75fe9ca

Please sign in to comment.