churn_vs_complexity 1.9.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +56 -0
  3. data/README.md +46 -23
  4. data/lib/churn_vs_complexity/analysis_scope.rb +20 -0
  5. data/lib/churn_vs_complexity/churn.rb +27 -9
  6. data/lib/churn_vs_complexity/cli/main.rb +58 -19
  7. data/lib/churn_vs_complexity/cli/parser.rb +85 -121
  8. data/lib/churn_vs_complexity/cli.rb +2 -3
  9. data/lib/churn_vs_complexity/complexity/flog_calculator.rb +0 -3
  10. data/lib/churn_vs_complexity/complexity/lizard_calculator.rb +64 -0
  11. data/lib/churn_vs_complexity/complexity.rb +1 -7
  12. data/lib/churn_vs_complexity/complexity_validator.rb +1 -16
  13. data/lib/churn_vs_complexity/concurrent_calculator.rb +20 -23
  14. data/lib/churn_vs_complexity/delta/checker.rb +35 -21
  15. data/lib/churn_vs_complexity/delta/commit_hydrator.rb +1 -1
  16. data/lib/churn_vs_complexity/delta/complexity_annotator.rb +23 -5
  17. data/lib/churn_vs_complexity/delta/config.rb +15 -16
  18. data/lib/churn_vs_complexity/delta/factory.rb +2 -2
  19. data/lib/churn_vs_complexity/delta/multi_checker.rb +24 -25
  20. data/lib/churn_vs_complexity/delta/serializer.rb +6 -4
  21. data/lib/churn_vs_complexity/delta.rb +11 -49
  22. data/lib/churn_vs_complexity/diff/checker.rb +10 -3
  23. data/lib/churn_vs_complexity/diff/config.rb +29 -20
  24. data/lib/churn_vs_complexity/file_selector.rb +63 -114
  25. data/lib/churn_vs_complexity/focus/config.rb +42 -32
  26. data/lib/churn_vs_complexity/focus/serializer.rb +17 -4
  27. data/lib/churn_vs_complexity/gate/config.rb +28 -25
  28. data/lib/churn_vs_complexity/git_strategy.rb +17 -12
  29. data/lib/churn_vs_complexity/hotspots/config.rb +29 -25
  30. data/lib/churn_vs_complexity/language_registry.rb +108 -0
  31. data/lib/churn_vs_complexity/language_validator.rb +1 -3
  32. data/lib/churn_vs_complexity/normal/config.rb +31 -100
  33. data/lib/churn_vs_complexity/normal/serializer/graph.rb +3 -2
  34. data/lib/churn_vs_complexity/normal/serializer/summary.rb +18 -14
  35. data/lib/churn_vs_complexity/normal/serializer/summary_hash.rb +15 -13
  36. data/lib/churn_vs_complexity/normal/serializer.rb +2 -1
  37. data/lib/churn_vs_complexity/normal.rb +17 -13
  38. data/lib/churn_vs_complexity/risk_classifier.rb +3 -19
  39. data/lib/churn_vs_complexity/timetravel/config.rb +34 -47
  40. data/lib/churn_vs_complexity/timetravel/schedule.rb +15 -0
  41. data/lib/churn_vs_complexity/timetravel/serializer/stats_calculator.rb +36 -32
  42. data/lib/churn_vs_complexity/timetravel/serializer.rb +16 -24
  43. data/lib/churn_vs_complexity/timetravel/traveller.rb +44 -15
  44. data/lib/churn_vs_complexity/timetravel.rb +36 -14
  45. data/lib/churn_vs_complexity/triage/checker.rb +24 -28
  46. data/lib/churn_vs_complexity/triage/collaborator_set.rb +11 -0
  47. data/lib/churn_vs_complexity/triage/config.rb +29 -26
  48. data/lib/churn_vs_complexity/triage.rb +1 -0
  49. data/lib/churn_vs_complexity/validator_set.rb +9 -0
  50. data/lib/churn_vs_complexity/version.rb +1 -1
  51. data/lib/churn_vs_complexity.rb +3 -0
  52. metadata +18 -22
  53. data/lib/churn_vs_complexity/complexity/eslint_calculator.rb +0 -40
  54. data/lib/churn_vs_complexity/complexity/go_calculator.rb +0 -48
  55. data/lib/churn_vs_complexity/complexity/kotlin_calculator.rb +0 -54
  56. data/lib/churn_vs_complexity/complexity/pmd/files_calculator.rb +0 -26
  57. data/lib/churn_vs_complexity/complexity/pmd/folder_calculator.rb +0 -20
  58. data/lib/churn_vs_complexity/complexity/pmd.rb +0 -54
  59. data/lib/churn_vs_complexity/complexity/python_calculator.rb +0 -50
  60. data/lib/churn_vs_complexity/complexity/rust_calculator.rb +0 -54
  61. data/lib/churn_vs_complexity/complexity/swift_calculator.rb +0 -54
  62. data/tmp/eslint-support/complexity-calculator.js +0 -51
  63. data/tmp/eslint-support/package.json +0 -11
  64. data/tmp/pmd-support/ruleset.xml +0 -21
