# frozen_string_literal: true require "thor" require "action_view" require_relative "../base" module Neetob class CLI module Github module Issues class List < Base include ActionView::Helpers::DateHelper attr_accessor :repos, :issue_state, :issue_assignee, :issue_search_query, :show_issues_count, :issue_label, :sandbox def initialize(repos, issue_assignee = "", issue_state = "open", issue_search_query = "", show_issues_count = false, issue_label = "", sandbox = false) super() @repos = repos @issue_state = issue_state @issue_assignee = issue_assignee @show_issues_count = show_issues_count @issue_search_query = issue_search_query @issue_label = issue_label @sandbox = sandbox end def run matching_repos = find_all_matching_apps_or_repos(repos, :github, sandbox) matching_repos.each do |repo| ui.info("\n Issues of #{repo} \n") begin issues = client.search_issues(uri_with_query_options(repo)) ui.info("There are #{issues[:total_count]} issues with matching query") if show_issues_count table_rows = create_table(issues[:items]) table = Terminal::Table.new headings: table_columns, rows: table_rows ui.success(table) rescue StandardError => e ExceptionHandler.new(e).process end end end private def table_columns issue_label.nil? ? ["Title", "Created at", "Assignee", "Link"] : ["Title", "Created at", "Assignee", "Link", "Labels"] end def create_table(issues) issues.map do |issue| row_data = [ issue[:title], "#{distance_of_time_in_words(issue[:created_at], Time.now)} ago", issue.dig(:assignee, :login), issue[:html_url] ] issue_label.nil? ? row_data : row_data.push(labels_names(issue[:labels])) end end def uri_with_query_options(repo) uri = "repo:#{repo} is:issue state:#{issue_state}" unless issue_label.nil? check_valid_labels(repo) uri.concat(" label:#{issue_label}") end uri.concat(issue_assignee == "none" ? " no:assignee" : " assignee:#{issue_assignee}") if issue_assignee "#{uri} #{issue_search_query}" end def labels_names(labels) labels.map { |label| label[:name] }.join(", ") end def check_valid_labels(repo) issue_label.split(",").each { |label| valid_label?(repo, label) } end def valid_label?(repo, label) client.label(repo, label) rescue Octokit::NotFound => e ui.error("There is no \"#{label}\" label in the \"#{repo}\" repo.") end end end end end end