Skip to content

Commit

Permalink
#51 polish
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 2, 2024
1 parent 343f364 commit fc1b029
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 122 deletions.
30 changes: 16 additions & 14 deletions lib/fbe/fb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@
require_relative '../fbe'

def Fbe.fb(fb: $fb, global: $global, options: $options, loog: $loog)
global[:fb] ||= begin
rules = Dir.glob(File.join('rules', '*.fe')).map { |f| File.read(f) }
fbe = Factbase::Rules.new(
fb,
"(and \n#{rules.join("\n")}\n)",
uid: '_id'
)
fbe = Factbase::Pre.new(fbe) do |f|
max = fb.query('(eq _id (max _id))').each.to_a.first
f._id = (max.nil? ? 0 : max._id) + 1
f._time = Time.now
f._version = "#{Factbase::VERSION}/#{Judges::VERSION}/#{options.judges_action_version}"
global[:fb] ||=
begin
rules = Dir.glob(File.join('rules', '*.fe')).map { |f| File.read(f) }
fbe = Factbase::Rules.new(
fb,
"(and \n#{rules.join("\n")}\n)",
uid: '_id'
)
fbe =
Factbase::Pre.new(fbe) do |f|
max = fb.query('(eq _id (max _id))').each.to_a.first
f._id = (max.nil? ? 0 : max._id) + 1
f._time = Time.now
f._version = "#{Factbase::VERSION}/#{Judges::VERSION}/#{options.judges_action_version}"
end
Factbase::Looged.new(fbe, loog)
end
Factbase::Looged.new(fbe, loog)
end
end
15 changes: 8 additions & 7 deletions lib/fbe/if_absent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
# Injects a fact if it's absent in the factbase.
def Fbe.if_absent(fb: Fbe.fb)
attrs = {}
f = others(map: attrs) do |*args|
k = args[0]
if k.end_with?('=')
@map[k[0..-2].to_sym] = args[1]
else
@map[k.to_sym]
f =
others(map: attrs) do |*args|
k = args[0]
if k.end_with?('=')
@map[k[0..-2].to_sym] = args[1]
else
@map[k.to_sym]
end
end
end
yield f
q = attrs.except('_id', '_time', '_version').map do |k, v|
vv = v.to_s
Expand Down
162 changes: 82 additions & 80 deletions lib/fbe/octo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,97 +35,99 @@

def Fbe.octo(options: $options, global: $global, loog: $loog)
raise 'The $global is not set' if global.nil?
global[:octo] ||= begin
if options.testing.nil?
o = Octokit::Client.new
token = options.github_token
if token.nil?
loog.debug("The 'github_token' option is not provided")
token = ENV.fetch('GITHUB_TOKEN', nil)
global[:octo] ||=
begin
if options.testing.nil?
o = Octokit::Client.new
token = options.github_token
if token.nil?
loog.debug("The 'GITHUB_TOKEN' environment variable is not set")
loog.debug("The 'github_token' option is not provided")
token = ENV.fetch('GITHUB_TOKEN', nil)
if token.nil?
loog.debug("The 'GITHUB_TOKEN' environment variable is not set")
else
loog.debug("The 'GITHUB_TOKEN' environment was provided")
end
else
loog.debug("The 'GITHUB_TOKEN' environment was provided")
loog.debug("The 'github_token' option was provided")
end
else
loog.debug("The 'github_token' option was provided")
end
if token.nil?
loog.warn('Accessing GitHub API without a token!')
elsif token.empty?
loog.warn('The GitHub API token is an empty string, won\'t use it')
else
o = Octokit::Client.new(access_token: token)
loog.info("Accessing GitHub API with a token (#{token.length} chars, ending by #{token[-4..]})")
end
o.auto_paginate = true
o.per_page = 100
o.connection_options = {
request: {
open_timeout: 15,
timeout: 15
}
}
stack = Faraday::RackBuilder.new do |builder|
builder.use(
Faraday::Retry::Middleware,
exceptions: Faraday::Retry::Middleware::DEFAULT_EXCEPTIONS + [
Octokit::TooManyRequests, Octokit::ServiceUnavailable
],
max: 4,
interval: ENV['RACK_ENV'] == 'test' ? 0.01 : 4,
methods: [:get],
backoff_factor: 2
)
builder.use(
Fbe::Middleware::Quota,
logger: loog,
pause: options.github_api_pause
)
builder.use(Faraday::HttpCache, serializer: Marshal, shared_cache: false, logger: Loog::NULL)
builder.use(Octokit::Response::RaiseError)
builder.use(Faraday::Response::Logger, Loog::NULL)
builder.adapter(Faraday.default_adapter)
end
o.middleware = stack
o = Verbose.new(o, log: loog)
else
loog.debug('The connection to GitHub API is mocked')
o = Fbe::FakeOctokit.new
end
decoor(o, loog:) do
def off_quota
left = @origin.rate_limit.remaining
if left < 5
@loog.info("To much GitHub API quota consumed already (remaining=#{left}), stopping")
true
if token.nil?
loog.warn('Accessing GitHub API without a token!')
elsif token.empty?
loog.warn('The GitHub API token is an empty string, won\'t use it')
else
false
o = Octokit::Client.new(access_token: token)
loog.info("Accessing GitHub API with a token (#{token.length} chars, ending by #{token[-4..]})")
end
o.auto_paginate = true
o.per_page = 100
o.connection_options = {
request: {
open_timeout: 15,
timeout: 15
}
}
stack =
Faraday::RackBuilder.new do |builder|
builder.use(
Faraday::Retry::Middleware,
exceptions: Faraday::Retry::Middleware::DEFAULT_EXCEPTIONS + [
Octokit::TooManyRequests, Octokit::ServiceUnavailable
],
max: 4,
interval: ENV['RACK_ENV'] == 'test' ? 0.01 : 4,
methods: [:get],
backoff_factor: 2
)
builder.use(
Fbe::Middleware::Quota,
logger: loog,
pause: options.github_api_pause
)
builder.use(Faraday::HttpCache, serializer: Marshal, shared_cache: false, logger: Loog::NULL)
builder.use(Octokit::Response::RaiseError)
builder.use(Faraday::Response::Logger, Loog::NULL)
builder.adapter(Faraday.default_adapter)
end
o.middleware = stack
o = Verbose.new(o, log: loog)
else
loog.debug('The connection to GitHub API is mocked')
o = Fbe::FakeOctokit.new
end
decoor(o, loog:) do
def off_quota
left = @origin.rate_limit.remaining
if left < 5
@loog.info("To much GitHub API quota consumed already (remaining=#{left}), stopping")
true
else
false
end
end

