Class: Lazylead::Task::Svn::Diff

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

Overview

Send notification about modification of svn files since particular

revision.

Instance Method Summary collapse

Constructor Details

#initialize(log = Log.new) ⇒ Diff

Returns a new instance of Diff.



41
42
43
# File 'lib/lazylead/task/svn/diff.rb', line 41

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

Instance Method Details

#run(_, postman, opts) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lazylead/task/svn/diff.rb', line 45

def run(_, postman, opts)
  cmd = [
    "svn log --diff --no-auth-cache",
    "--username #{opts.decrypt('svn_user', 'svn_salt')}",
    "--password #{opts.decrypt('svn_password', 'svn_salt')}",
    "-r#{opts['since_rev']}:HEAD #{opts['svn_url']}"
  ]
  stdout = `#{cmd.join(" ")}`
  stdout.scrub!
  send_email postman, opts.merge(stdout: stdout) unless stdout.blank?
end

#send_email(postman, opts) ⇒ Object

Send email with svn log as an attachment. The attachment won’t be stored locally and we’ll be removed once

mail sent.


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

def send_email(postman, opts)
  Dir.mktmpdir do |dir|
    name = "svn-log-#{Date.today.strftime('%d-%b-%Y')}.html"
    begin
      postman.send opts.merge(attachments: [to_f(File.join(dir, name), opts)])
    ensure
      FileUtils.rm_rf("#{dir}/*")
    end
  rescue StandardError => e
    @log.error "ll-010: Can't send an email '#{opts['subject']}' to #{opts['to']} due to " \
               "#{Backtrace.new(e)}'"
  end
end

#to_f(path, opts) ⇒ Object

Wrap attachment content to a *.zip file and archive.

to_f('my-content.html', opts)   => my-content.html.zip

You may disable archiving option by passing option no_archive

to_f('my-content.html', "no_archive" => true)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lazylead/task/svn/diff.rb', line 79

def to_f(path, opts)
  if opts.key? "no_archive"
    f = File.open(path, "w")
    body = opts.msg_body("template-attachment")
  else
    f = File.new("#{path}.zip", "wb")
    bytes = Zip::OutputStream.write_buffer do |zio|
      zio.put_next_entry(File.basename(path))
      zio.write opts.msg_body("template-attachment")
    end
    bytes.rewind # reposition buffer pointer to the beginning
    body = bytes.sysread
  end
  f.write body
  f.close
  f.path
end