-
Notifications
You must be signed in to change notification settings - Fork 2
/
accuracy.rb
182 lines (166 loc) · 6.07 KB
/
accuracy.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
# frozen_string_literal: true
# The MIT License
#
# Copyright (c) 2019-2022 Yurii Dubinka
#
# 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 NON-INFRINGEMENT. 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_relative "memes"
require_relative "../../log"
require_relative "../../opts"
require_relative "../../email"
require_relative "../../version"
require_relative "../../postman"
require_relative "../../requires"
module Lazylead
module Task
#
# Evaluate ticket format and accuracy
#
# The task supports the following features:
# - fetch issues from remote ticketing system by query
# - evaluate each field within the ticket
# - post the score to the ticket
class Accuracy
def initialize(log = Log.new)
@log = log
end
def run(sys, postman, opts)
init_rules(opts)
tasks = sys.issues(opts["jql"], opts.jira_defaults.merge(expand: "changelog"))
.map { |i| Score.new(i, opts) }
.each do |s|
s.evaluate
s.post
end
unless tasks.empty? && tasks.size <= opts.fetch(:limit, 300).to_i
opts[:tickets] = tasks
postman.send(opts)
end
opts
end
# Initialize accuracy rules and configuration for tickets verification.
def init_rules(opts)
Requires.new(__dir__).load
opts[:rules] = opts.construct("rules")
opts[:total] = opts[:rules].sum(&:score)
opts[:memes] = Memes.new(opts["memes"])
end
end
end
# The ticket score based on fields content.
class Score
attr_reader :issue, :score, :accuracy
def initialize(issue, opts)
@issue = issue
@opts = opts
end
# Estimate the ticket score and accuracy.
# Accuracy is a percentage between current score and maximum possible value.
def evaluate(digits = 2)
@score = @opts[:rules].select { |r| r.passed(@issue) }.sum(&:score)
@accuracy = (@score.to_f / @opts[:total] * 100).round(digits)
self
end
# Post the comment with score and accuracy to the ticket.
def post
return if @opts.key? "silent"
@issue.post comment
@issue.add_label "LL.accuracy", "#{grade(@accuracy)}%", "#{@accuracy}%"
end
# The jira comment in markdown format
def comment
comment = [
"Hi [~#{reporter}],",
"",
"The triage accuracy is '{color:#{color}}#{@score}{color}' " \
"(~{color:#{color}}#{@accuracy}%{color}), here are the reasons why:",
"|| Ticket requirement || Status || Field ||"
]
@opts[:rules].each do |r|
comment << "|#{r.desc}|#{r.passed(@issue) ? '(/)' : '(-)'}|#{r.field}|"
end
comment << docs_link
comment << reaction
comment << "Posted by [lazylead v#{Lazylead::VERSION}|https://bit.ly/2NjdndS]."
comment.join("\r\n")
end
# Link to ticket formatting rules
def docs_link
if @opts["docs"].nil? || @opts["docs"].blank?
""
else
"The requirements/examples of ticket formatting rules you may find " \
"[here|#{@opts['docs']}]."
end
end
def color
return "#061306" if colors.nil? || !defined?(@score) || [email protected]_a?(Numeric)
colors.reverse_each do |color|
return color.last if @accuracy >= color.first
end
"#061306"
end
def colors
@colors ||= JSON.parse(@opts["colors"])
.to_h
.to_a
.each { |e| e[0] = e[0].to_i }
.sort_by { |e| e[0] }
end
# Calculate grade for accuracy
# For example,
# grade(7.5) => 0
# grade(12) => 10
# grade(25.5) => 20
def grade(value)
(value / 10).floor * 10
end
# Detect the ticket reporter.
#
# If ticket created by some automatic/admin user account then reporter is the first non-system
# user account who modified the ticket.
def reporter
sys = @opts.slice("system-users", ",")
return @issue.reporter.id if sys.empty? || sys.none? { |susr| susr.eql? @issue.reporter.id }
first = @issue.history.find { |h| sys.none? { |susr| susr.eql? h["author"]["key"] } }
return @issue.reporter.id if first.nil?
first["author"]["key"]
end
# Add reaction meme to the ticket comment based on score.
# The meme details are represented as array, where each element is a separate line in future
# comment in jira.
#
# @todo #339/DEV Seems jira doesn't support the rendering of external images by url, thus so far
# we might have several options:
# - attach meme to ticket and make rendering using [^attach.jpg!thumbnail] option
# - have a link to meme (like it implemented now)
# The 1st option with attachment might generate multiple events in jira and spam ticket
# watchers, thus, some research & UX testing needed how to make it better.
def reaction
return [] if @opts[:memes].nil? && !@opts[:memes].enabled?
url = @opts[:memes].find(@accuracy)
return [] if url.blank?
[
"",
"Our reaction when we got the ticket with triage accuracy #{@accuracy}% is [here|#{url}].",
""
]
end
end
end