def user_name_by_id(id)
json = @origin.user(id)
name = json[:login]
@loog.debug("GitHub user ##{id} has a name: @#{name}")
name
end
def user_name_by_id(id)
json = @origin.user(id)
name = json[:login]
@loog.debug("GitHub user ##{id} has a name: @#{name}")
name
end

def repo_id_by_name(name)
json = @origin.repository(name)
id = json[:id]
@loog.debug("GitHub repository #{name} has an ID: ##{id}")
id
end
def repo_id_by_name(name)
json = @origin.repository(name)
id = json[:id]
@loog.debug("GitHub repository #{name} has an ID: ##{id}")
id
end

def repo_name_by_id(id)
json = @origin.repository(id)
name = json[:full_name]
@loog.debug("GitHub repository ##{id} has a name: #{name}")
name
def repo_name_by_id(id)
json = @origin.repository(id)
name = json[:full_name]
@loog.debug("GitHub repository ##{id} has a name: #{name}")
name
end
end
end
end
end

# Fake GitHub client, for tests.
Expand Down
47 changes: 26 additions & 21 deletions test/fbe/test_if_absent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,30 @@ class TestIfAbsent < Minitest::Test
def test_ignores
fb = Factbase.new
fb.insert.foo = 'hello dude'
n = Fbe.if_absent(fb:) do |f|
f.foo = 'hello dude'
end
n =
Fbe.if_absent(fb:) do |f|
f.foo = 'hello dude'
end
assert(n.nil?)
end

def test_ignores_with_time
fb = Factbase.new
t = Time.now
fb.insert.foo = t
n = Fbe.if_absent(fb:) do |f|
f.foo = t
end
n =
Fbe.if_absent(fb:) do |f|
f.foo = t
end
assert(n.nil?)
end

def test_injects
fb = Factbase.new
n = Fbe.if_absent(fb:) do |f|
f.foo = 42
end
n =
Fbe.if_absent(fb:) do |f|
f.foo = 42
end
assert_equal(42, n.foo)
end

Expand All @@ -77,12 +80,13 @@ def test_complex_ignores
t = Time.now
f1.z = t
f1.bar = 3.14
n = Fbe.if_absent(fb:) do |f|
f.foo = 'hello, "dude"!'
f.abc = 42
f.z = t
f.bar = 3.14
end
n =
Fbe.if_absent(fb:) do |f|
f.foo = 'hello, "dude"!'
f.abc = 42
f.z = t
f.bar = 3.14
end
assert(n.nil?)
end

Expand All @@ -94,12 +98,13 @@ def test_complex_injects
t = Time.now
f1.z = t
f1.bar = 3.14
n = Fbe.if_absent(fb:) do |f|
f.foo = "hello, \\\"dude\\\" \\' \\' ( \n\n ) (! '"
f.abc = 42
f.z = t + 1
f.bar = 3.15
end
n =
Fbe.if_absent(fb:) do |f|
f.foo = "hello, \\\"dude\\\" \\' \\' ( \n\n ) (! '"
f.abc = 42
f.z = t + 1
f.bar = 3.15
end
assert(!n.nil?)
end
end

0 comments on commit fc1b029

Please sign in to comment.