Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#214 new attributes failed_builds and succeeded_builds for pull-was-merged #296

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions judges/github-events/github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ def self.count_appreciated_comments(pr, issue_comments, code_comments)
issue_appreciations + code_appreciations
end

def self.fetch_workflows(pr)
succeeded_builds = 0
failed_builds = 0
Fbe.octo.check_runs_for_ref(pr[:base][:repo][:full_name], pr[:head][:sha])[:check_runs].each do |run|
workflow = Fbe.octo.workflow_run(
pr[:base][:repo][:full_name],
Fbe.octo.workflow_run_job(pr[:base][:repo][:full_name], run[:id])[:run_id]
)
next unless workflow[:event] == 'pull_request'
case workflow[:conclusion]
when 'success'
succeeded_builds += 1
when 'failure'
failed_builds += 1
end
end
{ succeeded_builds:, failed_builds: }
end

def self.fill_fact_by_hash(fact, hash)
hash.each do |prop, value|
fact.send(:"#{prop}=", value)
end
end

def self.fill_up_event(fact, json)
fact.when = Time.parse(json[:created_at].iso8601)
fact.event_type = json[:type]
Expand Down Expand Up @@ -151,11 +176,8 @@ def self.fill_up_event(fact, json)
when 'closed'
fact.what = "pull-was-#{pl[:merged_at].nil? ? 'closed' : 'merged'}"
fact.hoc = pl[:additions] + pl[:deletions]
comments_info(pl).then do |info|
info.each do |prop, value|
fact.send(:"#{prop}=", value)
end
end
fill_fact_by_hash(fact, comments_info(pl))
fill_fact_by_hash(fact, fetch_workflows(pl))
fact.branch = pl[:head][:ref]
fact.details =
"The pull request #{Fbe.issue(fact)} " \
Expand All @@ -177,7 +199,6 @@ def self.fill_up_event(fact, json)
).each.last
skip_event(json)
end

fact.issue = json[:payload][:pull_request][:number]
fact.what = 'pull-was-reviewed'
pull = Fbe.octo.pull_request(json[:repo][:name], fact.issue)
Expand Down Expand Up @@ -228,7 +249,7 @@ def self.fill_up_event(fact, json)
fact.what = 'release-published'
fact.who = json[:payload][:release][:author][:id]
fetch_contributors(fact, json[:repo][:name]).each { |c| fact.contributors = c }
fetch_release_info(fact, json[:repo][:name]).each { |prop, val| fact.send(:"#{prop}=", val) }
fill_fact_by_hash(fact, fetch_release_info(fact, json[:repo][:name]))
fact.details =
"A new release '#{json[:payload][:release][:name]}' has been published " \
"in #{json[:repo][:name]} by #{Fbe.who(fact)}."
Expand Down
15 changes: 9 additions & 6 deletions test/judges/test-github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,7 @@ def test_release_event_contributors

def test_pull_request_event_with_comments
fb = Factbase.new
$fb = fb
$global = {}
$local = {}
$options = Judges::Options.new({ 'repositories' => 'foo/foo', 'testing' => true })
$loog = Loog::NULL
load(File.join(__dir__, '../../judges/github-events/github-events.rb'))
load_it('github-events', fb, Judges::Options.new({ 'repositories' => 'foo/foo', 'testing' => true }))
f = fb.query('(eq what "pull-was-merged")').each.to_a.first
assert_equal(4, f.comments)
assert_equal(2, f.comments_to_code)
Expand All @@ -641,6 +636,14 @@ def test_pull_request_event_with_comments
assert_equal(1, f.comments_resolved)
end

def test_count_numbers_of_workflow_builds
fb = Factbase.new
load_it('github-events', fb, Judges::Options.new({ 'repositories' => 'foo/foo', 'testing' => true }))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suban05 Why not use web mock for this cases? Testing mode used for run yml files, for example - https://github.com/zerocracy/judges-action/blob/master/judges/github-events/quick-scan.yml

Copy link
Contributor Author

@Suban05 Suban05 Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yegorov good question. For the pull-was-merged event, we use Fbe.github_graph. Maybe I don't understand something, but it's very difficult to mock this thing by WebMock. And I use a fake client for Fbe.github_graph. However, there is an options argument in both clients (Fbe.octo and Fbe.github_graph), and when I set testing globally, Fbe.octo also becomes a fake client. That's why I use fake clients

f = fb.query('(eq what "pull-was-merged")').each.to_a.first
assert_equal(4, f.succeeded_builds)
assert_equal(2, f.failed_builds)
end

private

def stub_event(*json)
Expand Down
4 changes: 2 additions & 2 deletions test/test__helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
require 'minitest/autorun'

class Minitest::Test
def load_it(judge, fb)
def load_it(judge, fb, options = Judges::Options.new({ 'repositories' => 'foo/foo' }))
$fb = fb
$global = {}
$local = {}
$judge = judge
$options = Judges::Options.new({ 'repositories' => 'foo/foo' })
$options = options
$loog = Loog::NULL
load(File.join(__dir__, "../judges/#{judge}/#{judge}.rb"))
end
Expand Down
Loading