lazylead 0.1.2 → 0.4.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 (70) hide show
  1. checksums.yaml +4 -4
  2. data/.0pdd.yml +4 -1
  3. data/.circleci/config.yml +14 -4
  4. data/.docker/Dockerfile +5 -4
  5. data/.docker/vcs.dockerfile +10 -0
  6. data/.docs/accuracy.md +107 -0
  7. data/.docs/accuracy_email.jpg +0 -0
  8. data/.docs/accuracy_jira_comment.jpg +0 -0
  9. data/.docs/duedate_expired.md +92 -0
  10. data/.docs/propagate_down.md +89 -0
  11. data/.pdd +1 -1
  12. data/.rubocop.yml +7 -1
  13. data/.rultor.yml +2 -2
  14. data/.simplecov +0 -6
  15. data/bin/lazylead +13 -5
  16. data/lazylead.gemspec +4 -16
  17. data/lib/lazylead/cc.rb +180 -0
  18. data/lib/lazylead/cli/app.rb +4 -3
  19. data/lib/lazylead/exchange.rb +15 -2
  20. data/lib/lazylead/home.rb +38 -0
  21. data/lib/lazylead/log.rb +30 -8
  22. data/lib/lazylead/model.rb +60 -16
  23. data/lib/lazylead/opts.rb +68 -0
  24. data/lib/lazylead/postman.rb +15 -15
  25. data/lib/lazylead/schedule.rb +6 -4
  26. data/lib/lazylead/smtp.rb +1 -1
  27. data/lib/lazylead/system/fake.rb +1 -1
  28. data/lib/lazylead/system/jira.rb +55 -12
  29. data/lib/lazylead/system/synced.rb +2 -1
  30. data/lib/lazylead/task/accuracy/accuracy.rb +140 -0
  31. data/lib/lazylead/task/accuracy/affected_build.rb +43 -0
  32. data/lib/lazylead/task/accuracy/requirement.rb +40 -0
  33. data/lib/lazylead/task/alert.rb +8 -6
  34. data/lib/lazylead/task/confluence_ref.rb +4 -3
  35. data/lib/lazylead/task/echo.rb +4 -0
  36. data/lib/lazylead/task/fix_version.rb +10 -6
  37. data/lib/lazylead/task/missing_comment.rb +7 -5
  38. data/lib/lazylead/task/propagate_down.rb +126 -0
  39. data/lib/lazylead/task/savepoint.rb +58 -0
  40. data/lib/lazylead/task/touch.rb +102 -0
  41. data/lib/lazylead/version.rb +1 -1
  42. data/lib/messages/accuracy.erb +118 -0
  43. data/lib/messages/due_date_expired.erb +8 -7
  44. data/lib/messages/illegal_fixversion_change.erb +9 -8
  45. data/lib/messages/missing_comment.erb +10 -9
  46. data/lib/messages/savepoint.erb +43 -0
  47. data/lib/messages/svn_touch.erb +147 -0
  48. data/readme.md +37 -34
  49. data/test/lazylead/cc_test.rb +153 -0
  50. data/test/lazylead/cli/app_test.rb +3 -4
  51. data/test/lazylead/exchange_test.rb +22 -2
  52. data/test/lazylead/model_test.rb +14 -3
  53. data/test/lazylead/opts_test.rb +66 -0
  54. data/test/lazylead/postman_test.rb +57 -0
  55. data/test/lazylead/smtp_test.rb +1 -1
  56. data/test/lazylead/system/jira_test.rb +43 -1
  57. data/test/lazylead/task/accuracy/accuracy_test.rb +73 -0
  58. data/test/lazylead/task/accuracy/affected_build_test.rb +42 -0
  59. data/test/lazylead/task/assignee_alert_test.rb +47 -0
  60. data/test/lazylead/task/duedate_test.rb +52 -30
  61. data/test/lazylead/task/fix_version_test.rb +11 -10
  62. data/test/lazylead/task/missing_comment_test.rb +13 -13
  63. data/test/lazylead/task/propagate_down_test.rb +88 -0
  64. data/test/lazylead/task/savepoint_test.rb +51 -0
  65. data/test/lazylead/task/touch_test.rb +63 -0
  66. data/test/test.rb +11 -0
  67. data/upgrades/sqlite/001-install-main-lazylead-tables.sql +0 -1
  68. data/upgrades/sqlite/999.testdata.sql +8 -2
  69. metadata +43 -176
  70. data/todo.yml +0 -6
