Skip to content

Commit

Permalink
Merge branch 'master' into 214
Browse files Browse the repository at this point in the history
  • Loading branch information
Suban05 authored Aug 21, 2024
2 parents a20b2f3 + 413cc23 commit bd41ed9
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 0 deletions.
13 changes: 13 additions & 0 deletions judges/github-events/github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ def self.comments_info(pr)
}
end

def self.issue_seen_already?(fact)
Fbe.fb.query(
"(and (eq repository #{fact.repository}) " \
'(eq where "github") ' \
"(not (eq event_id #{fact.event_id}))" \
"(eq what \"#{fact.what}\") " \
"(eq issue #{fact.issue}))"
).each.any?
end

def self.count_appreciated_comments(pr, issue_comments, code_comments)
issue_appreciations =
issue_comments.sum do |comment|
Expand Down Expand Up @@ -199,6 +209,8 @@ def self.fill_up_event(fact, json)
).each.last
skip_event(json)
end
skip_event(json) unless json[:payload][:review][:state] == 'approved'

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 @@ -226,6 +238,7 @@ def self.fill_up_event(fact, json)
else
skip_event(json)
end
skip_event(json) if issue_seen_already?(fact)

when 'IssueCommentEvent'
fact.issue = json[:payload][:issue][:number]
Expand Down
279 changes: 279 additions & 0 deletions test/judges/test-github-events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def test_skip_event_when_user_equals_pr_author
node_id: 'MDQ6VXNlcjUyNjMwMQ==',
type: 'User'
},
state: 'approved',
pull_request_url: 'https://api.github.com/repos/yegor256/judges/pulls/93',
author_association: 'OWNER',
_links: {
Expand Down Expand Up @@ -175,6 +176,7 @@ def test_skip_event_when_user_equals_pr_author
node_id: 'MDQ6VXNlcjUyNjMwMQ==',
type: 'User'
},
state: 'approved',
pull_request_url: 'https://api.github.com/repos/yegor256/judges/pulls/93',
author_association: 'NONE',
_links: {
Expand Down Expand Up @@ -240,6 +242,280 @@ def test_skip_event_when_user_equals_pr_author
assert_nil(f[1])
end

def test_add_only_approved_pull_request_review_events
WebMock.disable_net_connect!
stub_request(:get, 'https://api.github.com/repos/foo/foo').to_return(
body: { id: 42, full_name: 'foo/foo' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repositories/42').to_return(
body: { id: 42, full_name: 'foo/foo' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repositories/42/events?per_page=100').to_return(
body: [
{
id: '40623323541',
type: 'PullRequestReviewEvent',
public: true,
created_at: '2024-07-31 12:45:09 UTC',
actor: {
id: 42,
login: 'torvalds',
display_login: 'torvalds',
gravatar_id: ''
},
repo: {
id: 42,
name: 'yegor256/judges'
},
payload: {
action: 'created',
review: {
id: 2_210_067_609,
user: {
login: 'torvalds',
id: 42,
type: 'User'
},
state: 'approved',
author_association: 'OWNER'
},
pull_request: {
id: 1_990_323_142,
number: 93,
state: 'open',
locked: false,
title: 'allows to push gizpped factbase',
user: {
login: 'test',
id: 526_200,
type: 'User',
site_admin: false
}
}
}
},
{
id: '40623323542',
type: 'PullRequestReviewEvent',
public: true,
created_at: '2024-07-31 12:45:09 UTC',
actor: {
id: 43,
login: 'yegor256',
display_login: 'yegor256'
},
repo: {
id: 42,
name: 'yegor256/judges'
},
payload: {
action: 'created',
review: {
id: 2_210_067_609,
user: {
login: 'yegor256',
id: 43,
type: 'User'
},
state: 'commented',
author_association: 'NONE'
},
pull_request: {
id: 1_990_323_142,
number: 93,
state: 'open',
locked: false,
title: 'allows to push gizpped factbase',
user: {
login: 'test',
id: 526_200,
type: 'User',
site_admin: false
}
}
}
}
].to_json,
headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/user/42').to_return(
body: { id: 42, login: 'torvalds' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/user/43').to_return(
body: { id: 43, login: 'yegor256' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/user/526200').to_return(
body: { id: 526_200, login: 'test' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repos/yegor256/judges/pulls/93')
.to_return(
status: 200,
body: {
default_branch: 'master',
additions: 1,
deletions: 1,
comments: 1,
review_comments: 2,
commits: 2,
changed_files: 3
}.to_json,
headers: { 'content-type': 'application/json' }
)
fb = Factbase.new
load_it('github-events', fb)
f = fb.query('(eq what "pull-was-reviewed")').each.to_a
assert_equal(1, f.count)
assert_equal(42, f.first.who)
end

def test_skip_issue_was_opened_event
WebMock.disable_net_connect!
stub_request(:get, 'https://api.github.com/repos/foo/foo').to_return(
body: { id: 42, full_name: 'foo/foo' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repositories/42').to_return(
body: { id: 42, full_name: 'foo/foo' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repositories/42/events?per_page=100').to_return(
body: [
{
id: 40_623_323_541,
type: 'IssuesEvent',
public: true,
created_at: '2024-07-31 12:45:09 UTC',
actor: {
id: 42,
login: 'yegor256',
display_login: 'yegor256',
gravatar_id: '',
url: 'https://api.github.com/users/yegor256'
},
repo: {
id: 42,
name: 'yegor256/judges',
url: 'https://api.github.com/repos/yegor256/judges'
},
payload: {
action: 'opened',
issue: {
number: 1347,
state: 'open',
title: 'Found a bug',
body: "I'm having a problem with this."
}
}
}
].to_json,
headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/user/42').to_return(
body: { id: 42, login: 'torvalds' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repos/yegor256/judges/issues/1347').to_return(
status: 200,
body: { number: 1347, state: 'open' }.to_json,
headers: { 'content-type': 'application/json' }
)
fb = Factbase.new
op = fb.insert
op.event_id = 100_500
op.what = 'issue-was-opened'
op.where = 'github'
op.repository = 42
op.issue = 1347
load_it('github-events', fb)
f = fb.query('(eq what "issue-was-opened")').each.to_a
assert_equal(1, f.length)
end

def test_skip_issue_was_closed_event
WebMock.disable_net_connect!
stub_request(:get, 'https://api.github.com/repos/foo/foo').to_return(
body: { id: 42, full_name: 'foo/foo' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repositories/42').to_return(
body: { id: 42, full_name: 'foo/foo' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repositories/42/events?per_page=100').to_return(
body: [
{
id: 40_623_323_541,
type: 'IssuesEvent',
public: true,
created_at: '2024-07-31 12:45:09 UTC',
actor: {
id: 42,
login: 'yegor256',
display_login: 'yegor256',
gravatar_id: '',
url: 'https://api.github.com/users/yegor256'
},
repo: {
id: 42,
name: 'yegor256/judges',
url: 'https://api.github.com/repos/yegor256/judges'
},
payload: {
action: 'closed',
issue: {
number: 1347,
state: 'closed',
title: 'Found a bug',
body: "I'm having a problem with this."
}
}
}
].to_json,
headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/user/42').to_return(
body: { id: 42, login: 'torvalds' }.to_json, headers: {
'content-type': 'application/json'
}
)
stub_request(:get, 'https://api.github.com/repos/yegor256/judges/issues/1347').to_return(
status: 200,
body: { number: 1347, state: 'closed' }.to_json,
headers: { 'content-type': 'application/json' }
)
fb = Factbase.new
op = fb.insert
op.event_id = 100_500
op.what = 'issue-was-closed'
op.where = 'github'
op.repository = 42
op.issue = 1347
load_it('github-events', fb)
f = fb.query('(eq what "issue-was-closed")').each.to_a
assert_equal(1, f.length)
end

def test_watch_pull_request_review_events
WebMock.disable_net_connect!
stub_request(:get, 'https://api.github.com/repos/foo/foo').to_return(
Expand Down Expand Up @@ -282,6 +558,7 @@ def test_watch_pull_request_review_events
node_id: 'MDQ6VXNlcjUyNjMwMQ==',
type: 'User'
},
state: 'approved',
pull_request_url: 'https://api.github.com/repos/yegor256/judges/pulls/93',
author_association: 'OWNER',
_links: {
Expand Down Expand Up @@ -339,6 +616,7 @@ def test_watch_pull_request_review_events
node_id: 'MDQ6VXNlcjUyNjMwMQ==',
type: 'User'
},
state: 'approved',
pull_request_url: 'https://api.github.com/repos/yegor256/judges/pulls/93',
author_association: 'OWNER',
_links: {
Expand Down Expand Up @@ -396,6 +674,7 @@ def test_watch_pull_request_review_events
node_id: 'MDQ6VXNlcjUyNjMwMQ==',
type: 'User'
},
state: 'approved',
pull_request_url: 'https://api.github.com/repos/yegor256/judges/pulls/93',
author_association: 'OWNER',
_links: {
Expand Down

0 comments on commit bd41ed9

Please sign in to comment.