Skip to content

Commit 44cfe9c

Browse files
committed
fixtest(github-ingestor): tests now pass and corrected CodeQL error
1 parent 1c3a0a6 commit 44cfe9c

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/ingestors/github_ingestor.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def to_github_api(url)
6767
uri = URI(url)
6868
return nil unless uri.host =~ /(\.|\A)(github\.com|github\.io)\Z/i
6969

70-
if uri.host.end_with?('github.io')
70+
if uri.host.include?('github.io')
7171
github_api_from_io(uri)
72-
elsif uri.host.end_with?('github.com')
72+
elsif uri.host.include?('github.com')
7373
github_api_from_com(uri)
7474
end
7575
end
@@ -93,8 +93,12 @@ def github_api_from_com(uri)
9393
# key: string key for the cache
9494
# ttl: time-to-live in seconds (default 7 days)
9595
def get_or_set_cache(key, url)
96+
content = open_url(url)
97+
return unless content
98+
99+
data = JSON.parse(content.read)
96100
Rails.cache.fetch(key, expires_in: TTL) do
97-
JSON.parse(open_url(url).read)
101+
data
98102
end
99103
rescue StandardError => e
100104
@messages << "#{self.class.name} get_or_set_cache failed for #{url}, #{e.message}"

test/unit/ingestors/github_ingestor_test.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ class GithubIngestorTest < ActiveSupport::TestCase # rubocop:disable Metrics/Cla
4343
# HTML
4444
html = File.read(Rails.root.join('test/fixtures/files/ingestion/github/mock.html'))
4545
@doc = Nokogiri::HTML(html)
46+
47+
# config cache working here
48+
@old_cache_store = Rails.cache
49+
Rails.cache = ActiveSupport::Cache::MemoryStore.new
4650
end
4751

4852
teardown do
4953
reset_timezone
50-
Rails.cache.clear
54+
Rails.cache = @old_cache_store
5155
end
5256

5357
test 'should read sitemap.txt composed of github.com and github.io but avoid keeping the non-github URLs' do
@@ -120,8 +124,6 @@ class GithubIngestorTest < ActiveSupport::TestCase # rubocop:disable Metrics/Cla
120124

121125
test 'should cache github repo metadata' do
122126
cache = 'github_ingestor_api.github.com_repos_hsf-training_cpluspluscourse'
123-
# Clear it before test to avoid false positives
124-
Rails.cache.delete(cache)
125127

126128
@ingestor.read('https://github.com/hsf-training/cpluspluscourse')
127129

@@ -341,4 +343,4 @@ def webmock(url, filename)
341343
file = Rails.root.join('test', 'fixtures', 'files', 'ingestion', filename)
342344
WebMock.stub_request(:get, url).to_return(status: 200, headers: {}, body: file.read)
343345
end
344-
end
346+
end

0 commit comments

Comments
 (0)