@@ -3,26 +3,11 @@
3
3
  module ChurnVsComplexity
4
4
  module FileSelector
5
5
  def self.extensions(language)
6
- case language
7
- when :java
8
- ['.java']
9
- when :ruby
10
- ['.rb']
11
- when :javascript
12
- ['.js', '.jsx', '.ts', '.tsx']
13
- when :python
14
- ['.py']
15
- when :go
16
- ['.go']
17
- when :kotlin
18
- ['.kt', '.kts']
19
- when :rust
20
- ['.rs']
21
- when :swift
22
- ['.swift']
23
- else
24
- raise Error, "Unsupported language: #{language}"
25
- end
6
+ LanguageRegistry.fetch(language).extensions
7
+ end
8
+
9
+ def self.for(language)
10
+ LanguageRegistry.file_selector_for(language)
26
11
  end
27
12
 
28
13
  module Any
@@ -33,27 +18,17 @@ module ChurnVsComplexity
33
18
  end
34
19
 
35
20
  class Excluding
36
- def initialize(extensions, excluded, convert_to_absolute_path = false)
21
+ def initialize(extensions, excluded)
37
22
  @extensions = extensions
38
23
  @excluded = excluded
39
- @convert_to_absolute_path = convert_to_absolute_path
40
24
  end
41
25
 
42
26
  def select_files(folder)
43
- were_excluded = []
44
- were_included = []
45
- candidates(folder).each do |f|
46
- if has_excluded_pattern?(f)
47
- were_excluded << f
48
- elsif has_correct_extension?(f) && File.file?(f)
49
- were_included << f
50
- end
51
- end
52
- if @convert_to_absolute_path
53
- were_excluded.map! { |f| File.absolute_path(f) }
54
- were_included.map! { |f| File.absolute_path(f) }
55
- end
56
- { explicitly_excluded: were_excluded, included: were_included }
27
+ were_excluded, were_included = partition_candidates(folder)
28
+ {
29
+ explicitly_excluded: were_excluded,
30
+ included: were_included,
31
+ }
57
32
  end
58
33
 
59
34
  protected
@@ -64,18 +39,35 @@ module ChurnVsComplexity
64
39
 
65
40
  private
66
41
 
67
- def has_correct_extension?(file_path)
42
+ def partition_candidates(folder)
43
+ excluded = []
44
+ included = []
45
+ candidates(folder).each do |file|
46
+ if excluded_pattern?(file)
47
+ excluded << file
48
+ elsif include_candidate?(file)
49
+ included << file
50
+ end
51
+ end
52
+ [excluded, included]
53
+ end
54
+
55
+ def include_candidate?(file)
56
+ correct_extension?(file) && File.file?(file)
57
+ end
58
+
59
+ def correct_extension?(file_path)
68
60
  @extensions.any? { |e| file_path.end_with?(e) }
