Sha256: ee43e69048b10532c1226518b610ace8377ad5938dc8284d10edafddf98baa79

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Danger
  class DangerChikuwa < Plugin
    attr_accessor :project_root, :inline_mode

    def _project_root
      root = @project_root || Dir.pwd
      root += "/" unless root.end_with? "/"
      root
    end

    def _inline_mode
      @inline_mode || false
    end

    def report(file_path)
      if File.exist?(file_path)
        results = parse_build_log(file_path)
        send_reports(results)
      else
        fail "build log file not found"
      end
    end

    private

    module Type
      WARN = "w"
      ERROR = "e"
    end

    class ReportData
      attr_accessor :message, :type, :file, :line

      def initialize(message, type, file, line)
        self.message = message
        self.type = type
        self.file = file
        self.line = line
      end
    end

    def parse_build_log(file_path)
      report_data = []
      File.foreach(file_path) do |line|
        logs = line.split(":")
        if logs.length < 4
          next
        end

        case logs[0]
        when "w"
          type = Type::WARN
        when "e"
          type = Type::ERROR
        else
          next
        end

        file = Pathname(logs[1].strip).relative_path_from(_project_root).to_s
        line_num = /(\d+)/.match(logs[2].strip).to_a[0].to_i
        logs.shift(3)
        message = logs.join(":").strip!

        report_data.push(ReportData.new(message, type, file, line_num))
      end

      return report_data
    end

    def send_reports(results)
      results.each do |data|
        send(data)
      end
    end

    def send(data)
      case data.type
      when Type::WARN
        if _inline_mode
          warn(data.message, file: data.file, line: data.line)
        else
          warn(data.message)
        end
      when Type::ERROR
        if _inline_mode
          failure(data.message, file: data.file, line: data.line)
        else
          failure(data.message)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danger-chikuwa-0.0.1 lib/chikuwa/plugin.rb