Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repo crawling #140

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions internal/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type GetRepositoriesConfig struct {
}

func NewGetRepositoriesConfig(ctx context.Context, url, username, apiToken string, org string) GetRepositoriesConfig {
if org != "" {
apiToken = gh.GenerateGithubAppTokenInternal(org)
}

return GetRepositoriesConfig{
ctx: ctx,
URL: url,
Expand Down Expand Up @@ -139,15 +143,8 @@ func GetRepositories(conf GetRepositoriesConfig) ([]repositoryMapping, error) {
return repositories, nil
}

var validRepositories []repositoryMapping
for _, r := range repositories {
if !r.Archived {
validRepositories = append(validRepositories, r)
}
}
return validRepositories, nil
return repositories, nil
}

return exponentialBackoff(getRepositories, conf.BackoffPolicy...)
}

Expand All @@ -167,7 +164,7 @@ func WalkRepositories(conf GetRepositoriesConfig, callback func(repositoryURLs [
query.Set("page", strconv.Itoa(page))
endpoint.RawQuery = query.Encode()
conf.URL = endpoint.String()

log.WithField("request github", endpoint.String()).Infof("Getting query for page %d", page)
repositories, err = GetRepositories(conf)
if err != nil {
if regenCount < 1 {
Expand All @@ -192,7 +189,18 @@ func WalkRepositories(conf GetRepositoriesConfig, callback func(repositoryURLs [
regenCount = 0
}

if len(repositories) == 0 {
var validRepositories []repositoryMapping
var archivedRepositories []repositoryMapping
for _, r := range repositories {
if !r.Archived {
validRepositories = append(validRepositories, r)
} else if r.Archived {
archivedRepositories = append(archivedRepositories, r)
}
}

if len(validRepositories) == 0 && len(archivedRepositories) == 0 {
log.WithField("request github return", endpoint.String()).Infof("returning with page %d", page)
return nil // Done, all repositories have been walked
}

Expand Down
1 change: 1 addition & 0 deletions internal/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestGetRepositories(t *testing.T) {

want := []repositoryMapping{
{Name: "xmlsec", Archived: false, Language: "C", URL: "https://github.com/vinted/xmlsec"},
{Name: "airbrake", Archived: true, Language: "Ruby", URL: "https://github.com/vinted/airbrake-graylog2"},
{Name: "dotpay", Archived: false, Language: "Ruby", URL: "https://github.com/vinted/dotpay"},
}
assert.Equal(t, want, repositories)
Expand Down
13 changes: 11 additions & 2 deletions pkg/bomtools/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,17 @@ func mergeAllByPURL(component *cdx.Component, allComponents []*cdx.Component) *c
mergedComponent.Properties = &p
}
if c.Licenses != nil {
l := mergeCollection[cdx.LicenseChoice](*c.Licenses, *mergedComponent.Licenses)
mergedComponent.Licenses = (*cdx.Licenses)(&l)
l := make([]cdx.LicenseChoice, 0)
for _, sl := range *c.Licenses {
// Check for license ID
if sl.License != nil && sl.License.ID != "" {
l = append(l, sl)
}
}

// Assuming mergedComponent.Licenses is initialized properly earlier
mergedLicenses := mergeCollection[cdx.LicenseChoice](l, *mergedComponent.Licenses)
mergedComponent.Licenses = (*cdx.Licenses)(&mergedLicenses)
}
if c.ExternalReferences != nil {
e := mergeCollection[cdx.ExternalReference](*c.ExternalReferences, *mergedComponent.ExternalReferences)
Expand Down
9 changes: 5 additions & 4 deletions pkg/dtrack/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ func (d DependencyTrackClient) createProject(ctx context.Context, payload create
d.setRequiredHeaders(req)

resp, err := http.DefaultClient.Do(req)
log.WithField("funcType", "createProject").Debugf("CreateProject request response body: %s", resp.Body)
log.WithField("funcType", "createProject").Debugf("CreateProject request response status code: %v", resp.StatusCode)
if err != nil {
log.WithField("funcType", "createProject").Debugf("CreateProject error %v", err.Error())
return "", fmt.Errorf(cantPerformHTTPRequest, requestURL, err)
}
// putting err here incase we get a resp nil
log.WithField("funcType", "createProject").Debugf("CreateProject request response status code: %v", resp.StatusCode)

defer func() {
closeErr := resp.Body.Close()
Expand Down Expand Up @@ -125,11 +126,11 @@ func (d DependencyTrackClient) updateSBOMs(ctx context.Context, payload updateSB
d.setRequiredHeaders(req)

resp, err := http.DefaultClient.Do(req)
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)
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)

defer func() {
closeErr := resp.Body.Close()
Expand Down
Loading