69
61
  end
70
62
 
71
- def has_excluded_pattern?(file_path)
63
+ def excluded_pattern?(file_path)
72
64
  @excluded.any? { |e| file_path.include?(e) }
73
65
  end
74
66
  end
75
67
 
76
68
  class Predefined < Excluding
77
- def initialize(included:, extensions:, excluded:, convert_to_absolute_path: false)
78
- super(extensions, excluded, convert_to_absolute_path)
69
+ def initialize(included:, extensions:, excluded:)
70
+ super(extensions, excluded)
79
71
  @included = included
80
72
  end
81
73
 
@@ -84,87 +76,44 @@ module ChurnVsComplexity
84
76
  def candidates(*) = @included
85
77
  end
86
78
 
87
- module Java
88
- def self.excluding(excluded)
89
- Excluding.new(FileSelector.extensions(:java), excluded)
79
+ class Factory
80
+ def initialize(extensions:, default_excludes: [])
81
+ @extensions = extensions.freeze
82
+ @default_excludes = default_excludes.freeze
90
83
  end
91
84
 
92
- def self.predefined(included:, excluded:)
93
- Predefined.new(included:, extensions: FileSelector.extensions(:java), excluded:)
94
- end
95
- end
96
-
97
- module Ruby
98
- def self.excluding(excluded)
99
- Excluding.new(FileSelector.extensions(:ruby), excluded)
85
+ def excluding(excluded)
86
+ Excluding.new(@extensions, @default_excludes + excluded)
100
87
  end
101
88
 
102
- def self.predefined(included:, excluded:)
103
- Predefined.new(included:, extensions: FileSelector.extensions(:ruby), excluded:)
89
+ def predefined(included:, excluded:)
90
+ Predefined.new(
91
+ included:,
92
+ extensions: @extensions,
93
+ excluded: @default_excludes + excluded,
94
+ )
104
95
  end
105
96
  end
106
97
 
107
- module JavaScript
108
- def self.excluding(excluded)
109
- Excluding.new(FileSelector.extensions(:javascript), excluded, true)
110
- end
111
-
112
- def self.predefined(included:, excluded:)
113
- Predefined.new(included:, extensions: FileSelector.extensions(:javascript), excluded:,
114
- convert_to_absolute_path: true,)
115
- end
116
- end
117
-
118
- module Python
119
- DEFAULT_EXCLUDES = %w[venv .venv env .env __pycache__ .tox site-packages].freeze
120
-
121
- def self.excluding(excluded)
122
- Excluding.new(FileSelector.extensions(:python), DEFAULT_EXCLUDES + excluded)
123
- end
124
-
125
- def self.predefined(included:, excluded:)
126
- Predefined.new(included:, extensions: FileSelector.extensions(:python), excluded: DEFAULT_EXCLUDES + excluded)
127
- end
128
- end
129
-
130
- module Go
131
- def self.excluding(excluded)
132
- Excluding.new(FileSelector.extensions(:go), excluded)
133
- end
134
-
135
- def self.predefined(included:, excluded:)
136
- Predefined.new(included:, extensions: FileSelector.extensions(:go), excluded:)
137
- end
138
- end
139
-
140
- module Kotlin
141
- def self.excluding(excluded)
142
- Excluding.new(FileSelector.extensions(:kotlin), excluded)
143
- end
144
-
145
- def self.predefined(included:, excluded:)
146
- Predefined.new(included:, extensions: FileSelector.extensions(:kotlin), excluded:)
147
- end
148
- end
149
-
150
- module Rust
151
- def self.excluding(excluded)
152
- Excluding.new(FileSelector.extensions(:rust), excluded)
153
- end
154
-
155
- def self.predefined(included:, excluded:)
156
- Predefined.new(included:, extensions: FileSelector.extensions(:rust), excluded:)
157
- end
158
- end
159
-
160
- module Swift
161
- def self.excluding(excluded)
162
- Excluding.new(FileSelector.extensions(:swift), excluded)
163
- end
164
-
165
- def self.predefined(included:, excluded:)
166
- Predefined.new(included:, extensions: FileSelector.extensions(:swift), excluded:)
167
- end
98
+ {
99
+ Java: :java,
100
+ Ruby: :ruby,
101
+ JavaScript: :javascript,
102
+ Python: :python,
103
+ Go: :go,
104
+ Kotlin: :kotlin,
105
+ Rust: :rust,
106
+ Swift: :swift,
107
+ }.each do |const_name, language|
108
+ const_set(
109
+ const_name,
110
+ Module.new do
111
+ define_singleton_method(:excluding) { |excluded| FileSelector.for(language).excluding(excluded) }
112
+ define_singleton_method(:predefined) do |included:, excluded:|
113
+ FileSelector.for(language).predefined(included:, excluded:)
114
+ end
115
+ end,
116
+ )
168
117
  end