@@ -0,0 +1,43 @@
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 "../../email"
27
+ require_relative "../../version"
28
+ require_relative "../../postman"
29
+ require_relative "requirement"
30
+
31
+ module Lazylead
32
+ # A requirement that Jira field "Affects Version/s" provided by the reporter.
33
+ class RequirementAffectedBuild < Requirement
34
+ def initialize(score = 0.5)
35
+ super "Affected build", score, "Affects Version/s"
36
+ end
37
+
38
+ # @return true if an issue has non-empty "Affects Version/s" field
39
+ def passed(issue)
40
+ !issue.fields["versions"].nil? && !issue.fields["versions"].empty?
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,40 @@
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
+ module Lazylead
26
+ # An single requirement regarding ticket format.
27
+ class Requirement
28
+ attr_reader :score, :desc, :field
29
+
30
+ def initialize(desc, score, field)
31
+ @desc = desc
32
+ @score = score
33
+ @field = field
34
+ end
35
+
36
+ def passed(_)
37
+ true
38
+ end
39
+ end
40
+ end
@@ -23,6 +23,7 @@
23
23
  # OR OTHER DEALINGS IN THE SOFTWARE.
24
24
 
25
25
  require_relative "../log"
26
+ require_relative "../opts"
26
27
  require_relative "../email"
27
28
  require_relative "../version"
28
29
  require_relative "../postman"
@@ -43,12 +44,13 @@ module Lazylead
43
44
  # - prepare email based on predefined template (*.erb)
44
45
  # - send the required notifications pre-defined "addressee".
45
46
  class Alert
46
- def initialize(log = Log::NOTHING)
47
+ def initialize(log = Log.new)
47
48
  @log = log
48
49
  end
49
50
 
50
51
  def run(sys, postman, opts)
51
- postman.send opts.merge(tickets: sys.issues(opts["sql"]))
52
+ tickets = sys.issues(opts["sql"], opts.jira_defaults)
53
+ postman.send opts.merge(tickets: tickets) unless tickets.empty?
52
54
  end
53
55
  end
54
56
 
@@ -64,12 +66,12 @@ module Lazylead
64
66
  # The email message is sending to the assignee regarding all his/her issues,
65
67
  # not like one email per each issue.
66
68
  class AssigneeAlert
67
- def initialize(log = Log::NOTHING)
69
+ def initialize(log = Log.new)
68
70
  @log = log
69
71
  end
70
72
 
71
73
  def run(sys, postman, opts)
72
- sys.issues(opts["sql"])
74
+ sys.issues(opts["sql"], opts.jira_defaults)
73
75
  .group_by(&:assignee)
74
76
  .each do |a, t|
75
77
  postman.send opts.merge(to: a.email, addressee: a.name, tickets: t)
@@ -89,12 +91,12 @@ module Lazylead
89
91
  # The email message is sending to the assignee regarding all his/her issues,
90
92
  # not like one email per each issue.
91
93
  class ReporterAlert
92
- def initialize(log = Log::NOTHING)
94
+ def initialize(log = Log.new)
93
95
  @log = log
94
96
  end
95
97
 
96
98
  def run(sys, postman, opts)
97
- sys.issues(opts["sql"])
99
+ sys.issues(opts["sql"], opts.jira_defaults)
98
100
  .group_by(&:reporter)
