Sha256: 357383dfa05e1a0845b211f38504edb8ccacd092f7593407461ac57ecd3956b9

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require_relative 'row'
require_relative 'pulls'
require_relative 'config_reader'
require_relative 'members'

module WhatsupGithub
  # Creates Row objects for the future table
  class RowCollector
    attr_reader :repos, :since

    def initialize(args = {})
      @repos = config.repos
      @since = args[:since]
    end

    def collect_rows
      rows = []
      repos.each do |repo|
        rows << collect_rows_for_a(repo)
      end
      rows.flatten
    end

    def collect_rows_for_a(repo)
      pulls(repo).map do |pull|
        Row.new(
          repo: repo,
          repo_url: pull.base.repo.html_url,
          private: pull.base.repo.private?,
          pr_number: pull.number,
          pr_title: pull.title,
          pr_body: pull.body,
          date: pull.merged_at,
          pr_labels: label_names(pull.labels),
          assignee: assignee(pull.assignees),
          membership: member?(pull.user.login),
          merger: pull.merged_by.login,
          merge_commit_sha: pull.merge_commit_sha,
          author: pull.user.login,
          author_url: pull.user.html_url,
          pr_url: pull.html_url
        )
      end
    end

    def sort_by_date
      collect_rows.sort_by do |c|
        Date.parse(c.date)
      end.reverse
    end

    def reverse(collection)
      collection.reverse
    end

    private

    def assignee(assignees)
      if assignees.empty?
        'NOBODY'
      else
        assignees.map(&:login).join(', ')
      end
    end

    def member?(login)
      return nil unless config.membership

      member_logins.include? login
    end

    def label_names(labels)
      labels.map(&:name)
    end

    def pulls(repo)
      Pulls.new(repo: repo, since: since).data
    end

    def load_members
      return if @members

      @members = Members.new(config.membership).members
    end

    def member_logins
      load_members
      @members.map(&:login)
    end

    def config
      Config.instance
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whatsup_github-0.4.1 lib/whatsup_github/row_collector.rb