lazylead 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -61,12 +61,25 @@ module Lazylead
61
61
  body_type: "HTML",
62
62
  to_recipients: to
63
63
  }
64
- msg.update(cc_recipients: split("cc", opts)) if opts.key? "cc"
64
+ msg.update(cc_recipients: opts["cc"]) if opts.key? "cc"
65
+ add_attachments(msg, opts)
65
66
  cli.send_message msg
67
+ close_attachments msg
66
68
  @log.debug "Email was generated from #{opts} and send by #{__FILE__}. " \
67
69
  "Here is the body: #{html}"
68
70
  end
69
71
 
72
+ def add_attachments(msg, opts)
73
+ return unless opts.key? "attachments"
74
+ files = split("attachments", opts).map { |f| File.open(f, "r") }
75
+ msg[:file_attachments] = files
76
+ end
77
+
78
+ def close_attachments(msg)
79
+ return if msg[:file_attachments].nil? || msg[:file_attachments].empty?
80
+ msg[:file_attachments].each(&:close)
81
+ end
82
+
70
83
  private
71
84
 
72
85
  def cli
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require "logging"
26
+
27
+ module Lazylead
28
+ # Home directory for lazylead
29
+ #
30
+ # Author:: Yurii Dubinka (yurii.dubinka@gmail.com)
31
+ # Copyright:: Copyright (c) 2019-2020 Yurii Dubinka
32
+ # License:: MIT
33
+ class Home
34
+ def dir
35
+ ENV["LL_HOME"].nil? ? Dir.pwd : ENV["LL_HOME"]
36
+ end
37
+ end
38
+ end
@@ -26,6 +26,7 @@ require "active_record"
26
26
  require "require_all"
27
27
  require_rel "task"
28
28
  require_rel "system"
29
+ require_relative "cc"
29
30
  require_relative "log"
30
31
  require_relative "postman"
31
32
  require_relative "exchange"
@@ -64,7 +65,9 @@ module Lazylead
64
65
  opts.each_with_object({}) do |e, o|
65
66
  k = e[0]
66
67
  v = e[1]
67
- v = ENV[v.slice(2, v.length - 3)] if v.start_with? "${"
68
+ if v.respond_to? :start_with?
69
+ v = ENV[v.slice(2, v.length - 3)] if v.start_with? "${"
70
+ end
68
71
  o[k] = v
69
72
  end
70
73
  end
@@ -90,21 +93,30 @@ module Lazylead
90
93
 
91
94
  def exec(log = Log::NOTHING)
92
95
  log.debug("Task ##{id} '#{name}' is started")
93
- action.constantize
94
- .new(log)
95
- .run(system.connect(log), postman(log), props(log))
96
+ sys = system.connect(log)
97
+ pman = postman(log)
98
+ opts = props(log)
99
+ opts = detect_cc(sys) if opts.key? "cc"
100
+ action.constantize.new(log).run(sys, pman, opts)
96
101
  log.debug("Task ##{id} '#{name}' is completed")
97
102
  end
98
103
 
104
+ def detect_cc(sys)
105
+ opts = props
106
+ opts["cc"] = CC.new.detect(opts["cc"], sys)
107
+ return opts.except "cc" if opts["cc"].is_a? EmptyCC
108
+ opts
109
+ end
110
+
99
111
  def props(log = Log::NOTHING)
100
112
  @props ||= begin
101
- if team.nil?
102
- log.warn("Team for task #{id} isn't defined.")
103
- to_hash
104
- else
105
- team.to_hash.merge(to_hash)
106
- end
107
- end
113
+ if team.nil?
114
+ log.warn("Team for task #{id} isn't defined.")
115
+ env(to_hash)
116
+ else
117
+ env(team.to_hash.merge(to_hash))
118
+ end
119
+ end
108
120
  end
109
121
 
110
122
  def postman(log = Log::NOTHING)
@@ -54,25 +54,25 @@ module Lazylead
54
54
  # :opts :: the mail configuration like to, from, cc, subject, template.
55
55
  def send(opts)
56
56
  html = make_body(opts)
57
- cc = detect_cc(opts)
58
- Mail.deliver do
59
- to opts[:to] || opts["to"]
60
- from opts["from"]
61
- cc cc if opts.key? "cc"
62
- subject opts["subject"]
63
- html_part do
64
- content_type "text/html; charset=UTF-8"
65
- body html
66
- end
57
+ mail = Mail.new
58
+ mail.to opts[:to] || opts["to"]
59
+ mail.from opts["from"]
60
+ mail.cc opts["cc"] if opts.key? "cc"
61
+ mail.subject opts["subject"]
62
+ mail.html_part do
63
+ content_type "text/html; charset=UTF-8"
64
+ body html
67
65
  end
66
+ add_attachments mail, opts
67
+ mail.deliver
68
68
  @log.debug "Email was generated from #{opts} and send by #{__FILE__}. " \
