Skip to content

Commit 2ea97d4

Browse files
authored
feat: allow the sync task to run for just a single gem (#527)
1 parent 54cce22 commit 2ea97d4

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,18 @@ The GitHub Advisory API requires a token to access it.
100100
- It can be a completely scopeless token (recommended); it does not require any permissions at all.
101101
- Get yours at https://github.com/settings/tokens
102102

103-
To run the GitHub Advisory sync, start by executing the rake task:
103+
To run the GitHub Advisory sync to retrieve all advisories, start by executing the rake task:
104+
104105
```
105106
GH_API_TOKEN=<your GitHub API Token> bundle exec rake sync_github_advisories
106107
```
107108

109+
Or, to only retrieve advisories for a single gem:
110+
111+
```
112+
GH_API_TOKEN=<your GitHub API Token> bundle exec rake sync_github_advisories[gem_name]
113+
```
114+
108115
- The rake task will write yaml files for any missing advisories.
109116
- Those files must be further edited.
110117
- Fill in `cvss_v3` field by following the CVE link and getting it from page

Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace :lint do
1313
end
1414

1515
desc "Sync GitHub RubyGem Advisories into this project"
16-
task :sync_github_advisories do
16+
task :sync_github_advisories, [:gem_name] do |_, args|
1717
require_relative "lib/github_advisory_sync"
18-
GitHub::GitHubAdvisorySync.sync
18+
GitHub::GitHubAdvisorySync.sync(gem_name: args[:gem_name])
1919
end
2020

2121
task :lint => ['lint:yaml']

lib/github_advisory_sync.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class GitHubAdvisorySync
1515
# The min_year argument specifies the earliest year CVE to sync.
1616
# It is more important to sync the newer ones, so this allows the user to
1717
# control how old of CVEs the sync should pull over
18-
def self.sync(min_year: 2015)
19-
gh_advisories = GraphQLAPIClient.new.all_rubygem_advisories
18+
def self.sync(min_year: 2015, gem_name: nil)
19+
gh_advisories = GraphQLAPIClient.new.all_rubygem_advisories(gem_name: gem_name)
2020

2121
# Filter out advisories with a CVE year that is before the min_year
2222
gh_advisories.select! { |v| v.cve_after_year?(min_year) }
@@ -89,10 +89,10 @@ def github_graphql_query(graphql_query_name, graphql_variables = {})
8989
body_obj
9090
end
9191

92-
def all_rubygem_advisories
92+
def all_rubygem_advisories(gem_name: nil)
9393
advisories = {}
9494

95-
retrieve_all_rubygem_vulnerabilities.each do |vulnerability|
95+
retrieve_all_rubygem_vulnerabilities(gem_name: gem_name).each do |vulnerability|
9696
advisory = GitHubAdvisory.new(vulnerability["advisory"])
9797

9898
next if advisory.withdrawn?
@@ -105,9 +105,9 @@ def all_rubygem_advisories
105105
advisories.values
106106
end
107107

108-
def retrieve_all_rubygem_vulnerabilities(max_pages = 1000, page_size = 100)
108+
def retrieve_all_rubygem_vulnerabilities(max_pages = 1000, page_size = 100, gem_name: nil)
109109
all_vulnerabilities = []
110-
variables = { "first" => page_size }
110+
variables = { "first" => page_size, "gem_name" => gem_name }
111111
max_pages.times do |page_num|
112112
puts "Getting page #{page_num + 1} of GitHub Vulnerabilities"
113113

@@ -126,8 +126,8 @@ def retrieve_all_rubygem_vulnerabilities(max_pages = 1000, page_size = 100)
126126

127127
module GraphQLQueries
128128
RUBYGEM_VULNERABILITIES_WITH_GITHUB_ADVISORIES = <<-GRAPHQL.freeze
129-
query($first: Int, $after: String) {
130-
securityVulnerabilities(first: $first, after: $after, ecosystem:RUBYGEMS) {
129+
query($first: Int, $after: String, $gem_name: String) {
130+
securityVulnerabilities(first: $first, after: $after, ecosystem:RUBYGEMS, package: $gem_name) {
131131
pageInfo {
132132
endCursor
133133
hasNextPage

0 commit comments

Comments
 (0)