Class: Travis::CLI::Monitor

Inherits:
ApiCommand show all
Defined in:
lib/travis/cli/monitor.rb

Constant Summary

Constants inherited from Command

Command::DAY, Command::HOUR, Command::MINUTE, Command::WEEK

Constants included from Tools::Assets

Tools::Assets::BASE

Instance Attribute Summary collapse

Attributes inherited from ApiCommand

#enterprise_name, #session

Attributes inherited from Command

#arguments, #config, #debug, #force_interactive, #formatter, #input, #output

Instance Method Summary collapse

Methods inherited from ApiCommand

#authenticate, #detected_endpoint?, #endpoint_config, #enterprise?, #org?, #pro?, #sync

Methods included from Travis::Client::Methods

#access_token, #access_token=, #account, #accounts, #api_endpoint, #api_endpoint=, #artifact, #broadcasts, #build, #cancel, #explicit_api_endpoint?, #github_auth, #hooks, #job, #lint, #listen, #logout, #regenerate_token, #remove_token, #repo, #restart, #user

Methods inherited from Command

abstract, abstract?, #check_completion, #check_ruby, #check_version, #command_name, command_name, #debug?, description, #error, #execute, #help, #info, #last_check, #on_signal, #parse, #say, skip, subcommands, #terminal, #time, #usage, #usage_for, #warn, #write_to

Methods included from Tools::Assets

asset, asset_path

Methods included from Parser

#new, #on, #on_initialize

Constructor Details

#initializeMonitor

Returns a new instance of Monitor.



28
29
30
31
# File 'lib/travis/cli/monitor.rb', line 28

def initialize(*)
  @repos = []
  super
end

Instance Attribute Details

#notificationObject (readonly)

Returns the value of attribute notification.



26
27
28
# File 'lib/travis/cli/monitor.rb', line 26

def notification
  @notification
end

#reposObject (readonly)

Returns the value of attribute repos.



26
27
28
# File 'lib/travis/cli/monitor.rb', line 26

def repos
  @repos
end

Instance Method Details

#all?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/travis/cli/monitor.rb', line 74

def all?
  !pull? and !push?
end

#descriptionObject



56
57
58
59
60
61
62
# File 'lib/travis/cli/monitor.rb', line 56

def description
  case repos.size
  when 0 then session.config['host']
  when 1 then repos.first.slug
  else "#{repos.size} repositories"
  end
end

#display(entity, time) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/travis/cli/monitor.rb', line 84

def display(entity, time)
  say [
    color(formatter.time(time), entity.color),
    color(entity.inspect_info, [entity.color, :bold]),
    color(entity.state, entity.color),
    color(entity.commit.subject, entity.color)
  ].join(' ')
  notification.notify(entity.repository.slug, [
    entity.class.name[/[^:]+$/],
    entity.number,
    "#{entity.state}:",
    entity.commit.subject
  ].join(' '))
end

#eventsObject



64
65
66
67
68
# File 'lib/travis/cli/monitor.rb', line 64

def events
  events = %w[build:started build:finished]
  events << 'job:started' << 'job:finished' unless builds?
  events
end

#firehose?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/travis/cli/monitor.rb', line 70

def firehose?
  org? and repos.empty?
end

#handle_event(event) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/travis/cli/monitor.rb', line 99

def handle_event(event)
  entity = event.job          || event.build
  time   = entity.finished_at || entity.started_at
  display(entity, time) if monitor? entity
rescue Travis::Client::Error => e
  raise e if explode?
end

#monitor?(entity) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/travis/cli/monitor.rb', line 78

def monitor?(entity)
  return true if all?

  entity.pull_request? ? pull? : push?
end

#runObject



107
108
109
110
111
112
# File 'lib/travis/cli/monitor.rb', line 107

def run
  listen(*repos) do |listener|
    listener.on_connect { say description, "Monitoring #{'builds for ' if builds?}%s:" }
    listener.on(*events) { |e| handle_event(e) }
  end
end

#setupObject



33
34
35
36
37
38
39
# File 'lib/travis/cli/monitor.rb', line 33

def setup
  super
  repos.map! { |r| repo(r) }
  repos.concat(user.repositories) if my_repos?
  setup_notification(!firehose? || :dummy) unless notification
  debug "Using notifications: #{notification.class.name[/[^:]+$/]}"
end

#setup_notification(type = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/travis/cli/monitor.rb', line 41

def setup_notification(type = nil)
  refuse = false
  case type
  when false     then @notification = Tools::Notification.new(:dummy)
  when nil, true then @notification = Tools::Notification.new
  else
    refuse        = true
    @notification = Tools::Notification.new(type)
  end
rescue ArgumentError => e
  @notification = Tools::Notification.new(:dummy)
  error(e.message) if refuse
  warn(e.message)
end