class TelegramBot::TrelloConnector

Attributes

board_name[R]
github_repository[R]
github_username[R]
statistics[R]

Public Class Methods

new(username:, repository:) click to toggle source
# File lib/telegram-bot/trello.rb, line 23
def initialize(username:, repository:)
  @statistics        = { boards_created: 0, lists_created: 0, cards_created: 0 }
  @github_username   = username
  @github_repository = repository
  @board_name        = @github_repository.split(/[-_]/i).map(&:capitalize).join(' ')

  @board = find_or_create_board_by_name
  create_scrum_lists(@board)
  close_default_lists(@board)
  populate_issues_cards
end

Public Instance Methods

show_statistics() click to toggle source
# File lib/telegram-bot/trello.rb, line 35
def show_statistics
  "Estadisticas del proceso:\n" +
  "-" * 25 + "\n" +
  "Tableros Creados: #{@statistics[:boards_created]}\n" +
  "Listas Creadas:   #{@statistics[:lists_created]}\n" +
  "Tarjetas Creadas: #{@statistics[:cards_created]}\n"
end

Private Instance Methods

archive_existing_cards() click to toggle source
# File lib/telegram-bot/trello.rb, line 80
def archive_existing_cards
  find_backlog_list.archive_all_cards
end
close_default_lists(board) click to toggle source
# File lib/telegram-bot/trello.rb, line 71
def close_default_lists(board)
  board.lists.each do |list|
    unless LIST_NAMES.include?(list.name)
      list.update_fields(closed: true)
      list.save
    end
  end
end
create_scrum_lists(board) click to toggle source
# File lib/telegram-bot/trello.rb, line 60
def create_scrum_lists(board)
  LIST_NAMES.reverse.each do |name, index|
    list = board.lists.detect { |list| list.name =~ /#{name}/i }
    unless list 
      Trello::List.create(name: name, board_id: board.id, pos: index)
      @statistics[:lists_created] += 1
    end
  end
  self
end
find_backlog_list() click to toggle source
# File lib/telegram-bot/trello.rb, line 56
def find_backlog_list
  @board.lists.detect { |list| list.name =~ /backlog/i }
end
find_or_create_board_by_name() click to toggle source
# File lib/telegram-bot/trello.rb, line 44
def find_or_create_board_by_name
  board = Trello::Board.all.detect do |board|
    board.name =~ /#{@board_name}/
  end

  unless board
    board = Trello::Board.create(name: @board_name, description: BOARD_DESC)
    @statistics[:boards_created] += 1
  end
  board
end
populate_issues_cards() click to toggle source
# File lib/telegram-bot/trello.rb, line 84
def populate_issues_cards
  backlog_list = find_backlog_list
  if backlog_list
    begin
      github = GitHubWrapper::Repository.find(username:   @github_username, 
                                              repository: @github_repository)
      github.get_issues(state: 'open').each do |issue|
        if issue.instance_of?(GitHubWrapper::Issue)
          card = backlog_list.cards.detect { |c| c.name =~ /^##{issue.number}\s[-]/i }

          unless card
            Trello::Card.create(name: "##{issue.number} - #{issue.title}", desc: issue.body, list_id: backlog_list.id)
            @statistics[:cards_created] += 1
          end
        end
      end
    rescue TypeError => e
      puts "User/Repository not found: #{e}"
    rescue StandardError => e
      puts "Error: #{e}"
    end
  end
  self
end