Skip to content

Commit

Permalink
fix them at a new fact
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 6, 2024
1 parent ab95f7c commit 704d76b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
57 changes: 35 additions & 22 deletions judges/github-events/github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,49 @@ def put_new_event(fact, json)

def one_repo(repo, limit)
seen = 0
catch :stop do
octo.repository_events(repo).each do |json|
unless fb.query("(eq event_id #{json[:id]})").each.to_a.empty?
$loog.debug("The event ##{json[:id]} (#{json[:type]}) has already been seen, skipping")
next
end
$loog.info("Detected new event ##{json[:id]} in #{json[:repo][:name]}: #{json[:type]}")
fb.txn do |fbt|
if_absent(fbt) do |n|
put_new_event(n, json)
end
end
seen += 1
if seen >= limit
$loog.debug("Already scanned #{seen} events, that's enough (>=#{limit})")
throw :stop
repo_id = octo.repo_id_by_name(repo)
q = "(and (eq what '#{$judge}') (eq repository #{repo_id}))"
prev = fb.query(q).each.to_a[0]
fb.query(q).delete!
first = nil
last = nil
octo.repository_events(repo).each do |json|
first = json[:id] if first.nil?
last = json[:id]
if !prev.nil? && last <= prev.first_event_id
$loog.debug(
"No reason to scan what was scanned before: first_event_id=#{prev.first_event_id}, " \
"prev.last_event_id=#{prev.last_event_id}, first=#{first}, last=#{last}"
)
break
end
fb.txn do |fbt|
f = if_absent(fbt) do |n|
put_new_event(n, json)
end
throw :alarm if octo.off_quota
$loog.info("Detected new event ##{json[:id]} in #{json[:repo][:name]}: #{json[:type]}") unless f.nil?
end
seen += 1
if seen >= limit
$loog.debug("Already scanned #{seen} events in #{repo}, that's enough (>=#{limit})")
break
end
break if octo.off_quota
end
f = fb.insert
f.repository = repo_id
f.first_event_id = first
f.last_event_id = last
f.what = $judge
seen
end

limit = $options.max_events
limit = 1000 if limit.nil?
raise "It is impossible to scan deeper than 10,000 GitHub events, you asked for #{limit}" if limit > 10_000

catch :alarm do
repos = each_repo.each.to_a
repos.each do |repo|
one_repo(repo, limit / repos.size)
end
repos = each_repo.each.to_a
repos.each do |repo|
one_repo(repo, limit / repos.size)
break if octo.off_quota
end
5 changes: 4 additions & 1 deletion judges/github-events/quick-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ options:
max_events: 3
repositories: yegor256/judges
expected:
- /fb[count(f)=2]
- /fb[count(f)=3]
- /fb/f/event_id
- /fb/f/when
- /fb/f/what
- /fb/f[what='github-events']
- /fb/f[what='github-events' and first_event_id='123']
- /fb/f[what='github-events' and last_event_id='125']
6 changes: 5 additions & 1 deletion judges/github-events/scan-over-existing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ options:
max_events: 2
repositories: yegor256/judges
expected:
- /fb[count(f)=2]
- /fb[count(f)=3]
- /fb/f[event_id=124]
- /fb/f[what='github-events']
- /fb/f[what='github-events' and first_event_id='123']
- /fb/f[what='github-events' and last_event_id='124']
2 changes: 1 addition & 1 deletion lib/octo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def octo
builder.use Octokit::Response::RaiseError
builder.adapter Faraday.default_adapter
end
Octokit.middleware = stack
o.middleware = stack
o = Obk.new(o, pause: 1000)
else
$loog.debug('The connection to GitHub API is mocked')
Expand Down
8 changes: 8 additions & 0 deletions rules/basic.fe
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
(eq what 'bug-was-resolved')
(eq what 'comment-was-posted')
(eq what 'git-was-pushed')
(eq what 'github-events')
(eq what 'issue-was-closed')
(eq what 'issue-was-opened')
(eq what 'issue-was-assigned')
Expand Down Expand Up @@ -90,6 +91,13 @@
(exists who)
(exists reason))))

(explain (when
(eq what 'github-events')
(and
(exists repository)
(exists first_event_id)
(exists last_event_id))))

(explain (when
(eq what 'label-was-attached')
(and
Expand Down

0 comments on commit 704d76b

Please sign in to comment.