Skip to content

Commit 234fe7a

Browse files
author
bmarkons
committed
Make CommitsRunner create and run from webhook
1 parent 366a9bc commit 234fe7a

8 files changed

Lines changed: 113 additions & 95 deletions

File tree

app/services/benchmark_pool.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ def self.enqueue(initiator_type, initiator, repo_name, options = {})
1313
raise ArgumentError, "unknown repo: #{repo_name}"
1414
end
1515
end
16-
1716
end

app/services/commits_runner.rb

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
1-
class CommitsRunner
2-
def self.run(commits, pattern = '')
3-
commits.select { |commit| valid?(commit) }
4-
.each { |commit| create_and_run(commit, pattern) }
5-
.count
1+
module CommitsRunner
2+
def self.run(trigger_source, commits, repo, pattern = '')
3+
formatted_commits =
4+
if trigger_source == :webhook
5+
format_webhook(commits, repo)
6+
elsif trigger_source == :api
7+
format_api(commits, repo)
8+
end
9+
10+
formatted_commits.select { |commit| valid?(commit) }
11+
.each { |commit| create_and_run(commit, pattern) }
12+
.count
613
end
714

815
private
916

17+
def self.format_api(commits, repo)
18+
commits.map do |commit|
19+
{
20+
sha: commit['sha'],
21+
message: commit['commit']['message'],
22+
repo: repo,
23+
url: commit['html_url'],
24+
created_at: commit['commit']['author']['date'],
25+
author_name: commit['commit']['author']['name']
26+
}
27+
end
28+
end
29+
30+
def self.format_webhook(commits, repo)
31+
commits.map do |commit|
32+
{
33+
sha: commit['id'],
34+
message: commit['message'],
35+
repo: repo,
36+
url: commit['url'],
37+
created_at: commit['timestamp'],
38+
author_name: commit['author']['name']
39+
}
40+
end
41+
end
42+
1043
def self.valid?(commit)
1144
!Commit.merge_or_skip_ci?(commit[:message]) && Commit.valid_author?(commit[:author_name])
1245
end
@@ -20,8 +53,9 @@ def self.create_and_run(commit, pattern)
2053
end
2154

2255
BenchmarkPool.enqueue(
23-
commit[:repo].name,
56+
:commit,
2457
commit[:sha],
58+
commit[:repo].name,
2559
include_patterns: pattern
2660
)
2761
end

app/services/github_event_handler.rb

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,14 @@ def branch_from_payload
2525
@payload['ref'].split('/')[2]
2626
end
2727

28-
# Grabs the commits hash and starts job to run benchmarks on remote server.
2928
def process_push
30-
repo = first_or_create_repo(@payload['repository'])
29+
repo = find_or_create_repo(@payload['repository'])
3130
commits = @payload['commits'] || [@payload['head_commit']]
3231

33-
commits.each do |commit|
34-
if create_commit(commit, repo.id)
35-
BenchmarkPool.enqueue(repo.name, commit['id'])
36-
end
37-
end
32+
CommitsRunner.run(:webhook, commits, repo)
3833
end
3934

40-
def first_or_create_repo(repository)
35+
def find_or_create_repo(repository)
4136
organization_name, repo_name = parse_full_name(repository['full_name'])
4237
repository_url = repository['html_url']
4338

@@ -65,19 +60,4 @@ def parse_full_name(full_name)
6560
full_name =~ /\A(\w+)\/(\w+)/
6661
[$1, $2]
6762
end
68-
69-
def create_commit(commit, repo_id)
70-
if valid_commit?(commit)
71-
Commit.find_or_create_by(sha1: commit['id']) do |c|
72-
c.url = commit['url']
73-
c.message = commit['message']
74-
c.repo_id = repo_id
75-
c.created_at = commit['timestamp']
76-
end
77-
end
78-
end
79-
80-
def valid_commit?(commit)
81-
!Commit.merge_or_skip_ci?(commit['message']) && Commit.valid_author?(commit['author']['name'])
82-
end
8363
end

app/services/manual_runner.rb

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,7 @@ def run_paginated(commits_count, page: 1, pattern: '')
2525
end
2626

