lazylead 0.1.0 → 0.3.1

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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +19 -7
  3. data/.circleci/release_image.sh +10 -7
  4. data/.docker/Dockerfile +10 -9
  5. data/.docker/docker-compose.yml +3 -3
  6. data/.docker/readme.md +24 -21
  7. data/.docker/vcs.dockerfile +10 -0
  8. data/.docs/duedate_expired.md +92 -0
  9. data/.docs/propagate_down.md +89 -0
  10. data/.rubocop.yml +1 -1
  11. data/.rultor.yml +13 -14
  12. data/.simplecov +0 -6
  13. data/CNAME +1 -0
  14. data/Rakefile +38 -1
  15. data/bin/lazylead +7 -2
  16. data/lazylead.gemspec +5 -17
  17. data/lib/lazylead/cc.rb +180 -0
  18. data/lib/lazylead/cli/app.rb +4 -3
  19. data/lib/lazylead/exchange.rb +14 -1
  20. data/lib/lazylead/home.rb +38 -0
  21. data/lib/lazylead/model.rb +29 -7
  22. data/lib/lazylead/postman.rb +14 -14
  23. data/lib/lazylead/schedule.rb +4 -2
  24. data/lib/lazylead/system/fake.rb +1 -1
  25. data/lib/lazylead/system/jira.rb +46 -5
  26. data/lib/lazylead/task/fix_version.rb +1 -1
  27. data/lib/lazylead/task/propagate_down.rb +118 -0
  28. data/lib/lazylead/task/savepoint.rb +58 -0
  29. data/lib/lazylead/version.rb +1 -1
  30. data/lib/messages/due_date_expired.erb +8 -7
  31. data/lib/messages/illegal_fixversion_change.erb +9 -8
  32. data/lib/messages/missing_comment.erb +10 -9
  33. data/lib/messages/savepoint.erb +43 -0
  34. data/readme.md +98 -82
  35. data/test/lazylead/cc_test.rb +153 -0
  36. data/test/lazylead/cli/app_test.rb +1 -2
  37. data/test/lazylead/exchange_test.rb +20 -0
  38. data/test/lazylead/model_test.rb +11 -0
  39. data/test/lazylead/postman_test.rb +57 -0
  40. data/test/lazylead/system/jira_test.rb +8 -0
  41. data/test/lazylead/task/assignee_alert_test.rb +47 -0
  42. data/test/lazylead/task/duedate_test.rb +20 -8
  43. data/test/lazylead/task/fix_version_test.rb +2 -4
  44. data/test/lazylead/task/missing_comment_test.rb +2 -4
  45. data/test/lazylead/task/propagate_down_test.rb +86 -0
  46. data/test/lazylead/task/savepoint_test.rb +51 -0
  47. data/test/test.rb +11 -0
  48. data/upgrades/sqlite/001-install-main-lazylead-tables.sql +3 -4
  49. data/upgrades/sqlite/999.testdata.sql +7 -2
  50. metadata +38 -175
  51. data/deploy.sh +0 -16
  52. data/todo.yml +0 -16
@@ -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
@@ -22,7 +22,7 @@
22
22
  # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
23
  # OR OTHER DEALINGS IN THE SOFTWARE.
24
24
 
25
- require "json"
25
+ require "active_support"
26
26
  require "rufus-scheduler"
27
27
  require_relative "log"
28
28
  require_relative "model"
@@ -51,7 +51,9 @@ module Lazylead
51
51
  def register(task)
52
52
  raise "ll-002: task can't be a null" if task.nil?
53
53
  @trigger.cron task.cron do
54
- task.exec @log
54
+ ActiveRecord::Base.connection_pool.with_connection do
55
+ task.exec @log
56
+ end
55
57
  end
56
58
  @log.debug "Task scheduled: #{task}"
57
59
  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
@@ -193,6 +197,10 @@ module Lazylead
193
197
  def inspect
194
198
  to_s
195
199
  end
200
+
201
+ def status
202
+ @issue.status.attrs["name"]
203
+ end
196
204
  end
197
205
 
198
206
  # The jira issue comments
@@ -224,12 +232,24 @@ module Lazylead
224
232
  end
225
233
  end
226
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
+
227
247
  # Jira instance without authentication in order to access public filters
228
248
  # or dashboards.
229
249
  class NoAuthJira
