lazylead 0.1.1 → 0.4.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.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.0pdd.yml +4 -1
  3. data/.circleci/config.yml +19 -7
  4. data/.circleci/release_image.sh +6 -3
  5. data/.docker/Dockerfile +10 -9
  6. data/.docker/docker-compose.yml +3 -3
  7. data/.docker/readme.md +24 -21
  8. data/.docker/vcs.dockerfile +10 -0
  9. data/.docs/accuracy.md +107 -0
  10. data/.docs/accuracy_email.jpg +0 -0
  11. data/.docs/accuracy_jira_comment.jpg +0 -0
  12. data/.docs/duedate_expired.md +92 -0
  13. data/.docs/propagate_down.md +89 -0
  14. data/.pdd +1 -1
  15. data/.rubocop.yml +7 -1
  16. data/.rultor.yml +13 -14
  17. data/.simplecov +0 -6
  18. data/Rakefile +38 -1
  19. data/bin/lazylead +13 -5
  20. data/lazylead.gemspec +6 -17
  21. data/lib/lazylead/cc.rb +180 -0
  22. data/lib/lazylead/cli/app.rb +4 -3
  23. data/lib/lazylead/exchange.rb +15 -2
  24. data/lib/lazylead/home.rb +38 -0
  25. data/lib/lazylead/log.rb +30 -8
  26. data/lib/lazylead/model.rb +60 -16
  27. data/lib/lazylead/opts.rb +68 -0
  28. data/lib/lazylead/postman.rb +15 -15
  29. data/lib/lazylead/schedule.rb +6 -4
  30. data/lib/lazylead/smtp.rb +1 -1
  31. data/lib/lazylead/system/fake.rb +1 -1
  32. data/lib/lazylead/system/jira.rb +55 -12
  33. data/lib/lazylead/system/synced.rb +2 -1
  34. data/lib/lazylead/task/accuracy/accuracy.rb +140 -0
  35. data/lib/lazylead/task/accuracy/affected_build.rb +43 -0
  36. data/lib/lazylead/task/accuracy/requirement.rb +40 -0
  37. data/lib/lazylead/task/alert.rb +8 -6
  38. data/lib/lazylead/task/confluence_ref.rb +4 -3
  39. data/lib/lazylead/task/echo.rb +4 -0
  40. data/lib/lazylead/task/fix_version.rb +11 -7
  41. data/lib/lazylead/task/missing_comment.rb +7 -5
  42. data/lib/lazylead/task/propagate_down.rb +126 -0
  43. data/lib/lazylead/task/savepoint.rb +58 -0
  44. data/lib/lazylead/task/touch.rb +102 -0
  45. data/lib/lazylead/version.rb +1 -1
  46. data/lib/messages/accuracy.erb +118 -0
  47. data/lib/messages/due_date_expired.erb +8 -7
  48. data/lib/messages/illegal_fixversion_change.erb +9 -8
  49. data/lib/messages/missing_comment.erb +10 -9
  50. data/lib/messages/savepoint.erb +43 -0
  51. data/lib/messages/svn_touch.erb +147 -0
  52. data/readme.md +90 -80
  53. data/test/lazylead/cc_test.rb +153 -0
  54. data/test/lazylead/cli/app_test.rb +3 -4
  55. data/test/lazylead/exchange_test.rb +22 -2
  56. data/test/lazylead/model_test.rb +14 -3
  57. data/test/lazylead/opts_test.rb +66 -0
  58. data/test/lazylead/postman_test.rb +57 -0
  59. data/test/lazylead/smtp_test.rb +1 -1
  60. data/test/lazylead/system/jira_test.rb +43 -1
  61. data/test/lazylead/task/accuracy/accuracy_test.rb +73 -0
  62. data/test/lazylead/task/accuracy/affected_build_test.rb +42 -0
  63. data/test/lazylead/task/assignee_alert_test.rb +47 -0
  64. data/test/lazylead/task/duedate_test.rb +52 -30
  65. data/test/lazylead/task/fix_version_test.rb +11 -10
  66. data/test/lazylead/task/missing_comment_test.rb +13 -13
  67. data/test/lazylead/task/propagate_down_test.rb +88 -0
  68. data/test/lazylead/task/savepoint_test.rb +51 -0
  69. data/test/lazylead/task/touch_test.rb +63 -0
  70. data/test/test.rb +11 -0
  71. data/upgrades/sqlite/001-install-main-lazylead-tables.sql +3 -4
  72. data/upgrades/sqlite/999.testdata.sql +8 -2
  73. metadata +57 -177
  74. data/deploy.sh +0 -16
  75. data/todo.yml +0 -6
