Skip to content

Commit 583618a

Browse files
committed
review(#1154): answering to 20/11/2025 requested changes
1 parent 1979543 commit 583618a

3 files changed

Lines changed: 20 additions & 23 deletions

File tree

lib/ingestors/github_ingestor.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class GithubIngestor < Ingestor # rubocop:disable Metrics/ClassLength
2323

2424
GITHUB_API_BASE = 'https://api.github.com/repos'
2525
CACHE_PREFIX = 'github_ingestor_'
26-
TTL = 1.week # time to live after the cache is deleted
26+
TTL = 1.week # cache expiration time (time to live before cache expires)
2727

2828
def self.config
2929
{
@@ -57,7 +57,7 @@ def read(source_url)
5757
add_material to_material(repo_data)
5858
end
5959
rescue StandardError => e
60-
@messages << "#{self.class.name} read failed, #{e.message}"
60+
Rails.logger.error("#{e.class}: read() failed, #{e.message}")
6161
end
6262

6363
private
@@ -94,13 +94,9 @@ def github_api_from_io(uri, parts)
9494
# url: url to open
9595
# ttl: time-to-live in seconds (default 7 days)
9696
def cache_fetch(key, url)
97-
Rails.cache.fetch(key, expires_in: TTL) do
97+
Rails.cache.fetch(key, expires_in: TTL, skip_nil: true) do
9898
JSON.parse(open_url(url).read)
9999
end
100-
rescue StandardError => e
101-
@messages << "#{self.class.name} cache_fetch failed for #{url}, #{e.message}"
102-
yield if block_given?
103-
nil
104100
end
105101

106102
# Sets material hash keys and values and add them to material
@@ -137,15 +133,15 @@ def fetch_homepage_doc(url)
137133
Nokogiri::HTML(response.body)
138134
end
139135

140-
# DEFINITION – Opens the GitHub homepage, fetches the 3 first >25 char <p> tags'text
136+
# DEFINITION – Opens the GitHub homepage, fetches the 3 first >50 char <p> tags'text
141137
# and joins them with a 'Read more...' link at the end of the description
142138
# Some of the first <p> tags were not descriptive, thus skipping them
143139
def fetch_definition(doc, url)
144140
desc = ''
145141
round = 3
146142
doc.css('p').each do |p|
147-
p_txt = p&.text&.strip&.gsub(/\s+/, ' ')
148-
next if (p_txt.length < 50) || round.zero?
143+
p_txt = p&.text&.strip&.gsub(/\s+/, ' ') || ''
144+
next if p_txt.length < 50 || round.zero?
149145

150146
desc = "#{desc}\n#{p_txt}"
151147
round -= 1
@@ -187,6 +183,8 @@ def fetch_latest_release(full_name)
187183
# CONTRIBUTORS – Opens contributors API address and returns list of contributors
188184
def fetch_contributors(contributors_url, full_name)
189185
contributors = cache_fetch("#{CACHE_PREFIX}contributors_#{full_name.gsub('/', '_')}", contributors_url)
186+
return [] unless contributors
187+
190188
contributors.map { |c| (c['login']) }
191189
end
192190

@@ -200,7 +198,7 @@ def fetch_prerequisites(doc)
200198
# ... any tag with id containing "prereq" (EN) or "prerreq" (ES)
201199
prereq_paragraphs = fetch_prerequisites_from_id_or_class(doc, prereq_paragraphs) if prereq_paragraphs.empty?
202200

203-
prereq_paragraphs&.join("\n")&.gsub(/\n\n+/, "\n")&.to_s&.strip
201+
prereq_paragraphs&.join("\n")&.gsub(/\n\n+/, "\n")&.strip || ''
204202
end
205203

206204
def fetch_prerequisites_from_h(doc, prereq_paragraphs)

lib/ingestors/ingestor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_redirected_url(url, limit = 5) # rubocop:disable Metrics/AbcSize
7878
raise 'Too many redirects' if limit.zero?
7979

8080
https_url = to_https(url) # some `homepage` were http
81-
response = HTTParty.get(https_url, follow_redirects: true, headers: { 'User-Agent' => config[:user_agent] })
81+
response = HTTParty.get(https_url, follow_redirects: true, headers: { 'User-Agent' => config[:user_agent] || 'TeSS Bot' })
8282
return https_url unless response.headers['content-type']&.include?('html')
8383

8484
doc = Nokogiri::HTML(response.body)

test/unit/ingestors/github_ingestor_test.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def initialize(ingestor)
2121
decorator_ref.open_url_counter + 1
2222
)
2323

24-
# puts("open_url has been used, total #{decorator_ref.open_url_counter}")
2524
super(url, **kwargs)
2625
end
2726
end
@@ -157,7 +156,7 @@ def materials
157156
ingestor.open_url("| touch #{file}")
158157
rescue StandardError
159158
end
160-
`ls #{dir}` # This is needed or the `exist?` check below seems to return a stale result
159+
Dir.children(dir) # This is needed or the `exist?` check below seems to return a stale result
161160
refute File.exist?(file)
162161
end
163162

@@ -322,17 +321,17 @@ def materials
322321
end
323322

324323
test 'std errors when exception is raised' do
325-
@ingestor.stub(:get_sources, ->(_url) { raise StandardError, 'test failure' }) do
326-
@ingestor.send(:read, 'https://github.com/example')
327-
end
328-
assert_includes @ingestor.instance_variable_get(:@messages).last,
329-
'Ingestors::GithubIngestor read failed, test failure'
324+
@ingestor.stub(:get_sources, ->(*) { raise StandardError, 'test failure' }) do
325+
mock_logger = Minitest::Mock.new
326+
mock_logger.expect(:error, nil, ['StandardError: read() failed, test failure'])
330327

331-
@ingestor.stub(:open_url, ->(_url) { raise StandardError, 'test failure' }) do
332-
@ingestor.send(:cache_fetch, 'key', 'https://github.com/example')
328+
Rails.stub(:logger, mock_logger) do
329+
@ingestor.send(:read, 'https://github.com/example')
330+
end
331+
mock_logger.verify
333332
end
334-
assert_includes @ingestor.instance_variable_get(:@messages).last,
335-
'Ingestors::GithubIngestor cache_fetch failed for https://github.com/example, test failure'
333+
334+
assert true, 'Mock verification passed'
336335
end
337336

338337
test 'prereq_node? returns true when id includes prereq' do

0 commit comments

Comments
 (0)