lazylead 0.1.2 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.0pdd.yml +4 -1
- data/.circleci/config.yml +14 -4
- data/.docker/Dockerfile +5 -4
- data/.docker/vcs.dockerfile +10 -0
- data/.docs/accuracy.md +107 -0
- data/.docs/accuracy_email.jpg +0 -0
- data/.docs/accuracy_jira_comment.jpg +0 -0
- data/.docs/duedate_expired.md +92 -0
- data/.docs/propagate_down.md +89 -0
- data/.pdd +1 -1
- data/.rubocop.yml +7 -1
- data/.rultor.yml +2 -2
- data/.simplecov +0 -6
- data/bin/lazylead +13 -5
- data/lazylead.gemspec +4 -16
- data/lib/lazylead/cc.rb +180 -0
- data/lib/lazylead/cli/app.rb +4 -3
- data/lib/lazylead/exchange.rb +15 -2
- data/lib/lazylead/home.rb +38 -0
- data/lib/lazylead/log.rb +30 -8
- data/lib/lazylead/model.rb +60 -16
- data/lib/lazylead/opts.rb +68 -0
- data/lib/lazylead/postman.rb +15 -15
- data/lib/lazylead/schedule.rb +6 -4
- data/lib/lazylead/smtp.rb +1 -1
- data/lib/lazylead/system/fake.rb +1 -1
- data/lib/lazylead/system/jira.rb +55 -12
- data/lib/lazylead/system/synced.rb +2 -1
- data/lib/lazylead/task/accuracy/accuracy.rb +140 -0
- data/lib/lazylead/task/accuracy/affected_build.rb +43 -0
- data/lib/lazylead/task/accuracy/requirement.rb +40 -0
- data/lib/lazylead/task/alert.rb +8 -6
- data/lib/lazylead/task/confluence_ref.rb +4 -3
- data/lib/lazylead/task/echo.rb +4 -0
- data/lib/lazylead/task/fix_version.rb +10 -6
- data/lib/lazylead/task/missing_comment.rb +7 -5
- data/lib/lazylead/task/propagate_down.rb +126 -0
- data/lib/lazylead/task/savepoint.rb +58 -0
- data/lib/lazylead/task/touch.rb +102 -0
- data/lib/lazylead/version.rb +1 -1
- data/lib/messages/accuracy.erb +118 -0
- data/lib/messages/due_date_expired.erb +8 -7
- data/lib/messages/illegal_fixversion_change.erb +9 -8
- data/lib/messages/missing_comment.erb +10 -9
- data/lib/messages/savepoint.erb +43 -0
- data/lib/messages/svn_touch.erb +147 -0
- data/readme.md +37 -34
- data/test/lazylead/cc_test.rb +153 -0
- data/test/lazylead/cli/app_test.rb +3 -4
- data/test/lazylead/exchange_test.rb +22 -2
- data/test/lazylead/model_test.rb +14 -3
- data/test/lazylead/opts_test.rb +66 -0
- data/test/lazylead/postman_test.rb +57 -0
- data/test/lazylead/smtp_test.rb +1 -1
- data/test/lazylead/system/jira_test.rb +43 -1
- data/test/lazylead/task/accuracy/accuracy_test.rb +73 -0
- data/test/lazylead/task/accuracy/affected_build_test.rb +42 -0
- data/test/lazylead/task/assignee_alert_test.rb +47 -0
- data/test/lazylead/task/duedate_test.rb +52 -30
- data/test/lazylead/task/fix_version_test.rb +11 -10
- data/test/lazylead/task/missing_comment_test.rb +13 -13
- data/test/lazylead/task/propagate_down_test.rb +88 -0
- data/test/lazylead/task/savepoint_test.rb +51 -0
- data/test/lazylead/task/touch_test.rb +63 -0
- data/test/test.rb +11 -0
- data/upgrades/sqlite/001-install-main-lazylead-tables.sql +0 -1
- data/upgrades/sqlite/999.testdata.sql +8 -2
- metadata +43 -176
- data/todo.yml +0 -6
data/lib/lazylead/model.rb
CHANGED
@@ -22,11 +22,15 @@
|
|
22
22
|
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
23
23
|
# OR OTHER DEALINGS IN THE SOFTWARE.
|
24
24
|
|
25
|
-
require "
|
25
|
+
require "backtrace"
|
26
26
|
require "require_all"
|
27
|
+
require "forwardable"
|
28
|
+
require "active_record"
|
27
29
|
require_rel "task"
|
28
30
|
require_rel "system"
|
31
|
+
require_relative "cc"
|
29
32
|
require_relative "log"
|
33
|
+
require_relative "opts"
|
30
34
|
require_relative "postman"
|
31
35
|
require_relative "exchange"
|
32
36
|
|
@@ -64,7 +68,9 @@ module Lazylead
|
|
64
68
|
opts.each_with_object({}) do |e, o|
|
65
69
|
k = e[0]
|
66
70
|
v = e[1]
|
67
|
-
|
71
|
+
if v.respond_to? :start_with?
|
72
|
+
v = ENV[v.slice(2, v.length - 3)] if v.start_with? "${"
|
73
|
+
end
|
68
74
|
o[k] = v
|
69
75
|
end
|
70
76
|
end
|
@@ -88,35 +94,74 @@ module Lazylead
|
|
88
94
|
belongs_to :team, foreign_key: "team_id"
|
89
95
|
belongs_to :system, foreign_key: "system"
|
90
96
|
|
91
|
-
def exec
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
+
def exec
|
98
|
+
sys = system.connect
|
99
|
+
opts = props
|
100
|
+
opts = detect_cc(sys) if opts.key? "cc"
|
101
|
+
action.constantize.new(log).run(sys, postman, opts)
|
102
|
+
end
|
103
|
+
|
104
|
+
def detect_cc(sys)
|
105
|
+
opts = props
|
106
|
+
opts["cc"] = CC.new.detect(opts["cc"], sys)
|
107
|
+
return opts.except "cc" if opts["cc"].is_a? EmptyCC
|
108
|
+
opts
|
97
109
|
end
|
98
110
|
|
99
111
|
def props
|
100
|
-
|
101
|
-
|
112
|
+
@props ||= begin
|
113
|
+
if team.nil?
|
114
|
+
Opts.new(env(to_hash))
|
115
|
+
else
|
116
|
+
Opts.new(env(team.to_hash.merge(to_hash)))
|
117
|
+
end
|
118
|
+
end
|
102
119
|
end
|
103
120
|
|
104
|
-
def postman
|
121
|
+
def postman
|
105
122
|
if props.key? "postman"
|
106
|
-
props["postman"].constantize.new
|
123
|
+
props["postman"].constantize.new
|
107
124
|
else
|
108
|
-
log.warn "No postman details provided, an local stub is used."
|
109
125
|
Postman.new
|
110
126
|
end
|
111
127
|
end
|
112
128
|
end
|
113
129
|
|
130
|
+
# A task with extended logging
|
131
|
+
# @see Lazylead::ORM::Task
|
132
|
+
class VerboseTask
|
133
|
+
extend Forwardable
|
134
|
+
def_delegators :@orig, :id, :name, :team, :to_s, :inspect, :props
|
135
|
+
|
136
|
+
def initialize(orig, log = Log.new)
|
137
|
+
@orig = orig
|
138
|
+
@log = log
|
139
|
+
end
|
140
|
+
|
141
|
+
def exec
|
142
|
+
Logging.mdc["tid"] = "task #{id}"
|
143
|
+
@log.debug "'#{name}' is started."
|
144
|
+
@log.warn "No postman, stub is used." unless props.key? "postman"
|
145
|
+
@log.warn "No team." if team.nil?
|
146
|
+
@orig.exec
|
147
|
+
@log.debug "'#{name}' is completed"
|
148
|
+
rescue StandardError => e
|
149
|
+
msg = <<~MSG
|
150
|
+
ll-006: Task ##{id} #{e} (#{e.class}) at #{self}
|
151
|
+
#{Backtrace.new(e) if ARGV.include? '--trace'}"
|
152
|
+
MSG
|
153
|
+
@log.error msg
|
154
|
+
ensure
|
155
|
+
Logging.mdc["tid"] = ""
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
114
159
|
# Ticketing systems to monitor.
|
115
160
|
class System < ActiveRecord::Base
|
116
161
|
include ORM
|
117
162
|
|
118
163
|
# Make an instance of ticketing system for future interaction.
|
119
|
-
def connect(log = Log
|
164
|
+
def connect(log = Log.new)
|
120
165
|
opts = to_hash
|
121
166
|
if opts["type"].empty?
|
122
167
|
log.warn "No task system details provided, an empty stub is used."
|
@@ -124,8 +169,7 @@ module Lazylead
|
|
124
169
|
else
|
125
170
|
opts["type"].constantize.new(
|
126
171
|
env(opts.except("type", "salt")),
|
127
|
-
opts["salt"].blank? ? NoSalt.new : Salt.new(opts["salt"])
|
128
|
-
log
|
172
|
+
opts["salt"].blank? ? NoSalt.new : Salt.new(opts["salt"])
|
129
173
|
)
|
130
174
|
end
|
131
175
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The MIT License
|
4
|
+
#
|
5
|
+
# Copyright (c) 2019-2020 Yurii Dubinka
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"),
|
9
|
+
# to deal in the Software without restriction, including without limitation
|
10
|
+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
11
|
+
# and/or sell copies of the Software, and to permit persons to whom
|
12
|
+
# the Software is furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included
|
15
|
+
# in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
22
|
+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
23
|
+
# OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
require "forwardable"
|
26
|
+
|
27
|
+
module Lazylead
|
28
|
+
#
|
29
|
+
# Default options for all lazylead tasks.
|
30
|
+
#
|
31
|
+
# Author:: Yurii Dubinka (yurii.dubinka@gmail.com)
|
32
|
+
# Copyright:: Copyright (c) 2019-2020 Yurii Dubinka
|
33
|
+
# License:: MIT
|
34
|
+
class Opts
|
35
|
+
extend Forwardable
|
36
|
+
def_delegators :@origin, :[], :[]=, :to_s, :key?, :fetch, :merge
|
37
|
+
|
38
|
+
def initialize(origin = {})
|
39
|
+
@origin = origin
|
40
|
+
end
|
41
|
+
|
42
|
+
# Split text value by delimiter, trim all spaces and reject blank items
|
43
|
+
def slice(key, delim)
|
44
|
+
to_h[key].split(delim).map(&:chomp).map(&:strip).reject(&:blank?)
|
45
|
+
end
|
46
|
+
|
47
|
+
def blank?(key)
|
48
|
+
to_h[key].nil? || @origin[key].blank?
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_h
|
52
|
+
@origin
|
53
|
+
end
|
54
|
+
|
55
|
+
# Default Jira options to use during search for all Jira-based tasks.
|
56
|
+
def jira_defaults
|
57
|
+
{
|
58
|
+
max_results: fetch("max_results", 50),
|
59
|
+
fields: jira_fields
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
# Default fields which to fetch within the Jira issue
|
64
|
+
def jira_fields
|
65
|
+
to_h.fetch("fields", "").split(",").map(&:to_sym)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/lazylead/postman.rb
CHANGED
@@ -46,7 +46,7 @@ module Lazylead
|
|
46
46
|
class Postman
|
47
47
|
include Emailing
|
48
48
|
|
49
|
-
def initialize(log = Log
|
49
|
+
def initialize(log = Log.new)
|
50
50
|
@log = log
|
51
51
|
end
|
52
52
|
|
@@ -54,25 +54,25 @@ module Lazylead
|
|
54
54
|
# :opts :: the mail configuration like to, from, cc, subject, template.
|
55
55
|
def send(opts)
|
56
56
|
html = make_body(opts)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
body html
|
66
|
-
end
|
57
|
+
mail = Mail.new
|
58
|
+
mail.to opts[:to] || opts["to"]
|
59
|
+
mail.from opts["from"]
|
60
|
+
mail.cc opts["cc"] if opts.key? "cc"
|
61
|
+
mail.subject opts["subject"]
|
62
|
+
mail.html_part do
|
63
|
+
content_type "text/html; charset=UTF-8"
|
64
|
+
body html
|
67
65
|
end
|
66
|
+
add_attachments mail, opts
|
67
|
+
mail.deliver
|
68
68
|
@log.debug "Email was generated from #{opts} and send by #{__FILE__}. " \
|
69
69
|
"Here is the body: #{html}"
|
70
70
|
end
|
71
71
|
|
72
|
-
def
|
73
|
-
|
74
|
-
|
75
|
-
|
72
|
+
def add_attachments(mail, opts)
|
73
|
+
return unless opts.key? "attachments"
|
74
|
+
opts["attachments"].select { |a| File.file? a }
|
75
|
+
.each { |a| mail.add_file a }
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
data/lib/lazylead/schedule.rb
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
23
23
|
# OR OTHER DEALINGS IN THE SOFTWARE.
|
24
24
|
|
25
|
-
require "
|
25
|
+
require "active_support"
|
26
26
|
require "rufus-scheduler"
|
27
27
|
require_relative "log"
|
28
28
|
require_relative "model"
|
@@ -40,7 +40,7 @@ module Lazylead
|
|
40
40
|
# schedule some task once or at particular time period like in next 200ms).
|
41
41
|
# For cron expressions we should define separate test suite which will test
|
42
42
|
# in parallel without blocking main CI process.
|
43
|
-
def initialize(log = Log
|
43
|
+
def initialize(log = Log.new, cling = true)
|
44
44
|
@log = log
|
45
45
|
@cling = cling
|
46
46
|
@trigger = Rufus::Scheduler.new
|
@@ -51,7 +51,9 @@ module Lazylead
|
|
51
51
|
def register(task)
|
52
52
|
raise "ll-002: task can't be a null" if task.nil?
|
53
53
|
@trigger.cron task.cron do
|
54
|
-
|
54
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
55
|
+
ORM::VerboseTask.new(task, @log).exec
|
56
|
+
end
|
55
57
|
end
|
56
58
|
@log.debug "Task scheduled: #{task}"
|
57
59
|
end
|
@@ -73,7 +75,7 @@ module Lazylead
|
|
73
75
|
|
74
76
|
# Fake application schedule for unit testing purposes
|
75
77
|
class NoSchedule
|
76
|
-
def initialize(log = Log
|
78
|
+
def initialize(log = Log.new)
|
77
79
|
@log = log
|
78
80
|
end
|
79
81
|
|
data/lib/lazylead/smtp.rb
CHANGED
@@ -34,7 +34,7 @@ module Lazylead
|
|
34
34
|
# Copyright:: Copyright (c) 2019-2020 Yurii Dubinka
|
35
35
|
# License:: MIT
|
36
36
|
class Smtp
|
37
|
-
def initialize(log = Log
|
37
|
+
def initialize(log = Log.new, salt = NoSalt.new, opts = {})
|
38
38
|
@log = log
|
39
39
|
@salt = salt
|
40
40
|
@opts = opts
|
data/lib/lazylead/system/fake.rb
CHANGED
data/lib/lazylead/system/jira.rb
CHANGED
@@ -23,6 +23,7 @@
|
|
23
23
|
# OR OTHER DEALINGS IN THE SOFTWARE.
|
24
24
|
|
25
25
|
require "jira-ruby"
|
26
|
+
require "forwardable"
|
26
27
|
require_relative "../salt"
|
27
28
|
|
28
29
|
module Lazylead
|
@@ -35,7 +36,7 @@ module Lazylead
|
|
35
36
|
# @todo #57/DEV The debug method should be moved outside of ctor.
|
36
37
|
# This was moved here from 'client' method because Rubocop failed the build
|
37
38
|
# due to 'Metrics/AbcSize' violation.
|
38
|
-
def initialize(opts, salt = NoSalt.new, log = Log
|
39
|
+
def initialize(opts, salt = NoSalt.new, log = Log.new)
|
39
40
|
@opts = opts
|
40
41
|
@salt = salt
|
41
42
|
@log = log
|
@@ -46,7 +47,7 @@ module Lazylead
|
|
46
47
|
|
47
48
|
def issues(jql, opts = {})
|
48
49
|
raw do |jira|
|
49
|
-
jira.Issue.jql(jql, opts).map { |i| Lazylead::Issue.new(i) }
|
50
|
+
jira.Issue.jql(jql, opts).map { |i| Lazylead::Issue.new(i, jira) }
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
@@ -136,8 +137,9 @@ module Lazylead
|
|
136
137
|
# Copyright:: Copyright (c) 2019-2020 Yurii Dubinka
|
137
138
|
# License:: MIT
|
138
139
|
class Issue
|
139
|
-
def initialize(issue)
|
140
|
+
def initialize(issue, jira)
|
140
141
|
@issue = issue
|
142
|
+
@jira = jira
|
141
143
|
end
|
142
144
|
|
143
145
|
def id
|
@@ -183,7 +185,10 @@ module Lazylead
|
|
183
185
|
end
|
184
186
|
|
185
187
|
def comments
|
186
|
-
@
|
188
|
+
return @comments if defined? @comments
|
189
|
+
@comments = @jira.Issue.find(@issue.id, expand: "comments", fields: "")
|
190
|
+
.comments
|
191
|
+
.map { |c| Comment.new(c) }
|
187
192
|
end
|
188
193
|
|
189
194
|
def to_s
|
@@ -193,6 +198,14 @@ module Lazylead
|
|
193
198
|
def inspect
|
194
199
|
to_s
|
195
200
|
end
|
201
|
+
|
202
|
+
def status
|
203
|
+
@issue.status.attrs["name"]
|
204
|
+
end
|
205
|
+
|
206
|
+
def post(markdown)
|
207
|
+
@issue.comments.build.save!(body: markdown)
|
208
|
+
end
|
196
209
|
end
|
197
210
|
|
198
211
|
# The jira issue comments
|
@@ -224,26 +237,56 @@ module Lazylead
|
|
224
237
|
end
|
225
238
|
end
|
226
239
|
|
240
|
+
# Comment in jira ticket
|
241
|
+
class Comment
|
242
|
+
def initialize(comment)
|
243
|
+
@comment = comment
|
244
|
+
end
|
245
|
+
|
246
|
+
# Check that comment has expected text
|
247
|
+
def include?(text)
|
248
|
+
@comment.attrs["body"].include? text
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
227
252
|
# Jira instance without authentication in order to access public filters
|
228
253
|
# or dashboards.
|
229
254
|
class NoAuthJira
|
230
|
-
|
255
|
+
extend Forwardable
|
256
|
+
def_delegators :@jira, :issues, :raw
|
257
|
+
|
258
|
+
def initialize(url, path = "", log = Log.new)
|
231
259
|
@jira = Jira.new(
|
232
|
-
{
|
260
|
+
{
|
261
|
+
username: nil,
|
262
|
+
password: nil,
|
263
|
+
site: url,
|
264
|
+
context_path: path
|
265
|
+
},
|
233
266
|
NoSalt.new,
|
234
267
|
log
|
235
268
|
)
|
236
269
|
end
|
270
|
+
end
|
237
271
|
|
238
|
-
|
239
|
-
|
272
|
+
# A fake jira system which allows to work with sub-tasks.
|
273
|
+
class Fake
|
274
|
+
def initialize(issues)
|
275
|
+
@issues = issues
|
276
|
+
end
|
277
|
+
|
278
|
+
def issues(*)
|
279
|
+
@issues
|
240
280
|
end
|
241
281
|
|
242
282
|
# Execute request to the ticketing system using raw client.
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
283
|
+
def raw
|
284
|
+
raise "ll-08: No block given to method" unless block_given?
|
285
|
+
yield(OpenStruct.new(Issue: self))
|
286
|
+
end
|
287
|
+
|
288
|
+
def find(id)
|
289
|
+
@issues.detect { |i| i.id.eql? id }
|
247
290
|
end
|
248
291
|
end
|
249
292
|
end
|
@@ -30,7 +30,8 @@ module Lazylead
|
|
30
30
|
@sys = sys
|
31
31
|
end
|
32
32
|
|
33
|
-
# @todo #/DEV Unit tests for 'issues' function
|
33
|
+
# @todo #/DEV Unit tests for 'issues' function, moreover the other methods
|
34
|
+
# from ticketing system obj are required
|
34
35
|
#
|
35
36
|
def issues(jql)
|
36
37
|
@mutex.synchronize do
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The MIT License
|
4
|
+
#
|
5
|
+
# Copyright (c) 2019-2020 Yurii Dubinka
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"),
|
9
|
+
# to deal in the Software without restriction, including without limitation
|
10
|
+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
11
|
+
# and/or sell copies of the Software, and to permit persons to whom
|
12
|
+
# the Software is furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included
|
15
|
+
# in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
22
|
+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
23
|
+
# OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
require_relative "../../log"
|
26
|
+
require_relative "../../opts"
|
27
|
+
require_relative "../../email"
|
28
|
+
require_relative "../../version"
|
29
|
+
require_relative "../../postman"
|
30
|
+
|
31
|
+
module Lazylead
|
32
|
+
module Task
|
33
|
+
#
|
34
|
+
# Evaluate ticket format and accuracy
|
35
|
+
#
|
36
|
+
# The task supports the following features:
|
37
|
+
# - fetch issues from remote ticketing system by query
|
38
|
+
# - evaluate each field within the ticket
|
39
|
+
# - post the score to the ticket
|
40
|
+
class Accuracy
|
41
|
+
def initialize(log = Log.new)
|
42
|
+
@log = log
|
43
|
+
end
|
44
|
+
|
45
|
+
def run(sys, postman, opts)
|
46
|
+
require_rules
|
47
|
+
rules = opts.slice("rules", ",")
|
48
|
+
.map(&:constantize)
|
49
|
+
.map(&:new)
|
50
|
+
raised = sys.issues(opts["jql"], opts.jira_defaults)
|
51
|
+
.map { |i| Score.new(i, opts, rules) }
|
52
|
+
.each(&:evaluate)
|
53
|
+
.each(&:post)
|
54
|
+
postman.send opts.merge(tickets: raised) unless raised.empty?
|
55
|
+
end
|
56
|
+
|
57
|
+
# Load all ticket accuracy rules for future verification
|
58
|
+
def require_rules
|
59
|
+
rules = File.dirname(__FILE__)
|
60
|
+
$LOAD_PATH.unshift(rules) unless $LOAD_PATH.include?(rules)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# The ticket score based on fields content.
|
66
|
+
class Score
|
67
|
+
attr_reader :issue, :total, :score, :accuracy
|
68
|
+
|
69
|
+
def initialize(issue, opts, rules)
|
70
|
+
@issue = issue
|
71
|
+
@link = opts["docs"]
|
72
|
+
@opts = opts
|
73
|
+
@rules = rules
|
74
|
+
end
|
75
|
+
|
76
|
+
# Estimate the ticket score and accuracy.
|
77
|
+
# Accuracy is a percentage between current score and maximum possible value.
|
78
|
+
def evaluate(digits = 2)
|
79
|
+
@total = @rules.map(&:score).sum
|
80
|
+
@score = @rules.select { |r| r.passed(@issue) }
|
81
|
+
.map(&:score)
|
82
|
+
.sum
|
83
|
+
@accuracy = (score / @total * 100).round(digits)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Post the comment with score and accuracy to the ticket.
|
87
|
+
def post
|
88
|
+
@issue.post(comment) unless @opts.key? "silent"
|
89
|
+
end
|
90
|
+
|
91
|
+
# The jira comment in markdown format
|
92
|
+
def comment
|
93
|
+
comment = [
|
94
|
+
"Hi [~#{@issue.reporter.id}],",
|
95
|
+
"",
|
96
|
+
"The triage accuracy is '{color:#{color}}#{@score}{color}'" \
|
97
|
+
" (~{color:#{color}}#{@accuracy}%{color}), here are the reasons why:",
|
98
|
+
"|| Ticket requirement || Status || Field ||"
|
99
|
+
]
|
100
|
+
@rules.each do |r|
|
101
|
+
comment << "|#{r.desc}|#{r.passed(@issue) ? '(/)' : '(-)'}|#{r.field}|"
|
102
|
+
end
|
103
|
+
comment << docs_link
|
104
|
+
comment << ""
|
105
|
+
comment << "Posted by [lazylead v#{Lazylead::VERSION}|" \
|
106
|
+
"https://bit.ly/2NjdndS]."
|
107
|
+
comment.join("\r\n")
|
108
|
+
end
|
109
|
+
|
110
|
+
# Link to ticket formatting rules
|
111
|
+
def docs_link
|
112
|
+
if @link.nil? || @link.blank?
|
113
|
+
""
|
114
|
+
else
|
115
|
+
"The requirements/examples of ticket formatting rules you may find " \
|
116
|
+
"[here|#{@link}]."
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def color
|
121
|
+
if colors.nil? || !defined?(@score) || !@score.is_a?(Numeric)
|
122
|
+
return "#061306"
|
123
|
+
end
|
124
|
+
colors.reverse_each do |color|
|
125
|
+
return color.last if @accuracy >= color.first
|
126
|
+
end
|
127
|
+
"#061306"
|
128
|
+
end
|
129
|
+
|
130
|
+
def colors
|
131
|
+
@colors ||= begin
|
132
|
+
JSON.parse(@opts["colors"])
|
133
|
+
.to_h
|
134
|
+
.to_a
|
135
|
+
.each { |e| e[0] = e[0].to_i }
|
136
|
+
.sort_by { |e| e[0] }
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|