Class: Blend::CLI::Juice

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

Instance Method Summary collapse

Instance Method Details

#activity(name) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/blend/cli/juice.rb', line 376

def activity( name )
  puts
  project_id = project_id_from_name name
  return if project_id.nil?

  now = DateTime.now.to_date + 1
  after = now - now.wday
  before = Time.now

  if( options[:lastweek] )
    after -= 7
    before = after + 7
  end

  summary = client.activities( project_id, after.to_time, before.to_time )

  project = client.project( project_id )

  puts "#{project['name']} activity".bold + " for #{summary[:after].strftime( "%Y-%m-%d %H:%M" )} (#{((Time.now-summary[:after])/3600/24).round(1)} days ago) - #{summary[:before].strftime( "%Y-%m-%d %H:%M" )} (now)"
  puts
  puts "Activity Summary".underline.blue
  summary[:type].keys.sort.each do |x|
    printf "%-6s %s\n", summary[:type][x].count, x
  end

  puts
  puts "Activity Breakdown".underline.blue
  summary[:actors_activites].keys.sort.each do |x|
    summary[:actors_activites][x].keys.select { |x| x}.sort.each do |type|
      printf "%-6s %-25s %s\n", summary[:actors_activites][x][type].count, type.strip_email, x
    end
  end

  puts
  puts "New Tickets".underline.blue
  (summary[:type]['bugtracking:openticket'] || []).each do |activity|
    printf "%-15s %-100s\n", activity['actor_identifier'], activity['description'][0..100].gsub( /\n/, " " )
    #puts activity['description'][0..100].gsub( /\n/, " " )
  end

  puts
  puts "Closed Tickets".underline.blue
  (summary[:type]['bugtracking:closedticket'] || []).each do |activity|
    printf "%-15s %-100s\n", activity['actor_identifier'], activity['description'][0..100].gsub( /\n/, " " )
    #puts activity['description'][0..100].gsub( /\n/, " " )
  end

  puts
  puts "Active Tickets".underline.blue
  summary[:type].keys.select do |x|
    x =~ /bugtracking/
  end.collect do |x|
    summary[:type][x].collect do |a|
      a['description']
    end
  end.flatten.sort.uniq.each do |x|
    puts x[0..100].gsub( /\n/, " " ) unless x =~ /^\[Changeset\]/
  end

  puts
  puts "Commits".underline.blue
  (summary[:type]['sourcecontrol:commit'] || []).each do |x|
    printf "%-30s %s\n", x['actor_identifier'].strip_email, x['description'][0..100].gsub( /\n/, " " )
  end
end

#add_hipchat(name, room) ⇒ Object



110
111
112
113
# File 'lib/blend/cli/juice.rb', line 110

def add_hipchat( name, room )
  client.project_add_hipchat( project_id_from_name( name ), room )
  info( name )
end

#add_team(name, team) ⇒ Object



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

def add_team( name, team )
  client.project_add_team( project_id_from_name( name ), team )
  info( name )
end

#all_projectsObject



148
149
150
151
152
# File 'lib/blend/cli/juice.rb', line 148

def all_projects
  client.projects.each do |p|
    project( p['name'] )
  end
end

#create(name) ⇒ Object



37
38
39
40
# File 'lib/blend/cli/juice.rb', line 37

def create( name )
  puts "TIP: you can also run check #{name} to set up everything"
  client.create_project( name )
end

#feeds(name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/blend/cli/juice.rb', line 72

def feeds(name)
  puts
  printf "%-30s %-20s %-30s\n".blue.underline, 'Feed', 'Environment', 'Namespace'
  client.feeds( project_id_from_name( name ) ).sort do
    |a,b| a['feed_name'] <=> b['feed_name']
  end.each do |feed|
    printf "%-30s %-20s %-30s\n", 
      feed['feed_name'], 
      (feed['environment'] || {})['name'],
      [feed['namespace'],feed['name']].select {|x| x}.join( '/' )
  end
  puts
end

#github_team_checkObject



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/blend/cli/juice.rb', line 334