99
101
  .each do |a, t|
100
102
  postman.send opts.merge(to: a.email, addressee: a.name, tickets: t)
@@ -26,6 +26,7 @@ require "json"
26
26
  require "faraday"
27
27
  require_relative "../system/jira"
28
28
  require_relative "../log"
29
+ require_relative "../opts"
29
30
  require_relative "../confluence"
30
31
 
31
32
  module Lazylead
@@ -35,14 +36,14 @@ module Lazylead
35
36
  # @todo #/DEV Support sub-task for link search. Potentially, the issue
36
37
  # might have sub-tasks where discussion ongoing.
37
38
  class ConfluenceRef
38
- def initialize(log = Log::NOTHING)
39
+ def initialize(log = Log.new)
39
40
  @log = log
40
41
  end
41
42
 
42
43
  def run(sys, _, opts)
43
44
  confluences = confluences(opts)
44
45
  return if confluences.empty?
45
- sys.issues(opts["jql"])
46
+ sys.issues(opts["jql"], opts.jira_defaults)
46
47
  .map { |i| Link.new(i, sys, confluences) }
47
48
  .each(&:fetch_links)
48
49
  .select(&:need_link?)
@@ -50,7 +51,7 @@ module Lazylead
50
51
  end
51
52
 
52
53
  def confluences(opts)
53
- return [] if opts["confluences"].nil? || opts["confluences"].blank?
54
+ return [] if opts.blank? "confluences"
54
55
  JSON.parse(opts["confluences"], object_class: OpenStruct)
55
56
  .map { |c| Confluence.new(c) }
56
57
  end
@@ -30,6 +30,10 @@ module Lazylead
30
30
  # Copyright:: Copyright (c) 2019-2020 Yurii Dubinka
31
31
  # License:: MIT
32
32
  class Echo
33
+ def initialize(log = Log.new)
34
+ @log = log
35
+ end
36
+
33
37
  def run(_, _, _)
34
38
  self.class.to_s
35
39
  end
@@ -23,24 +23,28 @@
23
23
  # OR OTHER DEALINGS IN THE SOFTWARE.
24
24
 
25
25
  require "date"
26
- require_relative "../system/jira"
27
26
  require_relative "../log"
27
+ require_relative "../opts"
28
+ require_relative "../system/jira"
28
29
 
29
30
  module Lazylead
30
31
  module Task
31
32
  # @todo #/DEV Each task should verify input arguments.
32
33
  # The common API should be provided for each task.
33
34
  class FixVersion
34
- def initialize(log = Log::NOTHING)
35
+ def initialize(log = Log.new)
35
36
  @log = log
36
37
  end
37
38
 
38
39
  def run(sys, postman, opts)
39
- allowed = opts["allowed"].split(",").map(&:strip).reject(&:blank?)
40
+ allowed = opts.slice("allowed", ",")
41
+ issues = sys.issues(
42
+ opts["jql"], opts.jira_defaults.merge(expand: "changelog")
43
+ )
44
+ return if issues.empty?
40
45
  postman.send opts.merge(
41
- versions: sys.issues(opts["jql"], expand: "changelog")
42
- .map { |i| Version.new(i, allowed) }
43
- .select(&:changed?)
46
+ versions: issues.map { |i| Version.new(i, allowed) }
47
+ .select(&:changed?)
44
48
  )
45
49
  end
46
50
  end
@@ -23,6 +23,7 @@
23
23
  # OR OTHER DEALINGS IN THE SOFTWARE.
24
24
 
25
25
  require_relative "../log"
26
+ require_relative "../opts"
26
27
  require_relative "../postman"
27
28
 
28
29
  module Lazylead
@@ -36,16 +37,17 @@ module Lazylead
36
37
  # nobody mentioned in comment the ftp location for recorded session.
37
38
  # Such cases needs to be reported.
38
39
  class MissingComment
