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.



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

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.



67
68
69
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 67

def accuracy
  @accuracy
end

#issueObject (readonly)

Returns the value of attribute issue.



67
68
69
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 67

def issue
  @issue
end

#scoreObject (readonly)

Returns the value of attribute score.



67
68
69
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 67

def score
  @score
end

#totalObject (readonly)

Returns the value of attribute total.



67
68
69
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 67

def total
  @total
end

Instance Method Details

#colorObject



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

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



130
131
132
133
134
135
136
137
138
# File 'lib/lazylead/task/accuracy/accuracy.rb', line 130

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



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

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



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

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.



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

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

#postObject

Post the comment with score and accuracy to the ticket.



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

def post
  @issue.post(comment) unless @opts.key? "silent"
end