This repository has been archived by the owner on May 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdone.thor
executable file
·169 lines (148 loc) · 5.38 KB
/
done.thor
1
2
3
4
5
6
7
8
9
10
11
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
40
41
42
43
44
45
46
47
48
49
50
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
83
84
85
86
87
88
89
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
118
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
164
165
166
167
168
169
#!/usr/bin/env ruby
CURRENT_PATH = File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__)
ENV['BUNDLE_GEMFILE'] = "#{CURRENT_PATH}/Gemfile"
require 'rubygems'
require 'fileutils'
require 'logger'
require 'tempfile'
require 'bundler'
require 'open-uri'
require 'net/http'
Bundler.require
I18n.enforce_available_locales = false # Avoid warnings.
CONFIG = YAML.load_file(File.expand_path('config.yml', CURRENT_PATH)).with_indifferent_access
class Done < Thor
# External editor
EDITOR = ENV['EDITOR'] || 'vim'
# Set up log
LOG_FILE = File.expand_path('time.log', "#{CURRENT_PATH}/log")
FileUtils.mkdir_p(File.dirname(LOG_FILE))
LOG = Logger.new(LOG_FILE, 'weekly')
LOG.formatter = proc {|severity, datetime, progname, msg|
offset = msg.split.first.to_i
if offset > 0
datetime -= offset.minutes
msg = msg.split[1..-1].join(' ')
end
puts msg
"#{datetime.strftime("%F %T")} #{File.basename(`pwd`.chomp)} #{msg}\n"
}
desc 'report', 'Daily Report'
method_options :days_ago => 0
def report
if File.exists?(path = File.expand_path('contacts.yml', CURRENT_PATH))
contacts = YAML.load_file(path)
else
contacts = get_from_api('contacts')
File.write(path, contacts.to_yaml)
end
# e.g. contacts = [{"id"=>1, "name"=>"Contact Name", "pinned"=>false, "short_code"=>"contact_name", "default_rate_dollars"=>""}]
# Parse log file, create report and open it in vim
date = options.days_ago.days.ago
logs = `grep '^#{date.strftime("%F")}' #{LOG_FILE}`.split("\n").sort
directories = []
t = Tempfile.new(%w(report .txt))
t.puts "# Total: 0.0"
t.puts "# Hours Project Comment"
time = nil
entries = []
logs.each do |log|
puts log
logtime, dir, comment = /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (\S+) ?(.*)$/.match(log).captures
directories << dir unless directories.include?(dir)
logtime = Time.parse(logtime)
time = logtime and next if time.nil?
hours = (logtime - time) / 1.hours
new_time = Time.parse(log[/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/])
hours = (new_time - time) / 1.hour
time = new_time
next if hours.zero? || /^out$/i.match(comment.squish)
entries << "#{sprintf('%5.2f', hours)} #{dir} #{log.split[3..-1].join(' ')}\n"
end
entries.sort_by { |l| l.split[1] }.each {|e| t << e}
t.puts
directories.each do |dir|
contact_id = CONFIG[:contacts][dir]
contact = contacts.find { |c| c['id'] == contact_id.to_i }
if contact
t.puts "# #{contact_id.to_s.rjust(7)} #{dir} #{contact['name']}"
else
t.puts "# #{dir} WARNING: No project set"
end
end
t.puts
contacts.sort_by { |c| c['name'] }.each do |contact|
t.puts "# #{contact['id'].to_s.rjust(7)} #{contact['name']}"
end
t.close
system "vim -S #{File.expand_path(CURRENT_PATH)}/done.vim #{t.path}"
# Reopen edited report, parse and send to API
t.open
t.rewind
contents = t.read.split("\n")
contents.reject! {|l| l.squish.blank? || l =~ /^\s*#/}
if contents.blank?
puts 'Aborting. Nothing to do.'
else
puts 'Sending time entries to API...'
contents.each do |l|
puts l.inspect
hours, dir, comment = l.split(' ', 3)
comment = comment.squish
contact_id = dir.to_i > 0 ? dir.to_i : CONFIG[:contacts][dir]
puts "No contact found for #{dir}!" and next if contact_id.blank?
post_to_api('entries', {
'entry[contact_id]' => contact_id,
'entry[logged_at]' => date,
'entry[duration]' => hours.to_f.hours.to_i,
'entry[description]' => comment,
})
end
end
t.close!
end
desc 'log [COMMENT]', 'Create a log entry from the given comment'
def log(*comments)
count_of_todays_logs = `grep -c '^#{Time.now.strftime("%F")}' #{LOG_FILE}`.chomp
if count_of_todays_logs.to_i.zero?
LOG.info "###### #{Time.now.strftime("%A %D")} ######"
end
if comments.present?
LOG.info comments.join(' ').squish
end
end
desc "gitlog", "Use the latest git log as the comment"
def gitlog
comment = `git log -n1 --pretty=format:%s --no-merges`
log(comment)
end
desc "editlog", "edit the current log file"
def editlog
system "#{EDITOR} #{LOG_FILE}"
end
desc 'githublog', 'Create a log entry from the Github issue description of the latest commit.'
def githublog
comment = `git log -n1 --pretty=format:%s --no-merges`
issue_number = comment.to_s[/#(\d+)/].to_s.gsub('#', '')
repo = `git remote show origin -n | grep Fetch | grep github`.to_s.match(%r{:([^/:]+/.+)\.git}).to_a[1]
if issue_number.present? && repo.present?
issue = JSON.parse(open("https://api.github.com/repos/#{repo}/issues/#{issue_number}?access_token=#{CONFIG[:github_token]}").read)
comment = issue['title'] if issue['title'].present?
end
log(comment)
end
private
def get_from_api(resource, params = {})
params.merge!(CONFIG[:api_params])
JSON.parse(open("#{CONFIG[:api_url]}#{resource}.json?#{params.to_param}").read)
end
def post_to_api(resource, params = {})
params.merge!(CONFIG[:api_params])
url = "#{CONFIG[:api_url]}#{resource}.json"
params_string = params.map do |key, value|
[key, value].join('=')
end.join('&').gsub("'", "\\'")
puts `curl -X POST --silent --data '#{params_string}' #{url}`
end
end
Done.start