Class: Githubscore

Inherits:
Object
  • Object
show all
Defined in:
lib/githubscore.rb

Constant Summary collapse

VERSION =
"1.0.1"
NOW =
Time.now
SECS_PER_WEEK =
86_400 * 7

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(args) ⇒ Object



11
12
13
# File 'lib/githubscore.rb', line 11

def self.run args
  self.new.run args
end

Instance Method Details

#avg_weeks(ary) ⇒ Object



84
85
86
87
# File 'lib/githubscore.rb', line 84

def avg_weeks ary
  return 0 if ary.empty?
  tot_weeks(ary) / ary.size
end

#clientObject



99
100
101
# File 'lib/githubscore.rb', line 99

def client
  @client ||= Octokit::Client.new :access_token => `gh auth token`.chomp
end

#oldest(ary) ⇒ Object



94
95
96
97
# File 'lib/githubscore.rb', line 94

def oldest ary
  old = ary.map { |h| h[:created_at] }.min
  old && old.iso8601[0..9] || "n/a"
end

#process_args(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/githubscore.rb', line 32

def process_args args
  args << client.user. if args.empty?

  repos = args.flat_map { |name|
    case name
    when /^\w+$/ then
      client.repos(name, :type => "owner")
    when /\// then
      client.repo(name)
    else
      abort "Unhandled arg: #{name.inspect}"
    end
  }.uniq

  repos.sort_by { |h|
    -h[:stargazers_count] - h[:watchers_count] - h[:forks_count]
  }.map { |h| h[:full_name] }
end

#process_repos(repos) ⇒ Object



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
# File 'lib/githubscore.rb', line 51

def process_repos repos
  fmt = "\r%4d/%4d"
  max = repos.size

  done = false
  stats = repos.each_with_index.map { |repo, i|
    next if done

    $stderr.print fmt % [i, max]

    issues = begin
               client.list_issues repo
             rescue Interrupt
               done = true
               next
             end

    next if issues.empty?
    prs, bugs = issues.partition { |h| h[:pull_request] }

    [
     repo,
     bugs.count, oldest(bugs), avg_weeks(bugs),
     prs.count,  oldest(prs),  avg_weeks(prs),
     tot_weeks(issues),
    ]
  }.compact

  $stderr.print "\n\n"

  stats
end

#run(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/githubscore.rb', line 15

def run args
  repos = process_args args
  stats = process_repos repos

  sorted = stats.sort_by { |_, bc, _, ba, pc, _, pa, tot| [-tot, -bc-pc] }

  total = 0

  sorted.each do |data|
    total += data[-1]
    puts "%-35s = %3d %10s %3dw | %3d %10s %3dw | %4dw" % data
  end

  puts
  puts "total = %d" % total
end

#tot_weeks(ary) ⇒ Object



89
90
91
92
# File 'lib/githubscore.rb', line 89

def tot_weeks ary
  return 0 if ary.empty?
  ary.map { |h| NOW - h[:created_at] }.inject(&:+) / SECS_PER_WEEK
end