Class: LinterChanges::Linter::Base

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/linter_changes/linter/base.rb

Direct Known Subclasses

Eslint::Adapter, RuboCop::Adapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_files:, command:, force_global:) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/linter_changes/linter/base.rb', line 10

def initialize(config_files:, command:, force_global:)
  @name = T.must(self.class.name).split('::')[-2].then do |adapter_name|
    T.must(adapter_name).downcase
  end
  @config_files = config_files
  @command = command
  @base_command = @command.split(' ').first # used for listing files with in the adaptars
  @target_branch = ENV['CHANGE_TARGET']
  # We force origin if the target_branch is present
  @target_branch = "origin/#{@target_branch}" if !@target_branch.nil? && !@target_branch['origin']
  if @target_branch
    @git_diff = GitDiff.new(target_branch: @target_branch)
  end
  @force_global = force_global || @target_branch.nil?
  @force_global = true unless @git_diff&.references_exists?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/linter_changes/linter/base.rb', line 7

def name
  @name
end

Instance Method Details

#changed_filesObject



28
29
30
# File 'lib/linter_changes/linter/base.rb', line 28

def changed_files
  @git_diff.changed_files
end

#config_changed?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/linter_changes/linter/base.rb', line 40

def config_changed?
  @config_files.any? do |pattern|
    changed_files.any? { |file| file.match? Regexp.new(pattern) }
  end
end

#list_target_filesObject

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/linter_changes/linter/base.rb', line 34

def list_target_files
  raise NotImplementedError, "#{self.class} must implement #list_target_files"
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/linter_changes/linter/base.rb', line 48

def run
  if @force_global
    if @target_branch.nil?
      Logger.debug "[#{name.capitalize}] No git branch provided by CHANGE_TARGET enviroment variable, running globally."
    elsif !@git_diff.references_exists?
      Logger.debug "[#{name.capitalize}] Some git reference does not exists locally. Forced to run globally"
    else
      Logger.debug "[#{name.capitalize}] Forced to run globally."
    end
    execute_linter(@command)
  elsif config_changed?
    Logger.debug "[#{name.capitalize}] Configuration changed. Running linter globally."
    execute_linter(@command)
  else
    files_to_lint = list_target_files & changed_files
    if files_to_lint.empty?
      Logger.debug "No files to lint for [#{name.capitalize}]."
      true
    else
      Logger.debug "Linting files with [#{name.capitalize}]: #{files_to_lint.join(', ')}"
      execute_linter("#{@command} #{files_to_lint.join(' ')}")
    end
  end
end