69
69
  "Here is the body: #{html}"
70
70
  end
71
71
 
72
- def detect_cc(opts)
73
- cc = opts["cc"]
74
- cc = split("cc", opts) if !cc.nil? && cc.include?(",")
75
- cc
72
+ def add_attachments(mail, opts)
73
+ return unless opts.key? "attachments"
74
+ opts["attachments"].select { |a| File.file? a }
75
+ .each { |a| mail.add_file a }
76
76
  end
77
77
  end
78
78
  end
@@ -34,7 +34,7 @@ module Lazylead
34
34
  @issues = issues
35
35
  end
36
36
 
37
- def issues(_)
37
+ def issues(_, _)
38
38
  @issues
39
39
  end
40
40
  end
@@ -46,7 +46,7 @@ module Lazylead
46
46
 
47
47
  def issues(jql, opts = {})
48
48
  raw do |jira|
49
- jira.Issue.jql(jql, opts).map { |i| Lazylead::Issue.new(i) }
49
+ jira.Issue.jql(jql, opts).map { |i| Lazylead::Issue.new(i, jira) }
50
50
  end
51
51
  end
52
52
 
@@ -136,8 +136,9 @@ module Lazylead
136
136
  # Copyright:: Copyright (c) 2019-2020 Yurii Dubinka
137
137
  # License:: MIT
138
138
  class Issue
139
- def initialize(issue)
139
+ def initialize(issue, jira)
140
140
  @issue = issue
141
+ @jira = jira
141
142
  end
142
143
 
143
144
  def id
@@ -183,7 +184,10 @@ module Lazylead
183
184
  end
184
185
 
185
186
  def comments
186
- @issue.comments
187
+ return @comments if defined? @comments
188
+ @comments = @jira.Issue.find(@issue.id, expand: "comments", fields: "")
189
+ .comments
190
+ .map { |c| Comment.new(c) }
187
191
  end
188
192
 
189
193
  def to_s
@@ -228,12 +232,24 @@ module Lazylead
228
232
  end
229
233
  end
230
234
 
235
+ # Comment in jira ticket
236
+ class Comment
237
+ def initialize(comment)
238
+ @comment = comment
239
+ end
240
+
241
+ # Check that comment has expected text
242
+ def include?(text)
243
+ @comment.attrs["body"].include? text
244
+ end
245
+ end
246
+
231
247
  # Jira instance without authentication in order to access public filters
232
248
  # or dashboards.
233
249
  class NoAuthJira
234
- def initialize(url, log = Log::NOTHING)
250
+ def initialize(url, path = "", log = Log::NOTHING)
235
251
  @jira = Jira.new(
236
- { username: nil, password: nil, site: url, context_path: "" },
252
+ { username: nil, password: nil, site: url, context_path: path },
237
253
  NoSalt.new,
238
254
  log
239
255
  )
@@ -250,4 +266,25 @@ module Lazylead
250
266
  @jira.raw(&block)
251
267
  end
252
268
  end