169
118
  end
170
119
  end
@@ -4,63 +4,73 @@ module ChurnVsComplexity
4
4
  module Focus
5
5
  class Config
6
6
  DEFAULT_BASELINE_PATH = '.focus-baseline.json'
7
+ SUPPORTED_SERIALIZERS = %i[json].freeze
8
+ Session = Data.define(:subcommand, :baseline_path)
7
9
 
8
- def initialize(
9
- language:,
10
- subcommand:,
11
- serializer: :json,
12
- since: nil,
13
- excluded: [],
14
- baseline_path: DEFAULT_BASELINE_PATH,
15
- complexity_validator: ComplexityValidator,
16
- since_validator: Normal::SinceValidator,
17
- **options
18
- )
19
- @language = language
20
- @subcommand = subcommand.to_sym
10
+ def self.from_options(options)
11
+ session = Session.new(
12
+ subcommand: options[:subcommand].to_sym,
13
+ baseline_path: options[:baseline_path] || DEFAULT_BASELINE_PATH,
14
+ )
15
+ new(
16
+ scope: AnalysisScope.from_options(options),
17
+ session:,
18
+ serializer: options[:serializer] || :json,
19
+ )
20
+ end
21
+
22
+ def initialize(scope:, session:, serializer: :json, validators: ValidatorSet.new)
23
+ @scope = scope
24
+ @session = session
21
25
  @serializer = serializer
22
- @since = since
23
- @excluded = excluded
24
- @baseline_path = baseline_path
25
- @complexity_validator = complexity_validator
26
- @since_validator = since_validator
27
- @options = options
26
+ @validators = validators
28
27
  end
29
28
 
30
29
  def validate!
31
- LanguageValidator.validate!(@language)
32
- @since_validator.validate!(since: @since, relative_period: nil)
33
- @complexity_validator.validate!(@language)
30
+ LanguageValidator.validate!(@scope.language)
31
+ validate_serializer!
32
+ validate_period!
33
+ @validators.complexity.validate!(@scope.language)
34
34
  validate_subcommand!
35
35
  end
36
36
 
37
37
  def checker
38
38
  Checker.new(
39
39
  engine: normal_engine,
40
- subcommand: @subcommand,
40
+ subcommand: @session.subcommand,
41
41
  serializer: focus_serializer,
42
- baseline_path: @baseline_path,
43
- language: @language,
42
+ baseline_path: @session.baseline_path,
43
+ language: @scope.language,
44
44
  )
45
45
  end
46
46
 
47
47
  private
48
48
 
49
49
  def normal_engine
50
- Normal::Config.new(language: @language, serializer: :none, since: @since, excluded: @excluded).checker
50
+ Normal::Config.new(scope: @scope, serializer: :none).checker
51
+ end
52
+
53
+ def validate_serializer!
54
+ return if SUPPORTED_SERIALIZERS.include?(@serializer)
55
+
56
+ raise ValidationError, "Unsupported serializer: #{@serializer}"
57
+ end
58
+
59
+ def validate_period!
60
+ @validators.since.validate!(since: @scope.since, relative_period: @scope.relative_period)
61
+ Normal::RelativePeriodValidator.validate!(relative_period: @scope.relative_period)
51
62
  end
