Skip to content

Commit

Permalink
#397: fix get actual repo name in github events
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegorov committed Nov 4, 2024
1 parent 8585678 commit 788888b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 11 deletions.
17 changes: 9 additions & 8 deletions judges/github-events/github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,27 @@ def self.fill_up_event(fact, json)
fact.event_type = json[:type]
fact.repository = json[:repo][:id].to_i
fact.who = json[:actor][:id].to_i if json[:actor]
rname = Fbe.octo.repo_name_by_id(fact.repository)

case json[:type]
when 'PushEvent'
fact.what = 'git-was-pushed'
fact.push_id = json[:payload][:push_id]
fact.ref = json[:payload][:ref]
fact.commit = json[:payload][:head]
fact.default_branch = Fbe.octo.repository(json[:repo][:name])[:default_branch]
fact.default_branch = Fbe.octo.repository(rname)[:default_branch]
fact.to_master = fact.default_branch == fact.ref.split('/')[2] ? 1 : 0
if fact.to_master.zero?
$loog.debug("Push #{fact.commit} has been made to non-default branch '#{fact.default_branch}', ignoring it")
skip_event(json)
end
pulls = Fbe.octo.commit_pulls(json[:repo][:name], fact.commit)
pulls = Fbe.octo.commit_pulls(rname, fact.commit)
unless pulls.empty?
$loog.debug("Push #{fact.commit} has been made inside #{pulls.size} pull request(s), ignoring it")
skip_event(json)
end
fact.details =
"A new Git push ##{json[:payload][:push_id]} has arrived to #{json[:repo][:name]}, " \
"A new Git push ##{json[:payload][:push_id]} has arrived to #{rname}, " \
"made by #{Fbe.who(fact)} (default branch is '#{fact.default_branch}'), " \
'not associated with any pull request.'

Expand Down Expand Up @@ -227,7 +228,7 @@ def self.fill_up_event(fact, json)

