-
Notifications
You must be signed in to change notification settings - Fork 10
/
profiles.rb
115 lines (86 loc) · 3.53 KB
/
profiles.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true
module SimpleCov::Buildkite::Profiles
def self.run(*args)
IO.popen(args, &:read).tap do
$?.success? or fail("Command exited with status #{$?.exitstatus}: #{args.join(" ")}")
end.chomp
end
def self.git(*args)
run 'git',
*args
end
def self.git_diff_names(*args, diff_filter: '')
git('diff',
'--name-only',
"--diff-filter=#{diff_filter}",
*args).split "\n"
end
def self.git_short_commit(commit)
git 'rev-parse',
'--short',
commit
end
def self.git_merge_base(*refs)
git 'merge-base',
*refs
end
SimpleCov.profiles.define 'buildkite' do
STDERR.puts 'SimpleCov::Buildkite profile initialising...'
fail('Not running on Buildkite') unless ENV['BUILDKITE'] == 'true'
branch_name = ENV['BUILDKITE_BRANCH']
STDERR.puts "branch_name=#{branch_name}"
base_branch_name = ENV['BUILDKITE_PULL_REQUEST_BASE_BRANCH']
if base_branch_name.nil? || base_branch_name.empty?
base_branch_name = ENV['BUILDKITE_PIPELINE_DEFAULT_BRANCH']
end
STDERR.puts "base_branch_name=#{base_branch_name}"
current_commit = ENV['BUILDKITE_COMMIT']
STDERR.puts "current_commit=#{current_commit}"
current_commit_short = git_short_commit(current_commit)
STDERR.puts "current_commit_short=#{current_commit_short}"
changed_files_in_commit = git_diff_names(current_commit,
diff_filter: 'd')
STDERR.puts "changed_files_in_commit.count=#{changed_files_in_commit.count}"
add_group "Files changed in #{current_commit_short}" do |tested_file|
changed_files_in_commit.detect do |changed_file|
tested_file.filename.end_with?(changed_file)
end
end
added_files_in_commit = git_diff_names(current_commit,
diff_filter: 'A')
STDERR.puts "added_files_in_commit.count=#{added_files_in_commit.count}"
add_group "Files added in #{current_commit_short}" do |tested_file|
added_files_in_commit.detect do |added_file|
tested_file.filename.end_with?(added_file)
end
end
# Compare with the base branch if it's not this branch
if base_branch_name && base_branch_name != branch_name
merge_base = git_merge_base(current_commit,
base_branch_name)
merge_base_short = git_short_commit(merge_base)
STDERR.puts "merge_base=#{merge_base}"
STDERR.puts "merge_base_short=#{merge_base_short}"
changed_files_in_branch = git_diff_names(merge_base,
current_commit,
diff_filter: 'd')
STDERR.puts "changed_files_in_branch.count=#{changed_files_in_branch.count}"
add_group "Files changed in #{merge_base_short}...#{current_commit_short}" do |tested_file|
changed_files_in_branch.detect do |changed_file|
tested_file.filename.end_with?(changed_file)
end
end
added_files_in_branch = git_diff_names(merge_base,
current_commit,
diff_filter: 'A')
STDERR.puts "added_files_in_branch.count=#{added_files_in_branch.count}"
add_group "Files added in #{merge_base_short}...#{current_commit_short}" do |tested_file|
added_files_in_branch.detect do |added_file|
tested_file.filename.end_with?(added_file)
end
end
end
rescue RuntimeError => error
STDERR.puts error
end
end