Class: Lazylead::Task::Svn::Touch

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

Overview

Send notification about modification of critical files in svn repo.

Instance Method Summary collapse

Constructor Details

#initialize(log = Log.new) ⇒ Touch

Returns a new instance of Touch.



38
39
40
# File 'lib/lazylead/task/svn/touch.rb', line 38

def initialize(log = Log.new)
  @log = log
end

Instance Method Details

#run(_, postman, opts) ⇒ Object



42
43
44
45
46
# File 'lib/lazylead/task/svn/touch.rb', line 42

def run(_, postman, opts)
  files = opts.slice("files", ",")
  commits = touch(files, opts)
  postman.send(opts.merge(entries: commits)) unless commits.empty?
end

#svn_log(opts) ⇒ Object

Return all svn commits for particular date range in repo



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lazylead/task/svn/touch.rb', line 62

def svn_log(opts)
  now = if opts.key? "now"
          DateTime.parse(opts["now"])
        else
          DateTime.now
        end
  start = (now.to_time - opts["period"].to_i).to_datetime
  cmd = [
    "svn log --no-auth-cache",
    "--username #{opts.decrypt('svn_user', 'svn_salt')}",
    "--password #{opts.decrypt('svn_password', 'svn_salt')}",
    "--xml -v -r {#{start}}:{#{now}} #{opts['svn_url']}"
  ]
  raw = `#{cmd.join(" ")}`
  Nokogiri.XML(raw, nil, "UTF-8")
end

#to_entry(xml) ⇒ Object

Convert single revision(XML text) to entry object. Entry object is a simple ruby struct object.



81
82
83
84
85
86
87
88
89
# File 'lib/lazylead/task/svn/touch.rb', line 81

def to_entry(xml)
  e = to_struct(Hash.from_xml(xml.to_s.strip)).logentry
  if e.paths.path.respond_to? :each
    e.paths.path.each(&:strip!)
  else
    e.paths.path.strip!
  end
  e
end

#to_struct(hsh) ⇒ Object

Make a simple ruby struct object from hash hierarchically,

considering nested hash(es) (if applicable)


93
94
95
96
97
# File 'lib/lazylead/task/svn/touch.rb', line 93

def to_struct(hsh)
  OpenStruct.new(
    hsh.transform_values { |v| v.is_a?(Hash) ? to_struct(v) : v }
  )
end

#touch(files, opts) ⇒ Object

Return all svn commits for a particular date range, which are touching

somehow the critical files within the svn repo.


50
51
52
53
54
55
56
57
58
59
# File 'lib/lazylead/task/svn/touch.rb', line 50

def touch(files, opts)
  xpath = files.map { |f| "contains(text(),\"#{f}\")" }.join(" or ")
  svn_log(opts).xpath("//logentry[paths/path[#{xpath}]]")
               .map(&method(:to_entry))
               .each do |e|
    if e.paths.path.respond_to? :delete_if
      e.paths.path.delete_if { |p| files.none? { |f| p.include? f } }
    end
  end
end