269
+
270
+ # A fake jira system which allows to work with sub-tasks.
271
+ class Fake
272
+ def initialize(issues)
273
+ @issues = issues
274
+ end
275
+
276
+ def issues(_, _)
277
+ @issues
278
+ end
279
+
280
+ # Execute request to the ticketing system using raw client.
281
+ def raw
282
+ raise "ll-08: No block given to method" unless block_given?
283
+ yield(OpenStruct.new(Issue: self))
284
+ end
285
+
286
+ def find(id)
287
+ @issues.detect { |i| i.id.eql? id }
288
+ end
289
+ end
253
290
  end
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require_relative "../log"
26
+ require_relative "../version"
27
+ require_relative "../system/jira"
28
+
29
+ module Lazylead
30
+ module Task
31
+ #
32
+ # Propagate particular JIRA fields from parent ticket to sub-tasks.
33
+ #
34
+ # The task algorithm:
35
+ # - fetch parent issues from remote ticketing system by JQL
36
+ # - reject issues without sub-tasks
37
+ # - fetch sub-tasks details
38
+ # - make a diff for pre-defined fields provided by the user
39
+ # - apply diff to sub-tasks
40
+ # - make a comment to sub-task with clarification.
41
+ class PropagateDown
42
+ def initialize(log = Log::NOTHING)
43
+ @log = log
44
+ end
45
+
46
+ # @todo #/DEV Define a new module Lazylead::Task with basic methods like
47
+ # split, groupBy(assignee, reporter, etc), blank?
48
+ def run(sys, _, opts)
49
+ fields = opts["fields"].split(",").map(&:strip).reject(&:blank?)
50
+ sys.issues(opts["jql"], fields: ["subtasks"] + fields)
51
+ .map { |i| Parent.new(i, sys, fields) }
52
+ .select(&:subtasks?)
53
+ .each(&:fetch)
54
+ .each(&:propagate)
55
+ end
56
+ end
57
+
58
+ # The parent ticket as a source for propagation to children.
59
+ class Parent
60
+ def initialize(issue, sys, fields)
61
+ @issue = issue
62
+ @sys = sys
63
+ @fields = fields
64
+ end
65
+
66
+ # Ensure that parent ticket has sub-tasks.
67
+ def subtasks?
68
+ !@issue.fields["subtasks"].empty?
69
+ end
70
+
71
+ # Take sub-tasks with their fields from external JIRA system.
72
+ def fetch
73
+ @subtasks = @issue.fields["subtasks"]
74
+ .map do |sub|
75
+ @sys.raw { |j| j.Issue.find(sub["id"]) }
76
+ end
77
+ end
78
+
79
+ # Fill pre-defined fields for sub-tasks from parent ticket
80
+ # and post comment to ticket with clarification.
81
+ def propagate
82
+ expected = Hash[@fields.collect { |f| [f, @issue.fields[f]] }]
83
+ @subtasks.each do |subtask|
84
+ actual = Hash[@fields.collect { |f| [f, subtask.fields[f]] }]
85
+ diff = diff(expected, actual)
86
+ next if diff.empty?
87
+ subtask.save(fields: diff)
88
+ subtask.comments.build.save!(body: comment(diff))
89
+ end
90
+ end
91
+
92
+ # Detect difference between fields in parent ticket and sub-task.
93
+ # The sub-tasks expected be updated by this diff.
94
+ def diff(expected, actual)
95
+ diff = {}
96
+ actual.each_with_object(diff) do |a, d|
97
+ k = a.first
98
+ v = a.last
99
+ d[k] = expected[k] if v.nil? || v.blank?
100
+ next if v.nil?
101
+ d[k] = v + "," + expected[k] unless v.to_s.include? expected[k]
102
+ end
103
+ end
104
+
105
+ # Build a jira comment in markdown(*.md) format with diff table.
106
+ def comment(diff)
107
+ markdown = [
108
+ "The following fields were propagated from #{@issue.key}:",
109
+ "||Field||Value||"
110
+ ]
111
+ diff.each { |k, v| markdown << "|#{k}|#{v}|" }
112
+ markdown << "Posted by [lazylead v#{Lazylead::VERSION}|" \
113
+ "https://bit.ly/2NjdndS]"
114
+ markdown.join("\r\n")
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require_relative "../log"
26
+ require_relative "../home"
27
+ require_relative "../email"
28
+ require_relative "../version"
29
+ require_relative "../postman"
30
+
31
+ module Lazylead
32
+ module Task
33
+ #
34
+ # Send current configuration to admin user.
35
+ #
36
+ class Savepoint
37
+ def initialize(log = Log::NOTHING)
38
+ @log = log
39
+ end
40
+
41
+ def run(_, postman, opts)
42
+ opts["max_size"] = "25" unless opts.key? "max_size"
43
+ path = File.join(
44
+ Lazylead::Home.new.dir,
45
+ ARGV[ARGV.find_index("--sqlite") + 1]
46
+ )
47
+ return unless File.file?(path) && less_mb(path, opts["max_size"])
48
+ postman.send opts.merge("attachments" => [path])
49
+ end
50
+
51
+ private
52
+
53
+ def less_mb(path, megabytes)
54
+ (File.size(path).to_f / 2**20).round(2) < megabytes.to_i
55
+ end
56
+ end
57
+ end
58
+ end
@@ -23,5 +23,5 @@
23
23
  # OR OTHER DEALINGS IN THE SOFTWARE.
24
24
 
25
25
  module Lazylead
26
- VERSION = "0.2.0"
26
+ VERSION = "0.3.0"
27
27
  end
@@ -1,4 +1,5 @@
1
- <html>
1
+ <!DOCTYPE html>
2
+ <html lang="en">
2
3
  <head>
3
4
  <style>
4
5
  /* CSS styles taken from https://github.com/yegor256/tacit */
@@ -68,13 +69,13 @@
68
69
  them accordingly or actualize the
69
70
  <span style="font-weight:bold">"Due date"</span> field for the following
70
71
  ticket(s):</p>
71
- <table>
72
+ <table summary="table with ticket(s) which have expired due dates">
72
73
  <tr>
73
- <th>Key</th>
74
- <th>Due date</th>
75
- <th>Priority</th>
76
- <th>Reporter</th>
77
- <th>Summary</th>
74
+ <th id="key">Key</th>
75
+ <th id="duedate">Due date</th>
76
+ <th id="priority">Priority</th>
77
+ <th id="reporter">Reporter</th>
78
+ <th id="summary">Summary</th>
78
79
  </tr>
79
80
  <% tickets.each do |ticket| %>
80
81
  <tr>