Class: GhIssues::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/gh_issues/cli.rb

Instance Method Summary collapse

Instance Method Details

#allObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gh_issues/cli.rb', line 42

def all
  ENV['GH_ISSUES_COLORIZE'] = '1' if options[:color]
  sort_by = ['name', 'count'].include?(options[:sort_by]) ? options[:sort_by] : 'name'
  sort_order = ['asc', 'desc'].include?(options[:sort_order]) ? options[:sort_order] : 'asc'
  all_issues = GhIssues.all_issues
  if all_issues.count > 0
    puts print_issue_list(all_issues, sort_by: sort_by, sort_order: sort_order)
  else
    puts "Hooray! You have no issue to display..."
  end
end

#list(owner) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gh_issues/cli.rb', line 64

def list(owner)
  ENV['GH_ISSUES_COLORIZE'] = '1' if options[:color]
  sort_by = ['name', 'count'].include?(options[:sort_by]) ? options[:sort_by] : 'name'
  sort_order = ['asc', 'desc'].include?(options[:sort_order]) ? options[:sort_order] : 'asc'
  begin
    owner_issues = GhIssues.list_owner_issues(owner)
  rescue GhIssues::InvalidOwnerError => error_message
    puts error_message
    exit
  end
  if owner_issues.count > 0
    only_owners_data = {}
    only_owners_data[owner] = owner_issues
    puts print_issue_list(only_owners_data, sort_by: sort_by, sort_order: sort_order)
  else
    puts "Hooray! You have no issue to display..."
  end
end

#show(repo = nil, issue_number = 0) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/gh_issues/cli.rb', line 95

def show(repo=nil, issue_number=0)
  ENV['GH_ISSUES_COLORIZE'] = '1' if options[:color]
  
  origin_url=`git remote get-url origin 2>/dev/null`.strip
  current_repo = nil
  if GhIssues.in_github_repo?(origin_url)
    current_repo = GhIssues.get_repo_name(origin_url)
  end

  unless repo
    repo = current_repo if current_repo
  else
    if is_numeric?(repo)
      issue_number = repo.to_i
      repo = current_repo
    end
  end
  
  issue_number = issue_number.to_i

  puts "Listing current GitHub repo: #{repo}" if repo == current_repo

  if issue_number > 0
    markdown = Redcarpet::Markdown.new(GhIssues::TextRenderer)
    issue = GhIssues.get_issue(repo, issue_number)
    table = Terminal::Table.new do |t|
      t.add_row [colorize("Repo/Issue", :yellow), colorize("#{repo}/#{issue_number}", :white)]
      t.add_separator
      t.add_row [colorize("Title", :yellow), issue[:title]]
      t.add_separator
      t.add_row [colorize("Opener", :yellow), issue[:user]]
      t.add_row [colorize("Labes", :yellow), issue[:labels].join(',')] if issue[:labels].count > 0
      t.add_row [colorize("Assignees", :yellow), issue[:assignees].join(', ')] if issue[:assignees].count > 0
      t.add_separator

      created_at = issue[:created_at].strftime(DEFAULT_DATE_FORMAT)
      updated_at = issue[:updated_at].strftime(DEFAULT_DATE_FORMAT)

      now = Time.now
      created_at_diff = TimeDifference.between(now, issue[:created_at]).in_days.to_i
      updated_at_diff = TimeDifference.between(now, issue[:updated_at]).in_days.to_i

      t.add_row [colorize("Created at", :yellow), "#{created_at} (#{created_at_diff} #{pluralize("day", "days", created_at_diff)} ago)"]
      t.add_row [colorize("Updated at", :yellow), "#{updated_at} (#{updated_at_diff} #{pluralize("day", "days", updated_at_diff)} ago)"]
      if issue[:body].length > 0
        body_text = markdown.render(issue[:body])
        t.add_separator
        t.add_row [colorize("Body", :yellow), wrap(body_text, WRAP_TEXT_AT)]
      end
      
      if issue[:comments].length > 0
        t.add_separator
        t.add_row [{value: "#{colorize(pluralize('Comment', 'Comments', issue[:comments].length), :yellow)} (#{issue[:comments].length})", colspan: 2, alignment: :center}]
        issue[:comments].each do |comment|
          t.add_separator
          comment_created_at = comment[:created_at].strftime(DEFAULT_DATE_FORMAT)
          comment_created_at_diff = TimeDifference.between(now, comment[:created_at]).in_days.to_i
          comment_text = "#{markdown.render(comment[:body])}\n---\n#{comment_created_at} (#{comment_created_at_diff} #{pluralize("day", "days", comment_created_at_diff)} ago)"
          t.add_row [colorize(comment[:user], :green), wrap(comment_text, WRAP_TEXT_AT)]
        end
      end
    end
    puts table
  else
    issues = GhIssues.show_repos_issues(repo)
    if issues.count > 0
      table = Terminal::Table.new do |t|
        t.add_row ["", colorize(repo, :yellow), colorize("Url", :yellow)]
        t.add_separator
        issues.each do |issue|
          t.add_row [
            {value: colorize("##{issue[:number]}", :white), alignment: :left},
            issue[:title],
            "#{issue[:html_url]}",
          ]
        end
      end
      puts table
    else
      puts "Hooray! You have no issue here..."
    end
  end
end

#versionObject



28
29
30
# File 'lib/gh_issues/cli.rb', line 28

def version
  puts GhIssues::VERSION
end