2727
def run_commits(page: 1, per_page: 100, pattern: '')
28-
fetched_commits = @octokit.commits("#{@repo.organization.name}/#{@repo.name}", per_page: per_page, page: page)
29-
formatted_commits = format_commits(fetched_commits)
30-
CommitsRunner.run(formatted_commits, pattern)
31-
end
32-
33-
def format_commits(commits)
34-
commits.map do |commit|
35-
{
36-
sha: commit['sha'],
37-
message: commit['commit']['message'],
38-
repo: @repo,
39-
url: commit['html_url'],
40-
created_at: commit['commit']['author']['date'],
41-
author_name: commit['commit']['author']['name']
42-
}
43-
end
28+
commits = @octokit.commits("#{@repo.organization.name}/#{@repo.name}", per_page: per_page, page: page)
29+
CommitsRunner.run(:api, commits, @repo, pattern)
4430
end
4531
end

test/integration/github_event_handler_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class GithubEventHandlerTest < ActionDispatch::IntegrationTest
44
test '#handle for single commits pushed' do
5-
BenchmarkPool.expects(:enqueue).with('ruby', '12345')
5+
BenchmarkPool.expects(:enqueue).with(:commit, '12345', 'ruby', include_patterns: '')
66

77
post_to_handler('ref' => 'refs/heads/master',
88
'head_commit' => {
@@ -31,8 +31,8 @@ class GithubEventHandlerTest < ActionDispatch::IntegrationTest
3131
end
3232

3333
test '#handle for multiple commits pushed' do
34-
BenchmarkPool.expects(:enqueue).with('ruby', '12345')
35-
BenchmarkPool.expects(:enqueue).with('ruby', '12346')
34+
BenchmarkPool.expects(:enqueue).with(:commit, '12345', 'ruby', include_patterns: '')
35+
BenchmarkPool.expects(:enqueue).with(:commit, '12346', 'ruby', include_patterns: '')
3636

3737
post_to_handler('ref' => 'refs/heads/master',
3838
'commits' =>

test/services/benchmark_pool_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BenchmarkPoolTest < ActiveSupport::TestCase
1212
BenchmarkPool.enqueue(:release, 'abc', 'ruby')
1313
end
1414

15-
assert_enqueued_jobs 2
15+
assert_enqueued_jobs 2
1616
end
1717

1818
test '#enqueue for rails' do
@@ -24,14 +24,14 @@ class BenchmarkPoolTest < ActiveSupport::TestCase
2424
BenchmarkPool.enqueue(:release, 'abc', 'rails')
2525
end
2626

27-
assert_enqueued_jobs 2
27+
assert_enqueued_jobs 2
2828
end
2929

3030
test '#enqueue for sequel' do
3131
assert_enqueued_with(job: RemoteServerJob) do
3232
BenchmarkPool.enqueue(:commit, 'abc', 'sequel')
3333
end
34-
34+
3535
assert_enqueued_with(job: RemoteServerJob) do
3636
BenchmarkPool.enqueue(:release, 'abc', 'sequel')
3737
end
@@ -47,7 +47,7 @@ class BenchmarkPoolTest < ActiveSupport::TestCase
4747
assert_enqueued_with(job: RemoteServerJob) do
4848
BenchmarkPool.enqueue(:release, 'abc', 'ruby-pg')
4949
end
50-
51-
assert_enqueued_jobs 2
50+
51+
assert_enqueued_jobs 2
5252
end
5353
end

test/services/commits_runner_test.rb

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,56 @@ class CommitsRunnerTest < ActiveSupport::TestCase
77
@repo = create(:repo, name: 'rails')
88
end
99

10-
test '#run' do
10+
test '#run commits triggered by webhook' do
1111
commits = [
1212
{
13-
sha: '12345',
14-
message: 'My beautiful commit message',
15-
repo: @repo,
16-
author_name: 'bmarkons',
17-
url: 'https://github.com/commit',
18-
created_at: 12345
13+
'id' => '12345',
14+
'message' => 'My beautiful commit message',
15+
'author' => {
16+
'name' => 'bmarkons'
17+
},
18+
'url' => 'https://github.com/commit',
19+
'timestamp' => 12345
1920
}
2021
]
2122

22-
CommitsRunner.run(commits)
23+
CommitsRunner.run(:webhook, commits, @repo)
2324

2425
expect_created(commits)
2526
assert_enqueued_jobs(commits.count)
2627
end
2728

28-
def expect_created(commits_hashes)
29-
commits_hashes.each do |hash|
30-
commit = Commit.find_by!(sha1: hash[:sha])
29+
test '#run commits triggered manually' do
30+
commits = [
31+
{
32+
'sha' => '12345',
33+
'commit' => {
34+
'message' => 'My beautiful commit message',
35+
'author' => {
36+
'date' => 12345,
37+
'name' => 'bmarkons'
38+
}
39+
},
40+
'html_url' => 'https://github.com/commit'
41+
}
42+
]
43+
44+
CommitsRunner.run(:api, commits, @repo)
45+
46+
expect_created(commits)
47+
assert_enqueued_jobs(commits.count)
48+
end
49+
50+
def expect_created(commit_hashes)
51+
commit_hashes.each do |commit_hash|
52+
commit = Commit.find_by!(sha1: commit_hash['sha'] || commit_hash['id'])
3153

32-
assert_equal hash[:sha], commit.sha1
33-
assert_equal hash[:message], commit.message
34-
assert_equal hash[:repo].id, commit.repo.id
35-
assert_equal hash[:repo].name, commit.repo.name
36-
assert_equal hash[:url], commit.url
37-
refute_equal hash[:created_at], commit.created_at
54+
assert_equal commit_hash['id'] || commit_hash['sha'], commit.sha1
55+
assert_equal commit_hash['message'] || commit_hash['commit']['message'], commit.message
56+
assert_equal @repo.id, commit.repo.id
57+
assert_equal @repo.name, commit.repo.name
58+
assert_equal commit_hash['url'] || commit_hash['html_url'], commit.url
59+
refute_equal commit_hash['timestamp'] || commit_hash['commit']['author']['date'], commit.created_at
3860
end
3961
end
4062
end

test/services/manual_runner_test.rb

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,7 @@ class ManualRunnerTest < ActiveSupport::TestCase
1717
organization = create(:organization, name: 'jeremyevans')
1818
repo = create(:repo, name: 'sequel', organization: organization)
1919

20-
CommitsRunner.expects(:run).times(2).returns(100).with do |commits|
21-
commits.each do |commit|
22-
assert commit[:sha]
23-
assert commit[:message]
24-
assert commit[:url]
25-
assert commit[:created_at]
26-
assert commit[:repo]
27-
assert commit[:author_name]
28-
end
29-
30-
assert 100, commits.count
31-
end
20+
expect_to_run(100, 2)
3221

3322
VCR.use_cassette('github 200 commits') do
3423
ManualRunner.new(repo).run_last(200)
@@ -39,21 +28,29 @@ class ManualRunnerTest < ActiveSupport::TestCase
3928
organization = create(:organization, name: 'jeremyevans')
4029
repo = create(:repo, name: 'sequel', organization: organization)
4130

42-
CommitsRunner.expects(:run).times(1).returns(20).with do |commits|
43-
commits.each do |commit|
44-
assert commit[:sha]
45-
assert commit[:message]
46-
assert commit[:url]
47-
assert commit[:created_at]
48-
assert commit[:repo]
49-
assert commit[:author_name]
50-
end
51-
52-
assert_equal 20, commits.count
53-
end
31+
expect_to_run(20, 1)
5432

5533
VCR.use_cassette('github 20 commits') do
5634
ManualRunner.new(repo).run_last(20)
5735
end
5836
end
37+
38+
private
39+
40+
def expect_to_run(count, times)
41+
CommitsRunner.expects(:run).times(times).returns(count).with do |source, commits, repo, pattern|
42+
commits.each do |commit|
43+
assert commit['sha']
44+
assert commit['commit']['message']
45+
assert commit['html_url']
46+
assert commit['commit']['author']['date']
47+
assert commit['commit']['author']['name']
48+
assert repo
49+
assert_equal '', pattern
50+
assert_equal :api, source
51+
end
52+
53+
assert_equal count, commits.count
54+
end
55+
end
5956
end

0 commit comments

Comments
 (0)