52
63
 
53
64
  def validate_subcommand!
54
- return if %i[start end].include?(@subcommand)
65
+ return if %i[start end].include?(@session.subcommand)
55
66
 
56
- raise ValidationError, "Invalid focus subcommand: #{@subcommand}. Use 'start' or 'end'."
67
+ raise ValidationError, "Invalid focus subcommand: #{@session.subcommand}. Use 'start' or 'end'."
57
68
  end
58
69
 
59
70
  def focus_serializer
60
- case @subcommand
61
- when :start then nil # start doesn't need a serializer
62
- when :end then Serializer::Json
63
- end
71
+ return if @session.subcommand == :start
72
+
73
+ Serializer::Json if @serializer == :json
64
74
  end
65
75
  end
66
76
  end
@@ -11,17 +11,29 @@ module ChurnVsComplexity
11
11
  end
12
12
 
13
13
  def self.build_report(baseline, current)
14
- before_gammas = baseline['files'].to_h { |f| [f['file'], f['gamma_score']] }
15
- after_gammas = current.to_h { |e| [e[:file], e[:gamma_score]] }
14
+ before_gammas = baseline_gammas(baseline)
15
+ after_gammas = current_gammas(current)
16
16
  touched = find_touched_files(before_gammas, after_gammas)
17
17
 
18
18
  JSON.generate(
19
19
  session: session_data(started: baseline['timestamp'], files_modified: touched.size),
20
- impact: impact_data(before: mean(before_gammas.values), after: mean(after_gammas.values)),
20
+ impact: report_impact(before_gammas, after_gammas),
21
21
  files_touched: touched,
22
22
  )
23
23
  end
24
24
 
25
+ def self.baseline_gammas(baseline)
26
+ baseline['files'].to_h { |entry| [entry['file'], entry['gamma_score']] }
27
+ end
28
+
29
+ def self.current_gammas(current)
30
+ current.to_h { |entry| [entry[:file], entry[:gamma_score]] }
31
+ end
32
+
33
+ def self.report_impact(before_gammas, after_gammas)
34
+ impact_data(before: mean(before_gammas.values), after: mean(after_gammas.values))
35
+ end
36
+
25
37
  def self.build_fallback_report(current)
26
38
  after_scores = current.map { |e| e[:gamma_score] }
27
39
 
@@ -89,7 +101,8 @@ module ChurnVsComplexity
89
101
  new_gamma > old_gamma ? 'Complexity increased. Consider adding tests.' : 'Complexity decreased. Good.'
90
102
  end
91
103
 
92
- private_class_method :build_report, :build_fallback_report, :session_data, :impact_data,
104
+ private_class_method :build_report, :baseline_gammas, :current_gammas, :report_impact,
105
+ :build_fallback_report, :session_data, :impact_data,
93
106
  :mean, :direction, :find_touched_files, :build_touched_entry,
94
107
  :test_file_exists?, :recommend
95
108
  end
@@ -4,46 +4,49 @@ module ChurnVsComplexity
4
4
  module Gate
5
5
  class Config
6
6
  DEFAULT_MAX_GAMMA = 25
7
+ SUPPORTED_SERIALIZERS = %i[json].freeze
7
8
 
8
- def initialize(
9
- language:,
10
- serializer: :json,
11
- max_gamma: DEFAULT_MAX_GAMMA,
12
- since: nil,
13
- excluded: [],
14
- complexity_validator: ComplexityValidator,
15
- since_validator: Normal::SinceValidator,
16
- **options
17
- )
18
- @language = language
9
+ def self.from_options(options)
10
+ new(
11
+ scope: AnalysisScope.from_options(options),
12
+ serializer: options[:serializer] || :json,
13
+ max_gamma: options[:max_gamma] || DEFAULT_MAX_GAMMA,
14
+ )
15
+ end
16
+
17
+ def initialize(scope:, serializer: :json, max_gamma: DEFAULT_MAX_GAMMA, validators: ValidatorSet.new)
18
+ @scope = scope
19
19
  @serializer = serializer