fact.issue = json[:payload][:pull_request][:number]
fact.what = 'pull-was-reviewed'
pull = Fbe.octo.pull_request(json[:repo][:name], fact.issue)
pull = Fbe.octo.pull_request(rname, fact.issue)
fact.hoc = pull[:additions] + pull[:deletions]
fact.comments = pull[:comments] + pull[:review_comments]
fact.commits = pull[:commits]
Expand Down Expand Up @@ -275,11 +276,11 @@ def self.fill_up_event(fact, json)
when 'published'
fact.what = 'release-published'
fact.who = json[:payload][:release][:author][:id]
fetch_contributors(fact, json[:repo][:name]).each { |c| fact.contributors = c }
fill_fact_by_hash(fact, fetch_release_info(fact, json[:repo][:name]))
fetch_contributors(fact, rname).each { |c| fact.contributors = c }
fill_fact_by_hash(fact, fetch_release_info(fact, rname))
fact.details =
"A new release '#{json[:payload][:release][:name]}' has been published " \
"in #{json[:repo][:name]} by #{Fbe.who(fact)}."
"in #{rname} by #{Fbe.who(fact)}."
else
skip_event(json)
end
Expand All @@ -291,7 +292,7 @@ def self.fill_up_event(fact, json)
fact.tag = json[:payload][:ref]
fact.details =
"A new tag '#{fact.tag}' has been created " \
"in #{json[:repo][:name]} by #{Fbe.who(fact)}."
"in #{rname} by #{Fbe.who(fact)}."
else
skip_event(json)
end
Expand Down
116 changes: 113 additions & 3 deletions test/judges/test-github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_skip_event_when_user_equals_pr_author
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repos/yegor256/judges/pulls/93')
stub_request(:get, 'https://api.github.com/repos/foo/foo/pulls/93')
.to_return(
status: 200,
body: {
Expand Down Expand Up @@ -359,7 +359,7 @@ def test_add_only_approved_pull_request_review_events
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repos/yegor256/judges/pulls/93')
stub_request(:get, 'https://api.github.com/repos/foo/foo/pulls/93')
.to_return(
status: 200,
body: {
Expand Down Expand Up @@ -719,7 +719,7 @@ def test_watch_pull_request_review_events
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repos/yegor256/judges/pulls/93')
stub_request(:get, 'https://api.github.com/repos/foo/foo/pulls/93')
.to_return(
status: 200,
body: {
Expand Down Expand Up @@ -815,6 +815,10 @@ def test_release_event_contributors
}
}
)
stub_github(
'https://api.github.com/repositories/820463873',
body: { id: 820_463_873, name: 'fbe', full_name: 'zerocracy/fbe' }
)
stub_request(:get, 'https://api.github.com/repos/zerocracy/fbe/contributors?per_page=100').to_return(
body: [
{
Expand Down Expand Up @@ -1080,6 +1084,112 @@ def test_release_event_contributors_without_last_release_tag_and_without_release
assert_equal([526_301, 526_302], f.last[:contributors])
end

def test_event_for_renamed_repository
WebMock.disable_net_connect!
stub_github(
'https://api.github.com/repositories/111/events?per_page=100',
body: [
{
id: '4321000',
type: 'ReleaseEvent',
actor: {
id: 29_139_614,
login: 'renovate[bot]'
},
repo: {
id: 111,
name: 'foo/old_baz'
},
payload: {
action: 'published',
release: {
id: 178_368,
tag_name: 'v1.2.3',
name: 'Release v1.2.3',
author: {
id: 29_139_614,
login: 'renovate[bot]'
}
}
},
created_at: Time.parse('2024-11-01 12:30:15 UTC')
}
]
)
stub_github(
'https://api.github.com/repos/foo/new_baz',
body: { id: 111, name: 'new_baz', full_name: 'foo/new_baz' }
)
stub_github(
'https://api.github.com/repositories/111',
body: { id: 111, name: 'new_baz', full_name: 'foo/new_baz' }
)
stub_github(
'https://api.github.com/user/29139614',
body: {
login: 'renovate[bot]',
id: 29_139_614,
type: 'Bot'
}
)
stub_github(
'https://api.github.com/repos/foo/new_baz/contributors?per_page=100',
body: [
{
login: 'yegor256',
id: 526_301
}
]
)
stub_github(
'https://api.github.com/repos/foo/new_baz/releases/20000',
body: {
id: 20_000,
tag_name: 'v1.2.2',
target_commitish: 'master',
name: 'Release v1.2.2',
draft: false,
prerelease: false,
created_at: Time.parse('2024-10-31 21:45:00 UTC'),
published_at: Time.parse('2024-10-31 21:45:00 UTC'),
body: 'Release v1.2.2'
}
)
stub_github(
'https://api.github.com/repos/foo/new_baz/compare/v1.2.2...v1.2.3?per_page=100',
body: {
total_commits: 2,
commits: [
{ sha: '2aa49900e720bd792e9' },
{ sha: '1bbb36f67e5b97246b1' }
],
files: [
{ additions: 7, deletions: 4, changes: 11 },
{ additions: 2, deletions: 0, changes: 2 },
{ additions: 0, deletions: 7, changes: 7 }
]
}
)
fb = Factbase.new
fb.insert.then do |f|
f.details = 'Release v1.2.2'
f.event_id = 35_207
f.event_type = 'ReleaseEvent'
f.is_human = 1
f.release_id = 20_000
f.repository = 111
f.what = 'release-published'
f.when = Time.parse('2024-10-31 21:45:00 UTC')
f.where = 'github'
f.who = 526_301
end
load_it('github-events', fb, Judges::Options.new({ 'repositories' => 'foo/new_baz' }))
f = fb.query('(eq what "release-published")').each.to_a.last
assert_equal(111, f.repository)
assert_equal('v1.2.3', f.tag)
refute_match(/old_baz/, f.details)
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 788888b

Please sign in to comment.