Class: Lazylead::Task::Parent

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

Overview

The parent ticket as a source for propagation to children.

Instance Method Summary collapse

Constructor Details

#initialize(issue, sys, fields) ⇒ Parent

Returns a new instance of Parent.



68
69
70
71
72
# File 'lib/lazylead/task/propagate_down.rb', line 68

def initialize(issue, sys, fields)
  @issue = issue
  @sys = sys
  @fields = fields
end

Instance Method Details

#comment(diff) ⇒ Object

Build a jira comment in markdown(*.md) format with diff table.



114
115
116
117
118
119
120
121
122
123
# File 'lib/lazylead/task/propagate_down.rb', line 114

def comment(diff)
  markdown = [
    "The following fields were propagated from #{@issue.key}:",
    "||Field||Value||"
  ]
  diff.each { |k, v| markdown << "|#{k}|#{v}|" }
  markdown << "Posted by [lazylead v#{Lazylead::VERSION}|" \
              "https://bit.ly/2NjdndS]"
  markdown.join("\r\n")
end

#diff(expected, actual) ⇒ Object

Detect difference between fields in parent ticket and sub-task. The sub-tasks expected be updated by this diff.



102
103
104
105
106
107
108
109
110
111
# File 'lib/lazylead/task/propagate_down.rb', line 102

def diff(expected, actual)
  diff = {}
  actual.each_with_object(diff) do |a, d|
    k = a.first
    v = a.last
    d[k] = expected[k] if v.nil? || v.blank?
    next if v.nil?
    d[k] = "#{v},#{expected[k]}" unless v.to_s.include? expected[k]
  end
end

#fetchObject

Take sub-tasks with their fields from external JIRA system.



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

def fetch
  @subtasks = @issue.fields["subtasks"]
                    .map do |sub|
    @sys.raw { |j| j.Issue.find(sub["id"]) }
  end
end

#propagateObject

Fill pre-defined fields for sub-tasks from parent ticket

and post comment to ticket with clarification.


89
90
91
92
93
94
95
96
97
98
# File 'lib/lazylead/task/propagate_down.rb', line 89

def propagate
  expected = @fields.to_h { |f| [f, @issue.fields[f]] }
  @subtasks.each do |subtask|
    actual = @fields.to_h { |f| [f, subtask.fields[f]] }
    diff = diff(expected, actual)
    next if diff.empty?
    subtask.save(fields: diff)
    subtask.comments.build.save!(body: comment(diff))
  end
end

#subtasks?Boolean

Ensure that parent ticket has sub-tasks.

Returns:

  • (Boolean)


75
76
77
# File 'lib/lazylead/task/propagate_down.rb', line 75

def subtasks?
  !@issue.fields["subtasks"].empty?
end