-
Notifications
You must be signed in to change notification settings - Fork 25
/
source.rb
251 lines (229 loc) · 8.1 KB
/
source.rb
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright (c) 2014-2022 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'digest/md5'
require 'net/http'
require 'json'
require 'shellwords'
require_relative '../pdd'
require_relative '../pdd/puzzle'
module PDD
MARKERS = ["\x40todo", 'TODO:?'].freeze
# Source.
class Source
# Ctor.
# +file+:: Absolute file name with source code
# +path+:: Path to show (without full file name)
def initialize(file, path)
@file = file
@path = path
end
def match_markers(l)
if l.downcase.include? 'todo'
/[^\s]\x40todo/.match(l) do |_|
raise Error, get_no_leading_space_error("\x40todo")
end
/\x40todo(?!\s+#)/.match(l) do |_|
raise Error, get_no_puzzle_marker_error("\x40todo")
end
/\x40todo\s+#\s/.match(l) do |_|
raise Error, get_space_after_hash_error("\x40todo")
end
/[^\s]TODO:?/.match(l) do |_|
raise Error, get_no_leading_space_error('TODO')
end
/TODO(?!:?\s+#)/.match(l) do |_|
raise Error, get_no_puzzle_marker_error('TODO')
end
/TODO:?\s+#\s/.match(l) do |_|
raise Error, get_space_after_hash_error('TODO')
end
a = [%r{(.*(?:^|\s))(?:\x40todo|TODO:|TODO)\s+#([\w\-.:/]+)\s+(.+)}.match(l)]
a.compact
else
[]
end
end
# Fetch all puzzles.
def puzzles
PDD.log.info "Reading #{@path} ..."
puzzles = []
lines = File.readlines(@file, encoding: 'UTF-8')
lines.each_with_index do |line, idx|
begin
match_markers(line).each do |m|
puzzles << puzzle(lines.drop(idx + 1), m, idx)
end
rescue Error, ArgumentError => ex
message = "#{@path}:#{idx + 1} #{ex.message}"
raise Error, message unless PDD.opts && PDD.opts['skip-errors']
PDD.log.warn message
end
end
puzzles
end
private
def get_no_leading_space_error(todo)
"#{todo} must have a leading space to become \
a puzzle, as this page explains: https://github.com/cqfn/pdd#how-to-format"
end
def get_no_puzzle_marker_error(todo)
"#{todo} found, but puzzle can't be parsed, \
most probably because #{todo} is not followed by a puzzle marker, \
as this page explains: https://github.com/cqfn/pdd#how-to-format"
end
def get_space_after_hash_error(todo)
"#{todo} found, but there is an unexpected space \
after the hash sign, it should not be there, \
see https://github.com/cqfn/pdd#how-to-format"
end
# Fetch puzzle
def puzzle(lines, match, idx)
col_idx = match[0].length - match[0].lstrip.length
tail = tail(lines, match[1], col_idx)
body = "#{match[3]} #{tail.join(' ')}".gsub(/\s+/, ' ').strip
body = body.chomp('*/-->').strip
marker = marker(match[2])
Puzzle.new(
marker.merge(
id: "#{marker[:ticket]}-#{Digest::MD5.hexdigest(body)[0..7]}",
lines: "#{idx + 1}-#{idx + tail.size + 1}",
body: body,
file: @path
).merge(git(idx + 1))
)
end
# Parse a marker.
def marker(text)
re = %r{([\w\-.]+)(?::(\d+)(?:(m|h)[a-z]*)?)?(?:/([A-Z]+))?}
match = re.match(text)
if match.nil?
raise "Invalid puzzle marker \"#{text}\", most probably formatted \
against the rules explained here: https://github.com/cqfn/pdd#how-to-format"
end
{
ticket: match[1],
estimate: minutes(match[2], match[3]),
role: match[4].nil? ? 'DEV' : match[4]
}
end
# Parse minutes.
def minutes(num, units)
min = num.nil? ? 0 : Integer(num)
min *= 60 if !units.nil? && units.start_with?('h')
min
end
# Fetch puzzle tail (all lines after the first one)
def tail(lines, prefix, start)
prefix = prefix.rstrip
prefix = " #{' ' * start}" if prefix.empty? # fallback to space indentation
lines
.take_while { |t| match_markers(t).none? && t.start_with?(prefix) }
.take_while do |t|
# account for carriage return in line endings
t_len = t.length - 1
t_len <= prefix.length || t_len > prefix.length + 2
end
.map { |t| t[prefix.length, t.length] }
.map { |t| t.start_with?(' ') ? t[1, t.length] : t }
end
# @todo #75:30min Let's make it possible to fetch Subversion data
# in a similar way as we are doing with Git. We should also just
# skip it if it's not SVN.
# Git information at the line
def git(pos)
dir = Shellwords.escape(File.dirname(@file))
name = Shellwords.escape(File.basename(@file))
git = "cd #{dir} && git"
if `#{git} rev-parse --is-inside-work-tree 2>/dev/null`.strip == 'true'
cmd = "#{git} blame -L #{pos},#{pos} --porcelain #{name}"
add_github_login(Hash[
`#{cmd}`.split("\n").map do |line|
case line
when /^author /
[:author, line.sub(/^author /, '')]
when /^author-mail [^@]+@[^.]+\..+/
[:email, line.sub(/^author-mail <(.+)>$/, '\1')]
when /^author-time /
[
:time,
Time.at(
line.sub(/^author-time ([0-9]+)$/, '\1').to_i
).utc.iso8601
]
end
end.compact
])
else
{}
end
end
def add_github_login(info)
login = find_github_login(info)
info[:author] = "@#{login}" unless login.empty?
info
end
def get_json(query)
uri = URI.parse(query)
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = uri.scheme == 'https'
req = Net::HTTP::Get.new(uri.request_uri)
req.set_content_type('application/json')
res = http.request(req)
JSON.parse res.body
end
def find_github_user(info)
email, author = info.values_at(:email, :author)
# if email is not defined, changes have not been committed
return if email.nil?
base_uri = 'https://api.github.com/search/users?per_page=1'
query = base_uri + "&q=#{email}+in:email"
json = get_json query
# find user by name instead since users can make github email private
unless json['total_count'].positive?
return if author.nil?
query = base_uri + "&q=#{author}+in:fullname"
json = get_json query
end
json['items'].first
end
def find_github_login(info)
user = find_github_user info
user['login']
rescue StandardError
''
end
end
# Verbose Source.
class VerboseSource
# Ctor.
# +file+:: Absolute file name with source code
# +source+:: Instance of source
def initialize(file, source)
@file = file
@source = source
end
# Fetch all puzzles.
def puzzles
@source.puzzles
rescue Error => e
raise Error, "#{@file}; #{e.message}"
end
end
end