Class: Lazylead::Task::SvnTouch

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

Overview

Send notification about modification of critical files in svn repo.

Instance Method Summary collapse

Constructor Details

#initialize(log = Log.new) ⇒ SvnTouch

Returns a new instance of SvnTouch.



36
37
38
# File 'lib/lazylead/task/touch.rb', line 36

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

Instance Method Details

#decrypt(text, sid = "svn_salt") ⇒ Object

Decrypt text using cryptography salt



78
79
80
81
# File 'lib/lazylead/task/touch.rb', line 78

def decrypt(text, sid = "svn_salt")
  return Salt.new(sid).decrypt(text) if ENV.key? sid
  text
end

#run(_, postman, opts) ⇒ Object



40
41
42
43
44
# File 'lib/lazylead/task/touch.rb', line 40

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



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

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 #{decrypt(opts['svn_user'])}",
    "--password #{decrypt(opts['svn_password'])}",
    "--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.



85
86
87
88
89
90
91
92
93
# File 'lib/lazylead/task/touch.rb', line 85

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)


97
98
99
100
101
# File 'lib/lazylead/task/touch.rb', line 97

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.


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

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