Sha256: 14810adb6885ae02db8e4cdbc75ce8e388c421409026b6e5bd5868f955aaad75

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module Herodot
  class ProjectLink
    PROJECT_CONFIG = '.herodot.yml'.freeze

    def self.project_config_file(path)
      File.join(File.expand_path(path), PROJECT_CONFIG)
    end

    def self.link(path, link, pattern)
      puts "Write link into #{project_config_file(path)}"
      File.open(project_config_file(path), 'w') do |f|
        YAML.dump({ link: link, pattern: pattern }, f)
      end
    end

    def initialize(worklog)
      @worklog = worklog
      @project_configurations = {}
    end

    def totals
      @worklog.totals.map do |date, logs|
        [date, decorated_logs(logs)]
      end
    end

    private

    def decorated_logs(logs)
      logs.map do |log|
        decorated_log(log)
      end
    end

    def decorated_log(log)
      link = issue_management_link(log)
      return log if link.nil?
      log.merge(link: link)
    end

    def issue_management_link(log)
      config = @project_configurations.fetch(log[:path], load_project_configuration(log[:path]))
      return nil unless config.fetch(:link, false)
      ticket = log[:branch].scan(Regexp.new(config.fetch(:pattern, /$^/)))
      [config[:link], ticket.first].join if ticket.any?
    end

    def load_project_configuration(path)
      file = self.class.project_config_file(path)
      return { link: false } unless File.exist?(file)
      File.open(file) { |f| YAML.load(f) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
herodot-0.2.1 lib/herodot/project_link.rb