20
20
  @max_gamma = max_gamma
21
- @since = since
22
- @excluded = excluded
23
- @complexity_validator = complexity_validator
24
- @since_validator = since_validator
25
- @options = options
21
+ @validators = validators
26
22
  end
27
23
 
28
24
  def validate!
29
- LanguageValidator.validate!(@language)
30
- @since_validator.validate!(since: @since, relative_period: nil)
31
- @complexity_validator.validate!(@language)
25
+ LanguageValidator.validate!(@scope.language)
26
+ validate_serializer!
27
+ validate_period!
28
+ @validators.complexity.validate!(@scope.language)
32
29
  validate_max_gamma!
33
30
  end
34
31
 
35
32
  def checker
36
- normal_config = Normal::Config.new(
37
- language: @language,
38
- serializer: :none,
39
- since: @since,
40
- excluded: @excluded,
41
- )
33
+ normal_config = Normal::Config.new(scope: @scope, serializer: :none)
42
34
  Checker.new(engine: normal_config.checker, serializer: gate_serializer, max_gamma: @max_gamma)
43
35
  end
44
36
 
45
37
  private
46
38
 
39
+ def validate_serializer!
40
+ return if SUPPORTED_SERIALIZERS.include?(@serializer)
41
+
42
+ raise ValidationError, "Unsupported serializer: #{@serializer}"
43
+ end
44
+
45
+ def validate_period!
46
+ @validators.since.validate!(since: @scope.since, relative_period: @scope.relative_period)
47
+ Normal::RelativePeriodValidator.validate!(relative_period: @scope.relative_period)
48
+ end
49
+
47
50
  def validate_max_gamma!
48
51
  return unless @max_gamma.negative?
49
52
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'open3'
4
+
3
5
  module ChurnVsComplexity
4
6
  class GitStrategy
5
7
  EMPTY_TREE_SHA = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
@@ -12,7 +14,7 @@ module ChurnVsComplexity
12
14
  def valid_commit?(commit:)
13
15
  @repo.object(commit)
14
16
  true
15
- rescue Git::GitExecuteError
17
+ rescue Git::Error
16
18
  false
17
19
  end
18
20
 
@@ -22,7 +24,7 @@ module ChurnVsComplexity
22
24
 
23
25
  def surrounding(commit:)
24
26
  current = object(commit)
25
- child_sha = `git -C #{@folder} log --reverse --ancestry-path #{current.sha}..HEAD --format=%H`.lines.first&.chomp
27
+ child_sha = next_commit_sha(current.sha)
26
28
  next_commit = child_sha.nil? || child_sha.empty? ? nil : child_sha
27
29
  [current.parent&.sha, next_commit]
28
30
  end
@@ -35,18 +37,16 @@ module ChurnVsComplexity
35
37
  end
36
38
 
37
39
  def checkout_in_worktree(worktree_folder, sha)
38
- command = "(cd #{worktree_folder} && git checkout #{sha}) > /dev/null 2>&1"
39
- `#{command}`
40
+ Open3.capture3('git', '-C', worktree_folder, 'checkout', sha)
40
41
  end
41
42
 
42
43
  # Both ends are given an explicit time. A bare date is an approximate date
43
44
  # to git, resolved against the clock and the timezone, so the window it
44
45
  # produces is not the window that was asked for.
45
46
  def commits_in(git_period)
46
- @repo.log(1_000_000)
47
- .since(start_of_day(git_period.effective_start_date))
48
- .until(end_of_day(git_period.end_date))
49
- .execute.to_a
47
+ log = @repo.log(1_000_000).until(end_of_day(git_period.end_date))
48
+ log = log.since(start_of_day(git_period.effective_start_date)) if git_period.requested_start_date
49
+ log.execute.to_a
50
50
  end
51
51
 