def github_team_check
  client.github_team_check.each do |team,projects|
    next if team == 'Owners'
    next if projects.size > 0
    puts "#{team} has no juice project".red
    # printf "%-30s %s\n", team, projects.collect {|x| x['name'] }.join( "," )

    if( options[:fix] )
      choices = client.projects.collect { |x| x['name'] }

      project = choose do |menu|
        menu.header = "Select a github team action"
        menu.prompt = "Please choose a unassigned juice project to associate #{team} with:"

        menu.choice "Ignore" do
          "ignore"
        end

        menu.choice "Create a project called #{team}" do
          "create"
        end

        menu.choices *choices
      end

      if( project == "ignore" )
        puts "Skip it"
      else
        if( project == "create" )
          puts "TODO: Create a new project called #{team}"
        else
          add_team project, team
          # puts "Attaching to: #{project}"
        end
      end
    end
  end
end

#heroku_api(token) ⇒ Object



136
137
138
# File 'lib/blend/cli/juice.rb', line 136

def heroku_api( token )
  client.heroku_api token
end

#hipchat_api(token) ⇒ Object



142
143
144
# File 'lib/blend/cli/juice.rb', line 142

def hipchat_api( token )
  client.hipchat_api token
end

#hipchat_checkObject



322
323
324
325
326
327
328
329
330
# File 'lib/blend/cli/juice.rb', line 322

def hipchat_check
  begin
    client.hipchat_check.each do |x|
      printf "%-30s %s\n", x[:room], x[:projects].join( "," )
    end
  rescue Exceptions::HipchatAuthenticationFailure
    puts "Unable to connect to Hipchat. Is your key valid?".red
  end
end

#loginObject



5
6
7
# File 'lib/blend/cli/juice.rb', line 5

def 
  puts "Logged in." if client.
end

#logoutObject



10
11
12
# File 'lib/blend/cli/juice.rb', line 10

def logout
  puts "Juice credentials cleared." if client.logout
end

#lookup_user(query) ⇒ Object



116
117
118
# File 'lib/blend/cli/juice.rb', line 116

def lookup_user( query )
  pp client.lookup_user( query )
end

#open(name) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/blend/cli/juice.rb', line 15

def open( name )
  project_id = project_id_from_name name
  if( project_id.nil? )
    puts "#{name} not found"
    return
  end

  system "open http://happyfunjuice.com/projects/#{project_id}"
end

#organizationsObject



61
62
63
64
65
66
67
68
# File 'lib/blend/cli/juice.rb', line 61

def organizations
  puts
  printf "%-5s %-25s %-25s\n".underline.blue, 'ID', 'Name', 'Domain name'
  client.organizations.each do |o|
    printf "%-5s %-25s %-25s\n", o['id'], o['name'], o['domain_name']
  end
  puts
end

#project(name) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/blend/cli/juice.rb', line 156

def project( name )
  status = Blend::Status::Project.new( name, options[:resolve] )

  status.header "#{name}: Juice Configuration"
  status.check "Project Exists", :project_found
  status.check "Hipchat Room", :hipchat
  status.check "Github Teams", :github_teams
  status.check "Repos Configured", :repos_setup
  status.check "Source Control", :source_control
  status.check "Bug Tracking", :bugtracking

  status.header "#{name}: Environments"
  status.check "Production", :production
  status.check "Staging", :staging

  status.header "#{name}: Team Configuration (#{status.github_teams.join(',')})" if status.github_teams.length > 0

  status.check "Members", :github_members

  status.check "User Matchup", :juice_users_synced
  
  status.github_members.each do |m|
    member = m[:name]
    access = m[:access]
    # puts member
    juice_user = client.user_from_github_user member
    if access == :read
      printf "%-20s %15s".yellow, member, "readonly"
    else
      printf "%-20s %15s".green, member, "fullaccess"
    end

    if juice_user
      printf " %-20s %s\n", juice_user['name'], juice_user['email']
    else
      puts " Unknown to juice".red
    end
  end

  status.repo_status.each do |repo|
    status.header "#{repo.name} Configuration"

    repo.check "Private", :private?
    repo.check "Hipchat Deployhook", :hipchat_hook
  end

  status.environment_status.each do |env|
    status.header "Server: #{env.server} Configuration"
    
    env.check "Dyno Redundancy", :dyno_redundancy
    env.check "Database", :database
    env.check "Backups", :backups
    env.check "Stack", :stack
    env.check "Exception Handling", :exception_handling
    env.check "Deploy Hooks", :deployhooks
    env.check "Log Monitoring", :log_monitoring
    env.check "App Monitoring", :app_monitoring
    env.check "SSL Addon", :ssl
  end

  status.domains_status.each do |domain|
    status.header "DNS: #{domain.domain} configuration"

    domain.check "Registered?", :registered?
    domain.check "Expires", :expires
    domain.check "Owner", :owner
    domain.check "SSL Cert", :ssl_exists?
    domain.check "SSL Expires", :ssl_valid_until
    domain.check "SSL Common Name", :ssl_common_name
  end

