Class: Bl::CLI

Inherits:
Thor
  • Object
show all
Includes:
Formatting, Requestable
Defined in:
lib/bl/cli.rb

Constant Summary collapse

ISSUES_PARAMS =
{
  projectId: :array,
  issueTypeId: :array,
  categoryId: :array,
  versionId: :array,
  milestoneId: :array,
  statusId: :array,
  priorityId: :array,
  assigneeId: :array,
  createdUserId: :array,
  resolutionId: :array,
  parentChild: :numeric,
  attachment: :boolean,
  sharedFile: :boolean,
  sort: :string,
  order: :string,
  offset: :numeric,
  count: :numeric,
  createdSince: :string,
  createUntil: :string,
  updatedSince: :string,
  updatedUntil: :string,
  startDateSince: :string,
  startDateUntil: :string,
  dueDateSince: :string,
  dueDateUntil: :string,
  id: :array,
  parentIssueId: :array,
  keyword: :string
}
ISSUE_BASE_ATTRIBUTES =
{
  summary: :string,
  description: :string,
  statusId: :numeric,
  resolutionId: :numeric,
  dueDate: :string,
  issueTypeId: :numeric,
  categoryId: :array,
  versionId: :array,
  milestoneId: :array,
  priorityId: :numeric,
  assigneeId: :numeric
}
ACTIVITY_TYPES =
{
  1 => 'Issue Created',
  2 => 'Issue Updated',
  3 => 'Issue Commented',
  4 => 'Issue Deleted',
  5 => 'Wiki Created',
  6 => 'Wiki Updated',
  7 => 'Wiki Deleted',
  8 => 'File Added',
  9 => 'File Updated',
  10 => 'File Deleted',
  11 => 'SVN Committed',
  12 => 'Git Pushed',
  13 => 'Git Repository Created',
  14 => 'Issue Multi Updated',
  15 => 'Project User Added',
  16 => 'Project User Deleted',
  17 => 'Comment Notification Added',
  18 => 'Pull Request Added',
  19 => 'Pull Request Updated',
  20 => 'Comment Added on Pull Request'
}

Instance Method Summary collapse

Methods included from Formatting

colorize_priority, colorize_status, colorize_type, print_issue

Methods included from Requestable

client

Constructor Details

#initializeCLI

Returns a new instance of CLI.



75
76
77
78
# File 'lib/bl/cli.rb', line 75

def initialize(*)
  @config = Bl::Config.instance
  super
end

Instance Method Details

#activitiesObject



237
238
239
240
241
242
243
244
245
246
# File 'lib/bl/cli.rb', line 237

def activities
  client.get('/space/activities').body.each do |a|
    puts [
      ACTIVITY_TYPES[a.type],
      a.content.inspect,
      a.createdUser.name,
      a.created
    ].join("\t")
  end
end

#add(*subjects) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/bl/cli.rb', line 179

def add(*subjects)
  subjects.each do |s|
    issue_default_options = @config[:issue][:default]
    res = client.post(
      'issues',
      issue_default_options.merge({summary: s}).merge(options)
    )
    puts "issue added: #{res.body.issueKey}\t#{res.body.summary}"
  end
end

#browse(key) ⇒ Object



172
173
174
175
# File 'lib/bl/cli.rb', line 172

def browse(key)
  url = 'https://' + @config[:space_id] + '.backlog.jp/view/' + key
  system("open #{url}")
end

#close(*keys) ⇒ Object



201
202
203
204
205
206
# File 'lib/bl/cli.rb', line 201

def close(*keys)
  keys.each do |k|
    res = client.patch("issues/#{k}", statusId: 4)
    puts "issue closed: #{res.body.issueKey}\t#{res.body.summary}"
  end
end

#configObject



86
87
88
# File 'lib/bl/cli.rb', line 86

def config
  p @config
end

#countObject



111
112
113
# File 'lib/bl/cli.rb', line 111

def count
  puts client.get('issues/count', options.to_h).body.count
end

#initObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bl/cli.rb', line 91

def init
  filename = File.join(Dir.home, Bl::CONFIG_FILE)
  if File.exist?(filename)
    puts "#{filename} exits."
  else
    config = Bl::Config.instance.default_config
    f = File.new(filename, 'w')
    f.write(config.to_yaml)
    puts "#{filename} generated."
  end
end

#listObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bl/cli.rb', line 122

def list
  opts = {}
  opts[:statusId] = [1, 2, 3] unless options[:all]
  opts[:assigneeId] = [-1] if options[:unassigned]
  if options[:today]
    today = Date.today
    opts[:dueDateSince] = today.to_s
    opts[:dueDateUntil] = today.next.to_s
  end
  opts[:dueDateUntil] = Date.today.to_s if options[:overdue]
  if options[:priority]
    opts[:sort] = "priority"
    opts[:order] = "asc"
  end
  opts[:categoryId] = [-1] if options[:nocategory]
  client.get('issues', opts).body.each do |i|
    print_issue(i)
  end
end

#notificationsObject



249
250
251
252
253
# File 'lib/bl/cli.rb', line 249

def notifications
  client.get('notifications').body.each do |n|
    puts n.pretty_inspect
  end
end

#prioritiesObject



216
217
218
219
220
# File 'lib/bl/cli.rb', line 216

def priorities
  client.get('priorities').body.each do |p|
    puts [p.id, colorize_priority(p.id, p.name)].join("\t")
  end
end

#resolutionsObject



223
224
225
226
227
# File 'lib/bl/cli.rb', line 223

def resolutions
  client.get('resolutions').body.each do |r|
    puts [r.id, r.name].join("\t")
  end
end

#searchObject



144
145
146
147
148
# File 'lib/bl/cli.rb', line 144

def search
  client.get('issues', options.to_h).body.each do |i|
    print_issue(i)
  end
end

#show(key) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/bl/cli.rb', line 151

def show(key)
  body = client.get("issues/#{key}").body
  puts "type: #{body.issueType.name}"
  puts "key: #{body.issueKey}"
  puts "created: #{body.created}"
  puts "due date: #{body.dueDate}"
  puts "summary: #{body.summary}"
  puts "priority: #{body.priority.name}"
  puts "category: #{body.category}"
  puts "resolution: #{body.resolution}"
  puts "version: #{body.versions}"
  puts "status: #{body.status.name}"
  puts "milestone: #{body.milestone}"
  puts "assignee: #{body.assignee.name}"
  puts "created user: #{body.createdUser.name}"
  puts '--'
  puts "description:"
  puts body.description
end

#spaceObject



104
105
106
107
# File 'lib/bl/cli.rb', line 104

def space
  res = client.get('space').body
  puts res.inspect
end

#statusesObject



209
210
211
212
213
# File 'lib/bl/cli.rb', line 209

def statuses
  client.get('statuses').body.each do |s|
    puts [s.id, colorize_status(s.id, s.name)].join("\t")
  end
end

#update(*keys) ⇒ Object



193
194
195
196
197
198
# File 'lib/bl/cli.rb', line 193

def update(*keys)
  keys.each do |k|
    res = client.patch("issues/#{k}", options.to_h)
    puts "issue updated: #{res.body.issueKey}\t#{res.body.summary}"
  end
end

#usersObject



230
231
232
233
234
# File 'lib/bl/cli.rb', line 230

def users
  client.get('users').body.each do |u|
    puts [u.id, u.userId, u.name, u.roleType, u.lang, u.mailAddress].join("\t")
  end
end

#versionObject



81
82
83
# File 'lib/bl/cli.rb', line 81

def version
  puts Bl::VERSION
end