Class: Things::IssuesDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/things/issues_diff/version.rb,
lib/things/issues_diff.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#diffObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/things/issues_diff.rb', line 119

def diff
  all_issues = YAML.load_file(@data_file)
  all_tasks = load_tasks
  @config['projects'].each do |project|
    issues = all_issues[project['name']] || {}
    tasks = all_tasks[project['name']] || {}

    unless @args[:ignore_filter]
      excludes = project.dig('milestones', 'exclude') || []
      includes = project.dig('milestones', 'include') || []

      unless includes.empty?
        issues = issues.select { |_, v| includes.include?(v['milestone']) }
      end
      unless excludes.empty?
        issues = issues.reject { |_, v| excludes.include?(v['milestone']) }
      end
    end

    new_issues = (issues.keys - tasks.keys).map { |number| issues[number] }
    old_tasks = (tasks.keys - issues.keys).map { |number| tasks[number] }

    if new_issues.empty? && old_tasks.empty?
      puts "--- #{project['name']}: Clear! ---"
      next
    end

    puts
    puts "--- #{project['name']}: Only exists in the GitHub issues (#{new_issues.length}) ---"
    puts
    new_issues.each do |issue|
      puts issue['title']
      puts issue['url'] + '  ' + issue['milestone']
      puts
    end
    puts
    puts "--- #{project['name']}: Only exists in the Things tasks (#{old_tasks.length}) ---"
    puts
    old_tasks.each do |task|
      puts task['title']
      puts task['url']
      puts
    end
  end
end

#execute(argv) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/things/issues_diff.rb', line 63

def execute(argv)
  parse_options(argv)
  if @commands.include?('help')
    usage
  end

  if @commands.include?('sampleconf')
    sample_config
    exit
  end

  load_config
  init_octokit

  if @commands.empty?
    usage
  end

  if @commands.include?('fetch')
    fetch
  end

  if @commands.include?('diff')
    diff
  end
end

#fetchObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/things/issues_diff.rb', line 90

def fetch
  result = {}
  @config['projects'].each do |project|
    puts "Loading: #{project['name']}"
    result[project['name']] = {}
    issues = @client.list_issues(project['name'], assignee: @config['user'])
    issues.each do |issue|
      labels = []
      if issue[:labels]
        labels = issue[:labels].map { |lbl| lbl[:name] }
      end

      milestone = issue[:milestone] ? issue[:milestone][:title] : ''
      entry = {
        'title' => issue[:title],
        'url' => issue[:html_url],
        'milestone' => milestone,
        'labels' => labels
      }
      result[project['name']][issue[:number].to_i] = entry
    end
  end
  yaml = YAML.dump(result)
  File.open(@data_file, 'w') do |out|
    out << yaml
  end
  puts "Saved: #{@data_file}"
end

#init_octokitObject



58
59
60
61
# File 'lib/things/issues_diff.rb', line 58

def init_octokit
  @client = Octokit::Client.new(access_token: @config['token'])
  @client.auto_paginate = true
end

#load_configObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/things/issues_diff.rb', line 30

def load_config
  default_config_file = File.join(ENV['HOME'], '.things-diff/config.yml')
  @config_file = @args[:config_file] || default_config_file
  unless File.exist?(@config_file)
    puts "Error: Config file not found: #{@config_file}"

    unless File.exist?(default_config_file)
      puts
      puts 'Please create config.yml and edit.'
      puts '  $ mkdir ~/.things-diff/'
      puts '  $ things-diff sampleconf > ~/.things-diff/config.yml'
      puts '  $ vi ~/.things-diff/config.yml'
    end
    exit 1
  end
  @config_file = File.absolute_path(@config_file)
  @config = YAML.load_file(@config_file)

  if @config['data_file']
    @data_file = File.join(File.dirname(@config_file), @config['data_file'])
    @data_file = File.absolute_path(@data_file)
  else
    puts 'Error: Please set the data_file'
    exit 1
  end
end

#load_tasksObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/things/issues_diff.rb', line 165

def load_tasks
  tasks = `things.sh all`.split(/\r?\n/)
  project_names = @config['projects'].map { |prj| prj['name'] }
  result = project_names.map { |name| [name, {}] }.to_h
  tasks.each do |task|
    project_names.each do |name|
      next unless task.include?(name)
      next unless task =~ /\sIssue #(\d+)\s/

      number = Regexp.last_match[1].to_i
      if result[name][number]
        puts "WARN: duplicated #{name} #{number}"
      end
      result[name][number] = {
        'url' => "https://github.com/#{name}/issues/#{number}",
        'title' => task
      }
    end
  end
  result
end

#parse_options(argv) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/things/issues_diff.rb', line 9

def parse_options(argv)
  opts = OptionParser.new
  opts.version = VERSION

  opts.on('-h', '--help', 'Show help') do
    usage
  end

  @args = {}
  opts.on('-c FILE', '--config', 'Config file') do |v|
    @args[:config_file] = v
  end
  opts.on('--ignore-filter', 'Do not filter issues/tasks') do |v|
    @args[:ignore_filter] = v
  end
  opts.parse!(argv)

  commands = argv[0].nil? ? [] : argv.shift.split(/\s*,\s*/)
  @commands = %w[help sampleconf fetch diff].& commands
end

#sample_configObject



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
# File 'lib/things/issues_diff.rb', line 192

def sample_config
  puts <<END_OF_CONFIG
# ----------------------------------------------------------------
# things-diff config file
# https://github.com/koseki/things-issues-diff
#
# $ mkdir ~/.things-diff
# $ things-diff sampleconf > ~/.things-diff/config.yml
# ----------------------------------------------------------------

# Create a token with the repo scope.
# https://github.com/settings/tokens/new
token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# The relative path is based on the config file.
data_file: data.yml

user: assignee-user-name
projects:
- name: repos/path
  milestones:
    exclude:
      - Icebox
      - Backlog
  labels:
    include:
      - important

- name: repos/path2
- name: repos/path3

END_OF_CONFIG
end

#usageObject



187
188
189
190
# File 'lib/things/issues_diff.rb', line 187

def usage
  puts 'Usage: things-diff --config FILE [help|sampleconf|fetch|diff]'
  exit 1
end