Skip to content

Commit

Permalink
#373: fix fetch contributors if tag empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegorov committed Oct 4, 2024
1 parent 3b1afd1 commit e600253
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 5 deletions.
22 changes: 17 additions & 5 deletions judges/github-events/github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,22 @@ def self.skip_event(json)
raise Factbase::Rollback
end

def self.fetch_tag(fact, repo)
tag = fact&.all_properties&.include?('tag') ? fact.tag : nil
if tag.nil? && fact&.all_properties&.include?('release_id')
tag = Fbe.octo.release(
"https://api.github.com/repos/#{repo}/releases/#{fact.release_id}"
).fetch(:tag_name, nil)
end
tag
end

def self.fetch_contributors(fact, repo)
last = Fbe.fb.query("(and (eq repository #{fact.repository}) (eq what \"#{fact.what}\"))").each.last
tag = fetch_tag(last, repo)
contributors = Set.new
if last
Fbe.octo.compare(repo, last.tag, fact.tag)[:commits].each do |commit|
if tag
Fbe.octo.compare(repo, tag, fact.tag)[:commits].each do |commit|
author_id = commit.dig(:author, :id)
contributors << author_id if author_id
end
Expand All @@ -61,10 +72,11 @@ def self.fetch_contributors(fact, repo)
end

def self.fetch_release_info(fact, repo)
last = Fbe.fb.query("(and (eq repository #{fact.repository}) (eq what \"#{fact.what}\"))").each.last&.tag
last ||= find_first_commit(repo)[:sha]
last = Fbe.fb.query("(and (eq repository #{fact.repository}) (eq what \"#{fact.what}\"))").each.last
tag = fetch_tag(last, repo)
tag ||= find_first_commit(repo)[:sha]
info = {}
Fbe.octo.compare(repo, last, fact.tag).then do |json|
Fbe.octo.compare(repo, tag, fact.tag).then do |json|
info[:commits] = json[:total_commits]
info[:hoc] = json[:files].map { |f| f[:changes] }.sum
info[:last_commit] = json[:commits].first[:sha]
Expand Down
174 changes: 174 additions & 0 deletions test/judges/test-github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,180 @@ def test_release_event_contributors
assert_equal('a50489ead5e8aa6', f.last.last_commit)
end

def test_release_event_contributors_without_last_release_tag_and_with_release_id
WebMock.disable_net_connect!
stub_event(
{
id: '10',
type: 'ReleaseEvent',
actor: {
id: 8_086_956,
login: 'rultor',
display_login: 'rultor'
},
repo: {
id: 42,
name: 'foo/foo',
url: 'https://api.github.com/repos/foo/foo'
},
payload: {
action: 'published',
release: {
id: 471_000,
author: {
login: 'rultor',
id: 8_086_956,
type: 'User',
site_admin: false
},
tag_name: '0.0.3',
name: 'v0.0.3',
created_at: Time.parse('2024-08-05T00:51:39Z'),
published_at: Time.parse('2024-08-05T00:52:07Z')
}
},
public: true,
created_at: Time.parse('2024-08-06T00:52:08Z'),
org: {
id: 24_234_201,
login: 'foo'
}
}
)
stub_github(
'https://api.github.com/repos/foo/foo/releases/470000',
body: {
id: 470_000,
tag_name: '0.0.2',
target_commitish: 'master',
name: 'v0.0.2',
draft: false,
prerelease: false,
created_at: Time.parse('2024-08-02 21:45:00 UTC'),
published_at: Time.parse('2024-08-02 21:45:00 UTC'),
body: '0.0.2 release'
}
)
stub_github(
'https://api.github.com/repos/foo/foo/compare/0.0.2...0.0.3?per_page=100',
body: {
total_commits: 3,
commits: [
{ sha: 'a50489ead5e8aa6', author: { login: 'Yegorov', id: 2_566_462 } },
{ sha: 'b50489ead5e8aa7', author: { login: 'Yegorov64', id: 2_566_463 } },
{ sha: '89ead5eb5048aa7', author: { login: 'Yegorov128', id: 2_566_464 } }
],
files: [{ additions: 15, deletions: 40, changes: 55 }]
}
)
stub_github(
'https://api.github.com/user/8086956',
body: { login: 'rultor', id: 8_086_956 }
)
fb = Factbase.new
fb.insert.then do |f|
f.details = 'v0.0.2'
f.event_id = 30_406
f.event_type = 'ReleaseEvent'
f.is_human = 1
f.release_id = 470_000
f.repository = 42
f.what = 'release-published'
f.when = Time.parse('2024-08-02 21:45:00 UTC')
f.where = 'github'
f.who = 526_301
end
load_it('github-events', fb)
f = fb.query('(and (eq repository 42) (eq what "release-published"))').each.to_a
assert_equal(2, f.count)
assert_nil(f.first[:tag])
refute_nil(f.first[:release_id])
assert_equal([2_566_462, 2_566_463, 2_566_464], f.last[:contributors])
end

def test_release_event_contributors_without_last_release_tag_and_without_release_id
WebMock.disable_net_connect!
stub_event(
{
id: '10',
type: 'ReleaseEvent',
actor: {
id: 8_086_956,
login: 'rultor',
display_login: 'rultor'
},
repo: {
id: 42,
name: 'foo/foo',
url: 'https://api.github.com/repos/foo/foo'
},
payload: {
action: 'published',
release: {
id: 471_000,
author: {
login: 'rultor',
id: 8_086_956,
type: 'User',
site_admin: false
},
tag_name: '0.0.3',
name: 'v0.0.3',
created_at: Time.parse('2024-08-05T00:51:39Z'),
published_at: Time.parse('2024-08-05T00:52:07Z')
}
},
public: true,
created_at: Time.parse('2024-08-06T00:52:08Z'),
org: {
id: 24_234_201,
login: 'foo'
}
}
)
stub_github(
'https://api.github.com/repos/foo/foo/contributors?per_page=100',
body: [
{ login: 'yegor256', id: 526_301 },
{ login: 'yegor512', id: 526_302 }
]
)
stub_github(
'https://api.github.com/repos/foo/foo/commits?per_page=100',
body: [{ sha: '4683257342e98cd94' }]
)
stub_github(
'https://api.github.com/repos/foo/foo/compare/4683257342e98cd94...0.0.3?per_page=100',
body: {
total_commits: 1,
commits: [{ sha: 'a50489ead5e8aa6', author: { login: 'Yegorov', id: 2_566_462 } }],
files: [{ additions: 15, deletions: 40, changes: 55 }]
}
)
stub_github(
'https://api.github.com/user/8086956',
body: { login: 'rultor', id: 8_086_956 }
)
fb = Factbase.new
fb.insert.then do |f|
f.details = 'v0.0.2'
f.event_id = 30_407
f.event_type = 'ReleaseEvent'
f.is_human = 1
f.repository = 42
f.what = 'release-published'
f.when = Time.parse('2024-08-02 21:45:00 UTC')
f.where = 'github'
f.who = 526_301
end
load_it('github-events', fb)
f = fb.query('(and (eq repository 42) (eq what "release-published"))').each.to_a
assert_equal(2, f.count)
assert_nil(f.first[:tag])
assert_nil(f.first[:release_id])
assert_equal([526_301, 526_302], f.last[:contributors])
end

def test_pull_request_event_with_comments
fb = Factbase.new
load_it('github-events', fb, Judges::Options.new({ 'repositories' => 'zerocracy/baza', 'testing' => true }))
Expand Down

0 comments on commit e600253

Please sign in to comment.