230
- def initialize(url, log = Log::NOTHING)
250
+ def initialize(url, path = "", log = Log::NOTHING)
231
251
  @jira = Jira.new(
232
- { username: nil, password: nil, site: url, context_path: "" },
252
+ { username: nil, password: nil, site: url, context_path: path },
233
253
  NoSalt.new,
234
254
  log
235
255
  )
@@ -246,4 +266,25 @@ module Lazylead
246
266
  @jira.raw(&block)
247
267
  end
248
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
249
290
  end
@@ -70,7 +70,7 @@ module Lazylead
70
70
  .reverse
71
71
  .find do |h|
72
72
  h["items"].any? do |i|
73
- i["field"] == "Fix Version" && !i["to"].nil?
73
+ i["field"] == "Fix Version"
74
74
  end
75
75
  end
76
76
  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.1.0"
26
+ VERSION = "0.3.1"
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>
@@ -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 */
@@ -87,14 +88,14 @@
87
88
 
88
89
  <p>The <span style='font-weight:bold'>'Fix Version'</span> for the following
89
90
  ticket(s) changed by not authorized person(s):</p>
90
- <table>
91
+ <table summary="ticket(s) where fix version changed">
91
92
  <tr>
92
- <th>Key</th>
93
- <th>Priority</th>
94
- <th>When</th>
95
- <th>Who</th>
96
- <th>Reporter</th>
97
- <th>Summary</th>
93
+ <th id="key">Key</th>
94
+ <th id="priority">Priority</th>
95
+ <th id="when">When</th>
96
+ <th id="who">Who</th>
97
+ <th id="reporter">Reporter</th>
98
+ <th id="summary">Summary</th>
98
99
  </tr>
99
100
  <% versions.each do |v| %>
100
101
  <tr>
@@ -1,4 +1,5 @@
1
- <html>
1
+ <!DOCTYPE html>
2
+ <html lang="en">
2
3
  <head>
3
4
  <style> /* CSS styles taken from https://github.com/yegor256/tacit */
4
5
  th {
@@ -95,15 +96,15 @@
95
96
  <p>Hi <%= addressee %>,</p>
96
97
 
97
98
  <p>The following ticket(s) has missing <%= details %>:</p>
98
- <table>
99
+ <table summary="ticket(s) without expected comment">
99
100
  <tr>
100
- <th>Key</th>
101
- <th>Due date</th>
102
- <th>Priority</th>
103
- <th>Assignee</th>
104
- <th>Reporter</th>
105
- <th>Summary</th>
106
- <th>Last comments</th>
101
+ <th id="key">Key</th>
102
+ <th id="duedate">Due date</th>
103
+ <th id="priority">Priority</th>
104
+ <th id="assignee">Assignee</th>
105
+ <th id="reporter">Reporter</th>
106
+ <th id="summary">Summary</th>
107
+ <th id="comments">Last comments</th>
107
108
  </tr>
108
109
  <% comments.each do |comment| %>
109
110
  <tr>
@@ -0,0 +1,43 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <style> /* CSS styles taken from https://github.com/yegor256/tacit */
5
+ a {
6
+ color: #275a90;
7
+ text-decoration: none
8
+ }
9
+
10
+ a:hover {
11
+ text-decoration: underline
12
+ }
13
+
14
+ * {
15
+ box-sizing: border-box;
16
+ margin: 0;
17
+ max-width: 100%;
18
+ padding: 0;
19
+ vertical-align: baseline;
20
+ font-family: system-ui, \"Helvetica Neue\", Helvetica, Arial, sans-serif;
21
+ font-size: 13px;
22
+ font-stretch: normal;
23
+ font-style: normal;
24
+ font-weight: 400;
25
+ line-height: 29.7px
26
+ }
27
+
28
+ body {
29
+ background: #fff;
30
+ color: #1a1919;
31
+ padding: 36px
32
+ }
33
+ </style>
34
+ <title>LL <%= Date.today %></title>
35
+ </head>
36
+ <body>
37
+ <p>Hi,</p>
38
+ <p>PFA the LL configuration at <%= DateTime.now %>.</p>
39
+ <p>Posted by
40
+ <a href="https://github.com/dgroup/lazylead">lazylead v<%= version %></a>.
41
+ </p>
42
+ </body>
43
+ </html>