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, rules) ⇒ Score

Returns a new instance of Score.



63
64
65
66
67
68
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 63

def initialize(issue, opts, rules)
  @issue = issue
  @link = opts["docs"]
  @opts = opts
  @rules = rules
end

Instance Attribute Details

#accuracyObject (readonly)

Returns the value of attribute accuracy.



61
62
63
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 61

def accuracy
  @accuracy
end

#issueObject (readonly)

Returns the value of attribute issue.



61
62
63
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 61

def issue
  @issue
end

#scoreObject (readonly)

Returns the value of attribute score.



61
62
63
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 61

def score
  @score
end

#totalObject (readonly)

Returns the value of attribute total.



61
62
63
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 61

def total
  @total
end

Instance Method Details

#colorObject



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

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

#colorsObject



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

def colors
  @colors ||= begin
                JSON.parse(@opts["colors"])
                    .to_h
                    .to_a
                    .each { |e| e[0] = e[0].to_i }
                    .sort_by { |e| e[0] }
              end
end

#commentObject

The jira comment in markdown format



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 88

def comment
  comment = [
    "Hi [~#{@issue.reporter.id}],",
    "",
    "The triage accuracy is '{color:#{color}}#{@score}{color}'" \
      " (~{color:#{color}}#{@accuracy}%{color}), here are the reasons why:",
    "|| Ticket requirement || Status || Field ||"
  ]
  @rules.each do |r|
    comment << "|#{r.desc}|#{r.passed(@issue) ? '(/)' : '(-)'}|#{r.field}|"
  end
  comment << docs_link
  comment << ""
  comment << "Posted by [lazylead v#{Lazylead::VERSION}|" \
                "https://bit.ly/2NjdndS]."
  comment.join("\r\n")
end

Link to ticket formatting rules



107
108
109
110
111
112
113
114
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 107

def docs_link
  if @link.nil? || @link.blank?
    ""
  else
    "The requirements/examples of ticket formatting rules you may find " \
      "[here|#{@link}]."
  end
end

#evaluate(digits = 2) ⇒ Object

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



72
73
74
75
76
77
78
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 72

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

#grade(value) ⇒ Object

Calculate grade for accuracy For example,

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


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

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

#postObject

Post the comment with score and accuracy to the ticket.



81
82
83
84
85
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 81

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