Module: Gh::Cards

Defined in:
lib/gh/cards.rb,
lib/gh/cards/cli.rb,
lib/gh/cards/version.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.2.4"

Class Method Summary collapse

Class Method Details

._generate_cards_html(template, directory, repo, cards) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gh/cards.rb', line 100

def self._generate_cards_html(template, directory, repo, cards)

  if File.file?("#{__dir__}/../../templates/#{template}.html.erb")
    template_file = "#{__dir__}/../../templates/#{template}.html.erb"
  elsif File.file?("#{__dir__}/../../templates/#{template}")
    template_file = "#{__dir__}/../../templates/#{template}"
  elsif File.file?(template)
    template_file = template
  else
    abort("Unable to find template: #{template}")
  end

  puts "Generating cards using template: #{template}"
  erb = ERB.new(File.read(template_file), nil, '-')
  File.write("#{directory}/cards.html", erb.result(binding))
end

._get_cards_from_gh_issues(client, repo, last_issue) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gh/cards.rb', line 76

def self._get_cards_from_gh_issues(client, repo, last_issue)
  issues = client.list_issues repo
  issues.reject!{ |issue| issue[:number] <= last_issue or issue[:pull_request]}

  cards = issues.map do |issue|
    {
      :title => issue[:title],
      :number => issue[:number],
      :labels => issue[:labels].map{|label| {:name => label[:name], :color => label[:color]}},
      :milestone => issue[:milestone] ? issue[:milestone][:title] : nil,
      :created_by => _get_gh_user_name(client, issue[:user][:login]),
      :created_at => issue[:created_at]
    }
  end
  return cards
end

._get_gh_user_name(client, user_login) ⇒ Object



93
94
95
96
97
98
# File 'lib/gh/cards.rb', line 93

def self._get_gh_user_name(client, )
  if $user_names_cache[.to_sym].nil?
    $user_names_cache[.to_sym] = client.user()[:name] || 
  end
  return $user_names_cache[.to_sym]
end

._get_last_issue(directory) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gh/cards.rb', line 52

def self._get_last_issue(directory)
  last_file = "#{directory}/last"

  if ! File.directory?(directory)
    FileUtils.mkdir_p directory
  end

  if ! File.file?(last_file)
    File.open(last_file, "w") do |f|
      f.write(0)
    end
  end

  last_issue_number = File.open(last_file, "r"){ |file| file.read }
  return last_issue_number.to_i
end

._get_this_repoObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/gh/cards.rb', line 41

def self._get_this_repo
  repo_regex = /^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$/
  begin
    g = Git.open('.')
  rescue
    abort("Unable to detect git repo! Make sure you're in a repo or use the --repo option")
  end
  r = g.remote('origin').url.match(repo_regex)
  return "#{r[4]}/#{r[5]}"
end

._set_last_issue(directory, cards) ⇒ Object



69
70
71
72
73
74
# File 'lib/gh/cards.rb', line 69

def self._set_last_issue(directory, cards)
  last_file = "#{directory}/last"
  File.open(last_file, "w") do |f|
    f.write(cards[0][:number])
  end
end

.generate(template, directory, repo) ⇒ Object



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
# File 'lib/gh/cards.rb', line 12

def self.generate(template, directory, repo)
  repo = repo || _get_this_repo()
  last_issue = _get_last_issue(directory)

  client = Octokit::Client.new(:netrc => true, :per_page => 100,
    :auto_traversal => true, :auto_paginate => true)

  cards = _get_cards_from_gh_issues(client, repo, last_issue)

  if cards.empty?
    exit_msg = "No issues found in #{repo}"
    if last_issue > 0
      exit_msg << "\n\nFile: #{directory}/last shows the last issue generated as #{last_issue}"
      exit_msg << "\nTo reset and generate all cards, delete #{directory}/last and re-run"
    end
    puts exit_msg
    exit 0
  end

  puts "Found #{cards.length} new #{"issue".pluralize cards.length} in #{repo} to generate cards from"

  _generate_cards_html(template, directory, repo, cards)

  _set_last_issue(directory, cards)

  system("open", "#{directory}/cards.html")
  puts "Done!"
end