@@ -48,7 +48,7 @@ module Lazylead
48
48
 
49
49
  def run(opts)
50
50
  apply_vcs_migration opts
51
- enable_active_record
51
+ enable_active_record opts
52
52
  @smtp.enable
53
53
  schedule_tasks
54
54
  end
@@ -63,10 +63,11 @@ module Lazylead
63
63
  @log.debug "Migration applied to #{@db} from #{vcs}"
64
64
  end
65
65
 
66
- def enable_active_record
66
+ def enable_active_record(opts)
67
67
  ActiveRecord::Base.establish_connection(
68
68
  adapter: "sqlite3",
69
- database: @db
69
+ database: @db,
70
+ pool: opts[:max_connections]
70
71
  )
71
72
  @log.debug "Database connection established"
72
73
  end
@@ -42,7 +42,7 @@ module Lazylead
42
42
  include Emailing
43
43
 
44
44
  def initialize(
45
- log = Log::NOTHING, salt = Salt.new("exchange_salt"), opts = ENV.to_h
45
+ log = Log.new, salt = Salt.new("exchange_salt"), opts = ENV.to_h
46
46
  )
47
47
  @log = log
48
48
  @salt = salt
@@ -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
@@ -23,16 +23,38 @@
23
23
  # OR OTHER DEALINGS IN THE SOFTWARE.
24
24
 
25
25
  require "logging"
26
+ require "forwardable"
26
27
 
27
28
  module Lazylead
28
- # Loggers.
29
+ # The main application logger
30
+ class Log
31
+ extend Forwardable
32
+ def_delegators :@log, :debug, :info, :warn, :error
33
+
34
+ def initialize(log = Lazylead::Level::ERRORS)
35
+ @log = log
36
+ @log = Lazylead::Level::DEBUG if ARGV.include? "--trace"
37
+ end
38
+
39
+ def nothing
40
+ @log = Lazylead::Level::NOTHING
41
+ self
42
+ end
43
+
44
+ def verbose
45
+ @log = Lazylead::Level::DEBUG
46
+ self
47
+ end
48
+ end
49
+
50
+ # Predefined logging levels.
29
51
  #
30
52
  # There are 3 colored loggers so far:
31
53
  # NOTHING - for cases when logging isn't required
32
54
  # VERBOSE - all logging levels including debug
33
55
  # ERRORS - for errors only which are critical for app.
34
56
  #
35
- module Log
57
+ module Level
36
58
  # Coloring configuration for appender(s).