52
52
  def start_of_day(date) = date.strftime('%Y-%m-%dT00:00:00')
@@ -65,17 +65,22 @@ module ChurnVsComplexity
65
65
  end
66
66
 
67
67
  def add_worktree(wt_folder)
68
- command = "(cd #{@folder} && git worktree add -f #{wt_folder}) > /dev/null 2>&1"
69
- `#{command}`
68
+ Open3.capture3('git', '-C', @folder, 'worktree', 'add', '--detach', '-f', wt_folder)
70
69
  end
71
70
 
72
71
  def remove_worktree(worktree_folder)
73
- command = "(cd #{worktree_folder} && git worktree remove -f #{worktree_folder}) > /dev/null 2>&1"
74
- `#{command}`
72
+ Open3.capture3('git', '-C', @folder, 'worktree', 'remove', '-f', worktree_folder)
75
73
  end
76
74
 
77
75
  private
78
76
 
77
+ def next_commit_sha(sha)
78
+ output, = Open3.capture2(
79
+ 'git', '-C', @folder, 'log', '--reverse', '--ancestry-path', "#{sha}..HEAD", '--format=%H'
80
+ )
81
+ output.lines.first&.chomp
82
+ end
83
+
79
84
  def diff_from_parent(commit_object)
80
85
  base = commit_object.parent
81
86
  base ? base.diff(commit_object) : @repo.diff(EMPTY_TREE_SHA, commit_object.sha)
@@ -3,42 +3,46 @@
3
3
  module ChurnVsComplexity
4
4
  module Hotspots
5
5
  class Config
6
- def initialize(
7
- language:,
8
- serializer: :json,
9
- since: nil,
10
- excluded: [],
11
- complexity_validator: ComplexityValidator,
12
- since_validator: Normal::SinceValidator,
13
- **options
14
- )
15
- @language = language
6
+ SUPPORTED_SERIALIZERS = %i[json markdown].freeze
7
+
8
+ def self.from_options(options)
9
+ new(
10
+ scope: AnalysisScope.from_options(options),
11
+ serializer: options[:serializer] || :json,
12
+ )
13
+ end
14
+
15
+ def initialize(scope:, serializer: :json, validators: ValidatorSet.new)
16
+ @scope = scope
16
17
  @serializer = serializer
17
- @since = since
18
- @excluded = excluded
19
- @complexity_validator = complexity_validator
20
- @since_validator = since_validator
21
- @options = options
18
+ @validators = validators
22
19
  end
23
20
 
24
21
  def validate!
25
- LanguageValidator.validate!(@language)
26
- @since_validator.validate!(since: @since, relative_period: nil)
27
- @complexity_validator.validate!(@language)
22
+ LanguageValidator.validate!(@scope.language)
23
+ validate_serializer!
24
+ validate_period!
25
+ @validators.complexity.validate!(@scope.language)
28
26
  end
29
27
 
30
28
  def checker
31
- normal_config = Normal::Config.new(
32
- language: @language,
33
- serializer: :none,
34
- since: @since,
35
- excluded: @excluded,
36
- )
37
- Checker.new(engine: normal_config.checker, serializer: hotspots_serializer, language: @language)
29
+ normal_config = Normal::Config.new(scope: @scope, serializer: :none)
30
+ Checker.new(engine: normal_config.checker, serializer: hotspots_serializer, language: @scope.language)
38
31
  end
39
32
 
40
33
  private
41
34
 
35
+ def validate_serializer!
36
+ return if SUPPORTED_SERIALIZERS.include?(@serializer)
37
+
38
+ raise ValidationError, "Unsupported serializer: #{@serializer}"
39
+ end
40
+
41
+ def validate_period!
42
+ @validators.since.validate!(since: @scope.since, relative_period: @scope.relative_period)
43
+ Normal::RelativePeriodValidator.validate!(relative_period: @scope.relative_period)
44
+ end
45
+
42
46
  def hotspots_serializer
43
47
  case @serializer
44
48
  when :json then Serializer::Json