39
- def initialize(log = Log::NOTHING)
40
+ def initialize(log = Log.new)
40
41
  @log = log
41
42
  end
42
43
 
43
44
  def run(sys, postman, opts)
44
- opts["details"] = "text '#{opts['text']}'" if opts["details"].blank?
45
+ opts["details"] = "text '#{opts['text']}'" if opts.blank? "details"
46
+ issues = sys.issues(opts["jql"], opts.jira_defaults)
47
+ return if issues.empty?
45
48
  postman.send opts.merge(
46
- comments: sys.issues(opts["jql"])
47
- .map { |i| Comments.new(i, sys) }
48
- .reject { |c| c.body? opts["text"] }
49
+ comments: issues.map { |i| Comments.new(i, sys) }
50
+ .reject { |c| c.body? opts["text"] }
49
51
  )
50
52
  end
51
53
  end
@@ -0,0 +1,126 @@
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 "../opts"
27
+ require_relative "../version"
28
+ require_relative "../system/jira"
29
+
30
+ module Lazylead
31
+ module Task
32
+ #
33
+ # Propagate particular JIRA fields from parent ticket to sub-tasks.
34
+ #
35
+ # The task algorithm:
36
+ # - fetch parent issues from remote ticketing system by JQL
37
+ # - reject issues without sub-tasks
38
+ # - fetch sub-tasks details
39
+ # - make a diff for pre-defined fields provided by the user
40
+ # - apply diff to sub-tasks
41
+ # - make a comment to sub-task with clarification.
42
+ class PropagateDown
43
+ def initialize(log = Log.new)
44
+ @log = log
45
+ end
46
+
47
+ # @todo #/DEV Define a new module Lazylead::Task with basic methods like
48
+ # split, groupBy(assignee, reporter, etc), blank?
49
+ def run(sys, _, opts)
50
+ fields = opts.slice("propagate", ",")
51
+ sys.issues(
52
+ opts["jql"],
53
+ {
54
+ expand: "changelog",
55
+ max_results: opts.fetch("max_results", 50),
56
+ fields: ["subtasks"] + opts.jira_fields
57
+ }
58
+ )
59
+ .map { |i| Parent.new(i, sys, fields) }
60
+ .select(&:subtasks?)
61
+ .each(&:fetch)
62
+ .each(&:propagate)
63
+ end
64
+ end
65
+
66
+ # The parent ticket as a source for propagation to children.
67
+ class Parent
68
+ def initialize(issue, sys, fields)
69
+ @issue = issue
70
+ @sys = sys
71
+ @fields = fields
72
+ end
73
+
74
+ # Ensure that parent ticket has sub-tasks.
75
+ def subtasks?
76
+ !@issue.fields["subtasks"].empty?
77
+ end
78
+
79
+ # Take sub-tasks with their fields from external JIRA system.
80
+ def fetch
81
+ @subtasks = @issue.fields["subtasks"]
82
+ .map do |sub|
83
+ @sys.raw { |j| j.Issue.find(sub["id"]) }
84
+ end
85
+ end
86
+
87
+ # Fill pre-defined fields for sub-tasks from parent ticket
88
+ # and post comment to ticket with clarification.
89
+ def propagate
90
+ expected = Hash[@fields.collect { |f| [f, @issue.fields[f]] }]
91
+ @subtasks.each do |subtask|
92
+ actual = Hash[@fields.collect { |f| [f, subtask.fields[f]] }]
93
+ diff = diff(expected, actual)
94
+ next if diff.empty?
95
+ subtask.save(fields: diff)
96
+ subtask.comments.build.save!(body: comment(diff))
97
+ end
98
+ end
99
+
100
+ # Detect difference between fields in parent ticket and sub-task.
101
+ # The sub-tasks expected be updated by this diff.
102
+ def diff(expected, actual)
103
+ diff = {}
104
+ actual.each_with_object(diff) do |a, d|
105
+ k = a.first
106
+ v = a.last
107
+ d[k] = expected[k] if v.nil? || v.blank?
108
+ next if v.nil?
109
+ d[k] = v + "," + expected[k] unless v.to_s.include? expected[k]
110
+ end
111
+ end
112
+
113
+ # Build a jira comment in markdown(*.md) format with diff table.
114
+ def comment(diff)
115
+ markdown = [
116
+ "The following fields were propagated from #{@issue.key}:",
117
+ "||Field||Value||"
118
+ ]
119
+ diff.each { |k, v| markdown << "|#{k}|#{v}|" }
120
+ markdown << "Posted by [lazylead v#{Lazylead::VERSION}|" \
121
+ "https://bit.ly/2NjdndS]"
122
+ markdown.join("\r\n")
123
+ end
124
+ end
125
+ end
126
+ 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.new)
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
@@ -0,0 +1,102 @@
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 "nokogiri"
26
+ require "active_support/core_ext/hash/conversions"
27
+ require_relative "../salt"
28
+ require_relative "../opts"
29
+
30
+ module Lazylead
31
+ module Task
32
+ #
33
+ # Send notification about modification of critical files in svn repo.
34
+ #
35
+ class SvnTouch
36
+ def initialize(log = Log.new)
37
+ @log = log
38
+ end
39
+
40
+ def run(_, postman, opts)
41
+ files = opts.slice("files", ",")
42
+ commits = touch(files, opts)
43
+ postman.send(opts.merge(entries: commits)) unless commits.empty?
44
+ end
45
+
46
+ # Return all svn commits for a particular date range, which are touching
47
+ # somehow the critical files within the svn repo.
48
+ def touch(files, opts)
49
+ xpath = files.map { |f| "contains(text(),\"#{f}\")" }.join(" or ")
50
+ svn_log(opts).xpath("//logentry[paths/path[#{xpath}]]")
51
+ .map(&method(:to_entry))
52
+ .each do |e|
53
+ if e.paths.path.respond_to? :delete_if
54
+ e.paths.path.delete_if { |p| files.none? { |f| p.include? f } }
55
+ end
56
+ end
57
+ end
58
+
59
+ # Return all svn commits for particular date range in repo
60
+ def svn_log(opts)
61
+ url = opts["svn_url"]
62
+ usr = decrypt(opts["svn_user"])
63
+ psw = decrypt(opts["svn_password"])
64
+ now = if opts.key? "now"
65
+ DateTime.parse(opts["now"])
66
+ else
67
+ DateTime.now
68
+ end
69
+ start = (now.to_time - opts["period"].to_i).to_datetime
70
+ raw = `svn log --no-auth-cache --username #{usr} --password #{psw}
71
+ --xml -v -r {#{start}}:{#{now}} #{url}`
72
+ Nokogiri.XML(raw, nil, "UTF-8")
73
+ end
74
+
75
+ # Decrypt text using cryptography salt
76
+ def decrypt(text, sid = "svn_salt")
77
+ return Salt.new(sid).decrypt(text) if ENV.key? sid
78
+ text
79
+ end
80
+
81
+ # Convert single revision(XML text) to entry object.
82
+ # Entry object is a simple ruby struct object.
83
+ def to_entry(xml)
84
+ e = to_struct(Hash.from_xml(xml.to_s.strip)).logentry
85
+ if e.paths.path.respond_to? :each
86
+ e.paths.path.each(&:strip!)
87
+ else
88
+ e.paths.path.strip!
89
+ end
90
+ e
91
+ end
92
+
93
+ # Make a simple ruby struct object from hash hierarchically,
94
+ # considering nested hash(es) (if applicable)
95
+ def to_struct(hsh)
96
+ OpenStruct.new(
97
+ hsh.transform_values { |v| v.is_a?(Hash) ? to_struct(v) : v }
98
+ )
99
+ end
100
+ end
101
+ end
102
+ end