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

feat: Handling pagination of repository vulnerabilities #85

Merged
merged 9 commits into from
Oct 5, 2023
Merged
Changes from 6 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
62 changes: 43 additions & 19 deletions querying/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@
}
}

type githubVulnerability struct {
SecurityAdvisory struct {
Description string
Identifiers []struct {
Type string
Value string
}
}
SecurityVulnerability struct {
Severity string
Package struct {
Ecosystem string
Name string
}
}
}

type orgRepo struct {
Name string
Url string
Expand All @@ -46,25 +63,14 @@
EndCursor githubv4.String
HasNextPage bool
}
Nodes []struct {
SecurityAdvisory struct {
Description string
Identifiers []struct {
Type string
Value string
}
}
SecurityVulnerability struct {
Severity string
Package struct {
Ecosystem string
Name string
}
}
}
Nodes []githubVulnerability
} `graphql:"vulnerabilityAlerts(states: OPEN, first: 100, after: $alertCursor)"`
}

type repositoryQuery struct {
Repository orgRepo `graphql:"repository(name: $repoName, owner: $orgName)"`
}

type orgVulnerabilityQuery struct {
Organization struct {
Name string
Expand Down Expand Up @@ -122,7 +128,7 @@
return err
}
for _, repo := range alertQuery.Organization.Repositories.Nodes {
err := gh.processRepoFindings(projects, repo, queryVars["repoCursor"].(*githubv4.String))
err := gh.processRepoFindings(projects, repo, repo.VulnerabilityAlerts.PageInfo.EndCursor)
if err != nil {
log.Warn().Err(err).Str("repository", repo.Name).Msg("Failed to process findings for repository.")
}
Expand All @@ -137,12 +143,12 @@
return nil
}

func (gh *GithubDataSource) processRepoFindings(projects *ProjectCollection, repo orgRepo, repoCursor *githubv4.String) error {
func (gh *GithubDataSource) processRepoFindings(projects *ProjectCollection, repo orgRepo, endCursor githubv4.String) error {
JoseAngel1196 marked this conversation as resolved.
Show resolved Hide resolved
log := logger.Get()
project := projects.GetProject(repo.Name)
project.Links["GitHub"] = repo.Url
log.Debug().Str("project", project.Name).Msg("Processing findings for project.")
// TODO: Handle pagination of vulnerabilityAlerts

for _, vuln := range repo.VulnerabilityAlerts.Nodes {
identifiers := FindingIdentifierMap{}
for _, id := range vuln.SecurityAdvisory.Identifiers {
Expand All @@ -167,6 +173,24 @@
finding.Severity = githubSeverities[vuln.SecurityVulnerability.Severity]
}()
}

if repo.VulnerabilityAlerts.PageInfo.HasNextPage {
var repositoryQuery repositoryQuery
JoseAngel1196 marked this conversation as resolved.
Show resolved Hide resolved
queryVars := map[string]interface{}{
"repoName": githubv4.String(repo.Name),
"orgName": githubv4.String(gh.orgName),
"alertCursor": githubv4.String(endCursor),
}
err := gh.GhClient.Query(gh.ctx, &repositoryQuery, queryVars)
if err != nil {
return err
}

Check warning on line 187 in querying/github.go

View check run for this annotation

Codecov / codecov/patch

querying/github.go#L178-L187

Added lines #L178 - L187 were not covered by tests

nextCursor := repositoryQuery.Repository.VulnerabilityAlerts.PageInfo.EndCursor
log.Info().Any("alertCursor", queryVars["alertCursor"]).Msg("Querying for more vulnerabilities for a repository.")
JoseAngel1196 marked this conversation as resolved.
Show resolved Hide resolved
return gh.processRepoFindings(projects, repositoryQuery.Repository, nextCursor)

Check warning on line 191 in querying/github.go

View check run for this annotation

Codecov / codecov/patch

querying/github.go#L189-L191

Added lines #L189 - L191 were not covered by tests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have verified this locally, will add some unit test for this in a follow up PR.

}

return nil
}

Expand Down