end

#projectsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/blend/cli/juice.rb', line 43

def projects
  puts
    printf "%-25s %-10s %-10s %-25s\n".blue, '', 'Commits', 'Deploys', '', ''
    printf "%-25s %-10s %-10s %-25s\n".blue, '', 'this', 'this', '', ''
    printf "%-25s %-10s %-10s %-25s %s\n".underline.blue, 'Name', 'wk', 'wk', 'Hipchat room', 'Teams'
  client.summary.sort{|a,b| b['name'].to_i <=> a['name'].to_i}.each_with_index do |project,i|
    printf "%-25s %-10s %-10s %-25s %s\n".try{|x| i%4==3 ? x.underline : x}, 
      #project['id'], 
      project['name'].projectize, 
      (project['has_source_feeds'] ? project['commits_this_week'] : ''),
      (project['has_server_feeds'] ? project['deploys_this_week'] : ''),
      project['blend_config']['hipchat_room'],
      project['blend_config']['teams'].join( ',' )
  end
  puts
end

#report(name) ⇒ Object



444
445
446
447
# File 'lib/blend/cli/juice.rb', line 444

def report(name)
  info name
  activity name
end

#report_dumpObject



451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/blend/cli/juice.rb', line 451

def report_dump
  puts "Loading projects"

  system "mkdir -p /tmp/juice_reports"
  client.projects.each do |project|
    File.open( "/tmp/juice_reports/#{project['name']}.txt", "w" ) do |out|
      puts "Running report for #{project['name']}"
      $stdout = out
      info project['name']
      activities project['name']
      $stdout = STDOUT
    end
  end
end

#search_users(query) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/blend/cli/juice.rb', line 121

def search_users( query )
  puts
  printf "%-25s %35s %35s %35s %35s\n".blue.underline, 'Name', 'Email', 'Personal email', 'Heroku', 'Github'
  client.search_users(query).each do |u|
    printf "%-25s %35s %35s %35s %35s\n", u['name'], u['email'], u['personal_email'], u['heroku_handle'], u['github_handle']
  end
  puts
end

#settings(name) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/blend/cli/juice.rb', line 26

def settings(name)
  project_id = project_id_from_name name
  if( project_id.nil? )
    puts "#{name} not found"
    return
  end

  system "open http://happyfunjuice.com/projects/#{project_id}/overview"
end

#users(name = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/blend/cli/juice.rb', line 87

def users( name=nil )

  if name.nil?
    _users = client.organization_users( 1 ) # Hard-code hfc org here
  else
    _users = client.project_users( project_id_from_name( name ) )
  end

  puts
  printf "%-25s %35s %35s %35s %35s\n".blue.underline, 'Name', 'Email', 'Personal email', 'Heroku', 'Github'
  _users.each do |u|
    printf "%-25s %35s %35s %35s %35s\n", u['name'], u['email'], u['personal_email'], u['heroku_handle'], u['github_handle']
  end
  puts
end