Class: Lazylead::Score

Inherits:
Object
  • Object
show all
Defined in:
lib/lazylead/task/accuracy/accuracy.rb

Overview

The ticket score based on fields content.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(issue, opts) ⇒ Score

Returns a new instance of Score.



71
72
73
74
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 71

def initialize(issue, opts)
  @issue = issue
  @opts = opts
end

Instance Attribute Details

#accuracyObject (readonly)

Returns the value of attribute accuracy.



69
70
71
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 69

def accuracy
  @accuracy
end

#issueObject (readonly)

Returns the value of attribute issue.



69
70
71
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 69

def issue
  @issue
end

#scoreObject (readonly)

Returns the value of attribute score.



69
70
71
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 69

def score
  @score
end

Instance Method Details

#colorObject



119
120
121
122
123
124
125
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 119

def color
  return "#061306" if colors.nil? || !defined?(@score) || !@score.is_a?(Numeric)
  colors.reverse_each do |color|
    return color.last if @accuracy >= color.first
  end
  "#061306"
end

#colorsObject



127
128
129
130
131
132
133
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 127

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

#commentObject

The jira comment in markdown format



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 92

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



110
111
112
113
114
115
116
117
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 110

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

#evaluate(digits = 2) ⇒ Object

Estimate the ticket score and accuracy. Accuracy is a percentage between current score and maximum possible value.



78
79
80
81
82
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 78

def evaluate(digits = 2)
  @score = @opts[:rules].select { |r| r.passed(@issue) }.sum(&:score)
  @accuracy = (score / @opts[:total] * 100).round(digits)
  self
end

#grade(value) ⇒ Object

Calculate grade for accuracy For example,

grade(7.5)   => 0
grade(12)    => 10
grade(25.5)  => 20


140
141
142
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 140

def grade(value)
  (value / 10).floor * 10
end

#postObject

Post the comment with score and accuracy to the ticket.



85
86
87
88
89
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 85

def post
  return if @opts.key? "silent"
  @issue.post comment
  @issue.add_label "LL.accuracy", "#{grade(@accuracy)}%", "#{@accuracy}%"
end

#reactionObject

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.

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.


166
167
168
169
170
171
172
173
174
175
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 166

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

#reporterObject

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.


148
149
150
151
152
153
154
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 148

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