From 054c354f84fd069d74c1777b434e7e906dc66d93 Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Wed, 25 Nov 2026 10:50:05 -0500 Subject: [PATCH] Fix sync crash when no advisory is unchanged Array#compact! returns nil when it removes nothing, so GitHubAdvisorySync.sync raised NoMethodError on files_written.empty? whenever every advisory wrote a file (e.g. a single new advisory with a gem_name filter) or GitHub returned no advisories at all. Use compact and add specs for these cases. --- lib/github_advisory_sync.rb | 2 +- spec/github_advisory_sync_spec.rb | 39 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/github_advisory_sync.rb b/lib/github_advisory_sync.rb index c21130e6dd..294974507e 100644 --- a/lib/github_advisory_sync.rb +++ b/lib/github_advisory_sync.rb @@ -22,7 +22,7 @@ def self.sync(min_year: 2000, gem_name: nil) # Filter out advisories with a CVE year that is before the min_year gh_advisories.select! { |v| v.cve_after_year?(min_year) } - files_written = gh_advisories.filter_map(&:sync).flatten.compact! + files_written = gh_advisories.filter_map(&:sync).flatten.compact puts "\nSync completed" if files_written.empty? diff --git a/spec/github_advisory_sync_spec.rb b/spec/github_advisory_sync_spec.rb index 27d8063dee..30299a9c16 100644 --- a/spec/github_advisory_sync_spec.rb +++ b/spec/github_advisory_sync_spec.rb @@ -212,3 +212,42 @@ def generated(field, gem_name, lines) end end end + +RSpec.describe GitHub::GitHubAdvisorySync do + describe ".sync" do + def sync_with(results) + advisories = results.map do |files| + instance_double(GitHub::GitHubAdvisory, cve_after_year?: true, sync: files) + end + client = instance_double(GitHub::GraphQLAPIClient, all_rubygem_advisories: advisories) + allow(GitHub::GraphQLAPIClient).to receive(:new).and_return(client) + + silence_output { described_class.sync } + end + + def silence_output + original = $stdout + $stdout = StringIO.new + yield + ensure + $stdout = original + end + + it "returns the files written when every advisory changed a file" do + expect(sync_with([["gems/a/CVE-1.yml"], ["gems/b/CVE-2.yml"]])) + .to eq(["gems/a/CVE-1.yml", "gems/b/CVE-2.yml"]) + end + + it "ignores advisories that left their files unchanged" do + expect(sync_with([[nil], ["gems/b/CVE-2.yml"]])).to eq(["gems/b/CVE-2.yml"]) + end + + it "returns an empty list when nothing changed" do + expect(sync_with([[nil], [nil]])).to eq([]) + end + + it "returns an empty list when GitHub has no advisories" do + expect(sync_with([])).to eq([]) + end + end +end