Skip to content

Commit

Permalink
github actions straight
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 6, 2024
1 parent ebaf433 commit ab95f7c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/zerocracy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
repositories=yegor256/judges,yegor256/factbase,zerocracy/judges-action,zerocracy/pages-action
factbase: zerocracy.fb
trim: 5
- uses: zerocracy/[email protected].7
- uses: zerocracy/[email protected].8
with:
verbose: true
factbase: zerocracy.fb
Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GEM
concurrent-ruby (1.2.3)
ethon (0.16.0)
ffi (>= 1.15.0)
factbase (0.0.42)
factbase (0.0.43)
json (~> 2.7)
loog (~> 0.2)
nokogiri (~> 1.10)
Expand All @@ -27,14 +27,15 @@ GEM
gli (2.21.1)
iri (0.8.0)
json (2.7.2)
judges (0.3.0)
judges (0.4.0)
backtrace (~> 0.3)
concurrent-ruby (= 1.2.3)
factbase (~> 0.0)
gli (~> 2.21)
iri (~> 0.8)
loog (~> 0.2)
nokogiri (~> 1.10)
retries (~> 0.0)
typhoeus (~> 1.3)
language_server-protocol (3.17.0.3)
loog (0.5.1)
Expand All @@ -59,6 +60,7 @@ GEM
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.2)
retries (0.0.5)
rexml (3.2.8)
strscan (>= 3.0.9)
rubocop (1.64.1)
Expand Down
85 changes: 36 additions & 49 deletions judges/github-events/github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,95 +22,78 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

def put_new_event(fbt, json)
n = fbt.insert
n.when = Time.parse(json[:created_at].iso8601)
n.event_type = json[:type]
n.event_id = json[:id].to_i
n.repository = json[:repo][:id].to_i
n.who = json[:actor][:id].to_i if json[:actor]
def put_new_event(fact, json)
fact.when = Time.parse(json[:created_at].iso8601)
fact.event_type = json[:type]
fact.event_id = json[:id].to_i
fact.repository = json[:repo][:id].to_i
fact.who = json[:actor][:id].to_i if json[:actor]

case json[:type]
when 'PushEvent'
n.what = 'git-was-pushed'
n.push_id = json[:payload][:push_id]
fact.what = 'git-was-pushed'
fact.push_id = json[:payload][:push_id]
raise Factbase::Rollback

when 'IssuesEvent'
n.issue = json[:payload][:issue][:number]
fact.issue = json[:payload][:issue][:number]
if json[:payload][:action] == 'closed'
n.what = 'issue-was-closed'
fact.what = 'issue-was-closed'
elsif json[:payload][:action] == 'opened'
n.what = 'issue-was-opened'
fact.what = 'issue-was-opened'
end

when 'IssueCommentEvent'
n.issue = json[:payload][:issue][:number]
fact.issue = json[:payload][:issue][:number]
if json[:payload][:action] == 'created'
n.what = 'comment-was-posted'
n.comment_id = json[:payload][:comment][:id]
n.comment_body = json[:payload][:comment][:body]
n.who = json[:payload][:comment][:user][:id]
fact.what = 'comment-was-posted'
fact.comment_id = json[:payload][:comment][:id]
fact.comment_body = json[:payload][:comment][:body]
fact.who = json[:payload][:comment][:user][:id]
end
raise Factbase::Rollback

when 'ReleaseEvent'
n.release_id = json[:payload][:release][:id]
fact.release_id = json[:payload][:release][:id]
if json[:payload][:action] == 'published'
n.what = 'release-published'
n.who = json[:payload][:release][:author][:id]
fact.what = 'release-published'
fact.who = json[:payload][:release][:author][:id]
end

when 'CreateEvent'
if json[:payload][:ref_type] == 'tag'
n.what = 'tag-was-created'
n.tag = json[:payload][:ref]
fact.what = 'tag-was-created'
fact.tag = json[:payload][:ref]
end

else
raise Factbase::Rollback
end

n.details =
fact.details =
"A new event ##{json[:id]} happened in GitHub repository #{json[:repo][:name]} " \
"(##{json[:repo][:id]}) of type '#{json[:type]}', " \
"with the creation time #{json[:created_at].iso8601}; " \
'this fact must be interpreted later by other judges.'
end

# Taking the largest ID of GitHub event that was seen so far (or NIL)
def largest(repo)
largest = fb.query(
"(eq event_id
(agg (eq repository #{octo.repo_id_by_name(repo)})
(max event_id)))"
).each.to_a[0]
unless largest.nil?
largest = largest.event_id
$loog.debug("The largest ID we've seen so far is #{largest} (everything below this number will be ignored)")
end
largest
end

def one_repo(repo, seen)
def one_repo(repo, limit)
seen = 0
catch :stop do
largest = largest(repo)
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
if largest && json[:id].to_i <= largest
$loog.debug("The event ##{json[:id]} (#{json[:type]}) is below the largest ID #{largest}, skipping")
throw :stop
end
$loog.info("Detected new event ##{json[:id]} in #{json[:repo][:name]}: #{json[:type]}")
fb.txn do |fbt|
put_new_event(fbt, json)
if_absent(fbt) do |n|
put_new_event(n, json)
end
end
seen += 1
if !$options.max_events.nil? && seen >= $options.max_events
$loog.debug("Already scanned #{seen} events, that's enough (due to 'max_events' option)")
if seen >= limit
$loog.debug("Already scanned #{seen} events, that's enough (>=#{limit})")
throw :stop
end
throw :alarm if octo.off_quota
Expand All @@ -119,9 +102,13 @@ def one_repo(repo, seen)
seen
end

seen = 0
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
each_repo do |repo|
seen += one_repo(repo, seen)
repos = each_repo.each.to_a
repos.each do |repo|
one_repo(repo, limit / repos.size)
end
end

0 comments on commit ab95f7c

Please sign in to comment.