class Linterbot::RunnerConfiguration

Constants

DEFAULT_CONFIG_FILE_PATH
DEFAULT_PROJECT_BASE_PATH

Attributes

approver_class[RW]
commenter_class[RW]
github_client[RW]
linter_report_file[RW]
project_base_path[RW]

Public Class Methods

configuration!(options) click to toggle source
# File lib/linterbot/runner_configuration.rb, line 68
def configuration!(options)
  config_file_path = options.config_file_path || File.expand_path(DEFAULT_CONFIG_FILE_PATH)
  loaded_config = load_config_file(config_file_path)
  base_config = default_configuration.merge(loaded_config)

  github_access_token = ENV["GITHUB_ACCESS_TOKEN"] || base_config[:github_access_token]
  raise missing_github_access_token unless github_access_token
  github_client = Octokit::Client.new(access_token: github_access_token)
  validate_github_access!(github_client)

  configuration = new(github_client, base_config)
  configuration.project_base_path = options.project_base_path if options.project_base_path
  configuration.linter_report_file = options.linter_report_file_path if options.linter_report_file_path

  if options.dry_run
    configuration.commenter_class = TTYPullRequestCommenter
    configuration.approver_class = TTYApprover
  end

  configuration
end
default_configuration() click to toggle source
# File lib/linterbot/runner_configuration.rb, line 59
def default_configuration
  {
    project_base_path: File.expand_path(DEFAULT_PROJECT_BASE_PATH),
    linter_report_file: STDIN,
    commenter_class: GitHubPullRequestCommenter,
    approver_class: CommitApprover
  }
end
full_repo_access?(github_client) click to toggle source
# File lib/linterbot/runner_configuration.rb, line 94
def full_repo_access?(github_client)
  github_client.scopes.include?("repo")
end
load_config_file(config_file_path) click to toggle source
# File lib/linterbot/runner_configuration.rb, line 44
def load_config_file(config_file_path)
  return {} unless File.exist?(config_file_path)
  file_content = File.read(config_file_path)
  config = YAML.load(file_content)
  # YAML.load retunrs false if file could not be parsed
  # for example in the case of an empty file.
  if config
    Hash[config.each.map { |key, value| [key.to_sym, value] }]
  else
    STDERR.puts "WARNING: Linterbot configuration file '#{config_file_path}' " \
                "has been ignored because is not a valid YAML file."
    return {}
  end
end
missing_github_access_token() click to toggle source
# File lib/linterbot/runner_configuration.rb, line 38
def missing_github_access_token
  fix_description = "You must either define the enviromental variable 'GITHUB_ACCESS_TOKEN " +
    "or the attribute 'github_access_token' in the configuration file.'"
  MissingAttribute.new("GitHub access token", fix_description)
end
new(github_client, options) click to toggle source
# File lib/linterbot/runner_configuration.rb, line 100
def initialize(github_client, options)
  @github_client = github_client
  @options = options
  @commenter_class = options[:commenter_class]
  @approver_class = options[:approver_class]
  @project_base_path = options[:project_base_path]
  @linter_report_file = options[:linter_report_file]
end
validate_github_access!(github_client) click to toggle source
# File lib/linterbot/runner_configuration.rb, line 90
def validate_github_access!(github_client)
  raise InvalidGitHubCredentials.new unless full_repo_access?(github_client)
end