37
59
  Logging.color_scheme("bright",
38
60
  levels: {
@@ -46,7 +68,7 @@ module Lazylead
46
68
  Logging.appenders.stdout(
47
69
  "stdout",
48
70
  layout: Logging.layouts.pattern(
49
- pattern: "[%d] %-5l %m\n",
71
+ pattern: "[%d] %-5l [%X{tid}] %m\n",
50
72
  color_scheme: "bright"
51
73
  )
52
74
  )
@@ -57,12 +79,12 @@ module Lazylead
57
79
  NOTHING.freeze
58
80
 
59
81
  # All levels including debug
60
- VERBOSE = Logging.logger["verbose"]
61
- VERBOSE.level = :debug
62
- VERBOSE.add_appenders "stdout"
63
- VERBOSE.freeze
82
+ DEBUG = Logging.logger["debug"]
83
+ DEBUG.level = :debug
84
+ DEBUG.add_appenders "stdout"
85
+ DEBUG.freeze
64
86
 
65
- # Alerts
87
+ # Alerts/errors
66
88
  ERRORS = Logging.logger["errors"]
67
89
  ERRORS.level = :error
68
90
  ERRORS.add_appenders "stdout"
@@ -22,11 +22,15 @@
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 "active_record"
25
+ require "backtrace"
26
26
  require "require_all"
27
+ require "forwardable"
28
+ require "active_record"
27
29
  require_rel "task"
28
30
  require_rel "system"
31
+ require_relative "cc"
29
32
  require_relative "log"
33
+ require_relative "opts"
30
34
  require_relative "postman"
31
35
  require_relative "exchange"
32
36
 
@@ -64,7 +68,9 @@ module Lazylead
64
68
  opts.each_with_object({}) do |e, o|
65
69
  k = e[0]
66
70
  v = e[1]
67
- v = ENV[v.slice(2, v.length - 3)] if v.start_with? "${"
71
+ if v.respond_to? :start_with?
72
+ v = ENV[v.slice(2, v.length - 3)] if v.start_with? "${"
73
+ end
68
74
  o[k] = v
69
75
  end
70
76
  end
@@ -88,35 +94,74 @@ module Lazylead
88
94
  belongs_to :team, foreign_key: "team_id"
89
95
  belongs_to :system, foreign_key: "system"
90
96
 
91
- def exec(log = Log::NOTHING)
92
- log.debug("Task ##{id} '#{name}' is started")
93
- action.constantize
94
- .new(log)
95
- .run(system.connect(log), postman(log), props)
96
- log.debug("Task ##{id} '#{name}' is completed")
97
+ def exec
98
+ sys = system.connect
99
+ opts = props
100
+ opts = detect_cc(sys) if opts.key? "cc"
101
+ action.constantize.new(log).run(sys, postman, opts)
102
+ end
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
97
109
  end
98
110
 
99
111
  def props
100
- return @prop if defined? @prop
101
- @prop = team.to_hash.merge(to_hash)
112
+ @props ||= begin
113
+ if team.nil?
114
+ Opts.new(env(to_hash))
115
+ else
116
+ Opts.new(env(team.to_hash.merge(to_hash)))
117
+ end
118
+ end
102
119
  end
103
120
 
104
- def postman(log = Log::NOTHING)
121
+ def postman
105
122
  if props.key? "postman"
106
- props["postman"].constantize.new(log)
123
+ props["postman"].constantize.new
107
124
  else
108
- log.warn "No postman details provided, an local stub is used."
109
125
  Postman.new
110
126
  end
111
127
  end
112
128
  end
113
129
 
130
+ # A task with extended logging
131
+ # @see Lazylead::ORM::Task
132
+ class VerboseTask
133
+ extend Forwardable
134
+ def_delegators :@orig, :id, :name, :team, :to_s, :inspect, :props
135
+
136
+ def initialize(orig, log = Log.new)
137
+ @orig = orig
138
+ @log = log
139
+ end
140
+
141
+ def exec
142
+ Logging.mdc["tid"] = "task #{id}"
143
+ @log.debug "'#{name}' is started."
144
+ @log.warn "No postman, stub is used." unless props.key? "postman"
145
+ @log.warn "No team." if team.nil?
146
+ @orig.exec @log
147
+ @log.debug "'#{name}' is completed"
148
+ rescue StandardError => e
149
+ msg = <<~MSG
150
+ ll-006: Task ##{id} #{e} (#{e.class}) at #{self}
151
+ #{Backtrace.new(e) if ARGV.include? '--trace'}"
152
+ MSG
153
+ @log.error msg
154
+ ensure
155
+ Logging.mdc["tid"] = ""
156
+ end
157
+ end
158
+
114
159
  # Ticketing systems to monitor.
115
160
  class System < ActiveRecord::Base
116
161
  include ORM
117
162
 
118
163
  # Make an instance of ticketing system for future interaction.
119
- def connect(log = Log::NOTHING)
164
+ def connect(log = Log.new)
120
165
  opts = to_hash
121
166
  if opts["type"].empty?
122
167
  log.warn "No task system details provided, an empty stub is used."
@@ -124,8 +169,7 @@ module Lazylead
124
169
  else
125
170
  opts["type"].constantize.new(
126
171
  env(opts.except("type", "salt")),
127
- opts["salt"].blank? ? NoSalt.new : Salt.new(opts["salt"]),
128
- log
172
+ opts["salt"].blank? ? NoSalt.new : Salt.new(opts["salt"])
129
173
  )
130
174
  end
131
175
  end
@@ -0,0 +1,68 @@
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 "forwardable"
26
+
27
+ module Lazylead
28
+ #
29
+ # Default options for all lazylead tasks.
30
+ #
31
+ # Author:: Yurii Dubinka (yurii.dubinka@gmail.com)
32
+ # Copyright:: Copyright (c) 2019-2020 Yurii Dubinka
33
+ # License:: MIT
34
+ class Opts
35
+ extend Forwardable
36
+ def_delegators :@origin, :[], :[]=, :to_s, :key?, :fetch, :merge
37
+
38
+ def initialize(origin = {})
39
+ @origin = origin
40
+ end
41
+
42
+ # Split text value by delimiter, trim all spaces and reject blank items
43
+ def slice(key, delim)
44
+ to_h[key].split(delim).map(&:chomp).map(&:strip).reject(&:blank?)
45
+ end
46
+
47
+ def blank?(key)
48
+ to_h[key].nil? || @origin[key].blank?
49
+ end
50
+
51
+ def to_h
52
+ @origin
53
+ end
54
+
55
+ # Default Jira options to use during search for all Jira-based tasks.
56
+ def jira_defaults
57
+ {
58
+ max_results: fetch("max_results", 50),
59
+ fields: jira_fields
60
+ }
61
+ end
62
+
63
+ # Default fields which to fetch within the Jira issue
64
+ def jira_fields
65
+ to_h.fetch("fields", "").split(",").map(&:to_sym)
66
+ end
67
+ end
68
+ end
@@ -46,7 +46,7 @@ module Lazylead
46
46
  class Postman
47
47
  include Emailing
48
48
 
49
- def initialize(log = Log::NOTHING)
49
+ def initialize(log = Log.new)
50
50
  @log = log
51
51
  end
52
52
 
@@ -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"
@@ -40,7 +40,7 @@ module Lazylead
40
40
  # schedule some task once or at particular time period like in next 200ms).
41
41
  # For cron expressions we should define separate test suite which will test
42
42
  # in parallel without blocking main CI process.
43
- def initialize(log = Log::NOTHING, cling = true)
43
+ def initialize(log = Log.new, cling = true)
44
44
  @log = log
45
45
  @cling = cling
46
46
  @trigger = Rufus::Scheduler.new
@@ -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
+ ORM::VerboseTask.new(task, @log).exec
56
+ end
55
57
  end
56
58
  @log.debug "Task scheduled: #{task}"
57
59
  end
@@ -73,7 +75,7 @@ module Lazylead
73
75
 
74
76
  # Fake application schedule for unit testing purposes
75
77
  class NoSchedule
76
- def initialize(log = Log::NOTHING)
78
+ def initialize(log = Log.new)
77
79
  @log = log
78
80
  end
79
81
 
@@ -34,7 +34,7 @@ module Lazylead
34
34
  # Copyright:: Copyright (c) 2019-2020 Yurii Dubinka
35
35
  # License:: MIT
36
36
  class Smtp
37
- def initialize(log = Log::NOTHING, salt = NoSalt.new, opts = {})
37
+ def initialize(log = Log.new, salt = NoSalt.new, opts = {})
38
38
  @log = log
39
39
  @salt = salt
40
40
  @opts = opts