Class: GHI::Commands::List

Inherits:
Command
  • Object
show all
Defined in:
lib/ghi/commands/list.rb

Constant Summary

Constants included from Formatting

Formatting::CURSOR, Formatting::THROBBERS

Constants included from Formatting::Colors

Formatting::Colors::ANSI, Formatting::Colors::WEB

Instance Attribute Summary collapse

Attributes inherited from Command

#action, #args, #verbose

Attributes included from Formatting

#paging

Instance Method Summary collapse

Methods inherited from Command

#api, #assigns, execute, #initialize, #repo, #repo=

Methods included from Formatting

#columns, #dimensions, #format_comment, #format_comment_editor, #format_comments_and_events, #format_date, #format_editor, #format_event, #format_event_type, #format_issue, #format_issues, #format_issues_header, #format_labels, #format_markdown, #format_milestone, #format_milestone_editor, #format_milestones, #format_number, #format_state, #format_tag, #format_username, #indent, #page, #paginate?, #paging?, #past_due?, #percent, #puts, #throb, #truncate

Methods included from Formatting::Colors

#bg, #blink, #bright, #colorize?, colorize?, #fg, #highlight, #inverse, #no_color, #to_hex, #underline

Constructor Details

This class inherits a constructor from GHI::Commands::Command

Instance Attribute Details

#exclude_pull_requestsObject

Returns the value of attribute exclude_pull_requests.



9
10
11
# File 'lib/ghi/commands/list.rb', line 9

def exclude_pull_requests
  @exclude_pull_requests
end

#pull_requests_onlyObject

Returns the value of attribute pull_requests_only.



10
11
12
# File 'lib/ghi/commands/list.rb', line 10

def pull_requests_only
  @pull_requests_only
end

#quietObject

Returns the value of attribute quiet.



8
9
10
# File 'lib/ghi/commands/list.rb', line 8

def quiet
  @quiet
end

#reverseObject

Returns the value of attribute reverse.



7
8
9
# File 'lib/ghi/commands/list.rb', line 7

def reverse
  @reverse
end

#webObject

Returns the value of attribute web.



6
7
8
# File 'lib/ghi/commands/list.rb', line 6

def web
  @web
end

Instance Method Details

#executeObject



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
# File 'lib/ghi/commands/list.rb', line 113

def execute
  if index = args.index { |arg| /^@/ === arg }
    assigns[:assignee] = args.delete_at(index)[1..-1]
  end

  begin
    options.parse! args
    @repo ||= ARGV[0] if ARGV.one?
  rescue OptionParser::InvalidOption => e
    fallback.parse! e.args
    retry
  end
  assigns[:labels] = assigns[:labels].join ',' if assigns[:labels]
  if assigns[:exclude_labels]
    assigns[:exclude_labels] = assigns[:exclude_labels].join ','
  end
  if reverse
    assigns[:sort] ||= 'created'
    assigns[:direction] = 'asc'
  end
  if web
    Web.new(repo || 'dashboard').open 'issues', assigns
  else
    assigns[:per_page] = 100
    unless quiet
      print header = format_issues_header
      print "\n" unless paginate?
    end
    res = throb(
      0, format_state(assigns[:state], quiet ? CURSOR[:up][1] : '#')
    ) { api.get uri, assigns }
    print "\r#{CURSOR[:up][1]}" if header && paginate?
    page header do
      issues = res.body

      if exclude_pull_requests || pull_requests_only
        prs, issues = issues.partition { |i| i['pull_request'].values.any? }
        issues = prs if pull_requests_only
      end
      if assigns[:exclude_labels]
        issues = issues.reject  do |i|
          i["labels"].any? do |label|
            assigns[:exclude_labels].include? label["name"]
          end
        end
      end
      if verbose
        puts issues.map { |i| format_issue i }
      else
        puts format_issues(issues, repo.nil?)
      end
      break unless res.next_page
      res = throb { api.get res.next_page }
    end
  end
rescue Client::Error => e
  if e.response.code == '422'
    e.errors.any? { |err|
      err['code'] == 'missing' && err['field'] == 'milestone'
    } and abort 'No such milestone.'
  end

  raise
end

#optionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ghi/commands/list.rb', line 12

def options
  OptionParser.new do |opts|
    opts.banner = 'usage: ghi list [options]'
    opts.separator ''
    opts.on '-g', '--global', 'all of your issues on GitHub' do
      assigns[:filter] = 'all'
      @repo = nil
    end
    opts.on(
      '-s', '--state <in>', %w(open closed),
      {'o'=>'open', 'c'=>'closed'}, "'open' or 'closed'"
    ) do |state|
      assigns[:state] = state
    end
    opts.on(
      '-L', '--label <labelname>...', Array, 'by label(s)'
    ) do |labels|
      (assigns[:labels] ||= []).concat labels
    end
    opts.on(
      '-N', '--not-label <labelname>...', Array, 'exclude with label(s)'
    ) do |labels|
      (assigns[:exclude_labels] ||= []).concat labels
    end
    opts.on(
      '-S', '--sort <by>', %w(created updated comments),
      {'c'=>'created','u'=>'updated','m'=>'comments'},
      "'created', 'updated', or 'comments'"
    ) do |sort|
      assigns[:sort] = sort
    end
    opts.on '--reverse', 'reverse (ascending) sort order' do
      self.reverse = !reverse
    end
    opts.on('-p', '--pulls','list only pull requests') { self.pull_requests_only = true }
    opts.on('-P', '--no-pulls','exclude pull requests') { self.exclude_pull_requests = true }
    opts.on(
      '--since <date>', 'issues more recent than',
      "e.g., '2011-04-30'"
    ) do |date|
      begin
        assigns[:since] = DateTime.parse date # TODO: Better parsing.
      rescue ArgumentError => e
        raise OptionParser::InvalidArgument, e.message
      end
    end
    opts.on('-v', '--verbose') { self.verbose = true }
    opts.on('-w', '--web') { self.web = true }
    opts.separator ''
    opts.separator 'Global options'
    opts.on(
      '-f', '--filter <by>',
      filters = %w[all assigned created mentioned subscribed],
      Hash[filters.map { |f| [f[0, 1], f] }],
      filters.map { |f| "'#{f}'" }.join(', ')
    ) do |filter|
      assigns[:filter] = filter
    end
    opts.on '--mine', 'assigned to you' do
      assigns[:filter] = 'assigned'
      assigns[:assignee] = Authorization.username
    end
    opts.separator ''
    opts.separator 'Project options'
    opts.on(
      '-M', '--[no-]milestone [<n>]', Integer,
      'with (specified) milestone'
    ) do |milestone|
      assigns[:milestone] = any_or_none_or milestone
    end
    opts.on(
      '-u', '--[no-]assignee [<user>]', 'assigned to specified user'
    ) do |assignee|
      assignee = assignee.sub /^@/, '' if assignee
      assigns[:assignee] = any_or_none_or assignee
    end
    opts.on '--mine', 'assigned to you' do
      assigns[:filter] = 'assigned'
      assigns[:assignee] = Authorization.username
    end
    opts.on(
      '--creator [<user>]', 'created by you or specified user'
    ) do |creator|
      creator = creator.sub /^@/, '' if creator
      assigns[:creator] = creator || Authorization.username
    end
    opts.on(
      '-U', '--mentioned [<user>]', 'mentioning you or specified user'
    ) do |mentioned|
      assigns[:mentioned] = mentioned || Authorization.username
    end
    opts.on(
      '-O', '--org <organization>', 'in repos within an organization you belong to'
    ) do |org|
	    assigns[:org] = org
      @repo = nil
    end
    opts.separator ''
  end
end