Skip to content

Commit

Permalink
#243: svn_log task - use log as attachment
Browse files Browse the repository at this point in the history
Fixed #243
  • Loading branch information
dgroup committed Nov 8, 2020
1 parent 418ff30 commit 76f38e7
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 19 deletions.
1 change: 1 addition & 0 deletions lazylead.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ tasks instead of solving technical problems."
s.add_runtime_dependency "slop", "4.4"
s.add_runtime_dependency "sqlite3", "1.4.2"
s.add_runtime_dependency "tilt", "2.0.10"
s.add_runtime_dependency "tmpdir", "0.1.0"
s.add_runtime_dependency "tzinfo", "1.1"
s.add_runtime_dependency "tzinfo-data", "1.2019.3"
s.add_runtime_dependency "vcs4sql", "0.1.0"
Expand Down
7 changes: 4 additions & 3 deletions lib/lazylead/postman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ def make_email(opts)
end

def add_attachments(mail, opts)
return unless opts.key? "attachments"
opts["attachments"].select { |a| File.file? a }
.each { |a| mail.add_file a }
return unless opts.key?("attachments") || opts.key?(:attachments)
attach = opts["attachments"] || opts[:attachments]
return if attach.nil?
attach.select { |a| File.file? a }.each { |a| mail.add_file a }
end
end
end
2 changes: 1 addition & 1 deletion lib/lazylead/task/accuracy/stacktrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initialize(score = 3)

def passed(issue)
return false if issue.description.nil?
!frames(issue.description).select { |f| oracle?(f) || java?(f) }.empty?
frames(issue.description).any? { |f| oracle?(f) || java?(f) }
end

# Detect all {noformat}, {code} frames in ticket description
Expand Down
22 changes: 21 additions & 1 deletion lib/lazylead/task/touch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

require "tmpdir"
require "nokogiri"
require "active_support/core_ext/hash/conversions"
require_relative "../salt"
Expand Down Expand Up @@ -112,7 +113,26 @@ def run(_, postman, opts)
"-r#{opts['since_rev']}:HEAD #{opts['svn_url']}"
]
stdout = `#{cmd.join(" ")}`
postman.send(opts.merge(stdout: stdout)) unless stdout.blank?
send_email stdout, postman, opts unless stdout.blank?
end

# Send email with svn log as an attachment
# The attachment won't be stored locally and we'll be removed once it sent
def send_email(stdout, postman, opts)
Dir.mktmpdir do |dir|
name = "svn-log-#{Date.today.strftime('%d-%b-%Y')}.html"
f = File.open(File.join(dir, name), "w")
f.write(
Email.new(
opts["template-attachment"],
opts.merge(stdout: stdout, version: Lazylead::VERSION)
).render
)
postman.send opts.merge(stdout: stdout, attachments: [f.path])
rescue StandardError => e
@log.error "ll-010: Can't send an email for #{opts} based on "\
"'#{stdout}'", e
end
end
end
end
Expand Down
19 changes: 6 additions & 13 deletions lib/messages/svn_log.erb
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,24 @@
</head>
<body>
<p>Hi,</p>
<p>The following file(s) changed since <code><%= since_rev %></code> rev in <a href="<%= svn_url %>"><%= svn_url %></a>:</p>
<p>The following file(s) changed since <code><%= since_rev %></code> rev in
<a href="<%= svn_url %>"><%= svn_url %></a>:</p>
<% stdout.split("------------------------------------------------------------------------").reject(&:blank?).reverse.each do |commit| %>
<div class="commit">
<% commit.split("\n").reject(&:blank?).each_with_index do |line, index| %>
<% if index.zero? %>
<% details = line.split("|").map(&:strip).reject(&:blank?) %>
<p style="background: gainsboro;">
<a href="<%= commit_url %><%= details[0][1 .. -1] %>"><%= details[0] %></a>
<a href="<%= commit_url %><%= details[0][1..-1] %>"><%= details[0] %></a>
by <a href="<%= user %><%= details[1] %>"><%= details[1] %></a>
at <span style="color: #275a90;"><%= details[2] %></span>
</p>
<% else %>
<% if line.start_with?("+++") || line.start_with?("---") %>
<p style="background: gainsboro;"><%= line.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;') %></p>
<% elsif line.start_with?("+") %>
<p style="<%= "background: darkseagreen;" %>"><%= line.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;') %></p>
<% elsif line.start_with?("-") %>
<p style="<%= "background: lightsalmon;" %>"><%= line.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;') %></p>
<% else %>
<p style="background: gainsboro;"><%= line.gsub(/[<>]/,'<' => '&lt;', '>' => '&gt;') %></p>
<% end %>
<% end %>
<% if index == 1 %>
<p style="background: gainsboro;"><%= line %></p>
<% end %>
<% end %>
</div>
<br/><br/><br/>
<% end %>
<p>Posted by
<a href="https://github.com/dgroup/lazylead">lazylead v<%= version %></a>.
Expand Down
117 changes: 117 additions & 0 deletions lib/messages/svn_log_attachment.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<!DOCTYPE html>
<html lang="en">
<head>
<style>
/* CSS styles taken from https://github.com/yegor256/tacit */
pre, code, kbd, samp, var, output {
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
font-size: 14.4px
}

pre code {
background: none;
border: 0;
line-height: 29.7px;
padding: 0
}

code, kbd {
background: #daf1e0;
border-radius: 3.6px;
color: #2a6f3b;
display: inline-block;
line-height: 18px;
padding: 3.6px 6.3px 2.7px
}

a {
color: #275a90;
text-decoration: none
}

a:hover {
text-decoration: underline
}

* {
border: 0;
border-collapse: separate;
border-spacing: 0;
box-sizing: border-box;
margin: 0;
max-width: 100%;
padding: 0;
vertical-align: baseline;
font-family: system-ui, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 13px;
font-stretch: normal;
font-style: normal;
font-weight: 400;
line-height: 29.7px
}

html, body {
width: 100%
}

html {
height: 100%
}

body {
background: #fff;
color: #1a1919;
padding: 36px
}

.commit {
min-width: 100%;
border-radius: 3.5px;
overflow: hidden;
display: inline-block;
line-height: 15px;
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
border: 1px solid #BCC6CC;
}

.commit * {
padding-left: 4px;
font-size: 8px;
line-height: 15px;
}
</style>
<title>SVN log</title>
</head>
<body>
<p>Hi,</p>
<p>The following file(s) changed since <code><%= since_rev %></code> rev in <a href="<%= svn_url %>"><%= svn_url %></a>:</p>
<% stdout.split("------------------------------------------------------------------------").reject(&:blank?).reverse.each do |commit| %>
<div class="commit">
<% commit.split("\n").reject(&:blank?).each_with_index do |line, index| %>
<% if index.zero? %>
<% details = line.split("|").map(&:strip).reject(&:blank?) %>
<p style="background: gainsboro;">
<a href="<%= commit_url %><%= details[0][1 .. -1] %>"><%= details[0] %></a>
by <a href="<%= user %><%= details[1] %>"><%= details[1] %></a>
at <span style="color: #275a90;"><%= details[2] %></span>
</p>
<% else %>
<% if line.start_with?("+++") || line.start_with?("---") %>
<p style="background: gainsboro;"><%= line.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;') %></p>
<% elsif line.start_with?("+") %>
<p style="<%= "background: darkseagreen;" %>"><%= line.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;') %></p>
<% elsif line.start_with?("-") %>
<p style="<%= "background: lightsalmon;" %>"><%= line.gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;') %></p>
<% else %>
<p style="background: gainsboro;"><%= line.gsub(/[<>]/,'<' => '&lt;', '>' => '&gt;') %></p>
<% end %>
<% end %>
<% end %>
</div>
<br/><br/><br/>
<% end %>
<p>Posted by
<a href="https://github.com/dgroup/lazylead">lazylead v<%= version %></a>.
</p>
</body>
</html>
40 changes: 39 additions & 1 deletion test/lazylead/task/touch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,49 @@ class SvnLogTest < Lazylead::Test
"to" => "lead@fake.com",
"since_rev" => "1",
"subject" => "[SVN] Changed since rev1",
"template" => "lib/messages/svn_log.erb"
"template" => "lib/messages/svn_log.erb",
"template-attachment" => "lib/messages/svn_log_attachment.erb"
)
)
assert_email_line "[SVN] Changed since rev1",
%w[r2 by dgroup at 2020-08-16]
end

test "changes since revision with attachment" do
skip "No svn credentials provided" unless env? "svn_log_user",
"svn_log_password"
skip "No internet connection to riouxsvn.com" unless ping? "riouxsvn.com"
skip "No postman credentials provided" unless env? "LL_SMTP_HOST",
"LL_SMTP_PORT",
"LL_SMTP_USER",
"LL_SMTP_PASS",
"LL_SMTP_TO",
"LL_SMTP_FROM"
Lazylead::Smtp.new(
Log.new,
NoSalt.new,
smtp_host: ENV["LL_SMTP_HOST"],
smtp_port: ENV["LL_SMTP_PORT"],
smtp_user: ENV["LL_SMTP_USER"],
smtp_pass: ENV["LL_SMTP_PASS"]
).enable
Task::SvnLog.new.run(
[],
Postman.new,
Opts.new(
"from" => ENV["LL_SMTP_FROM"],
"svn_url" => "https://svn.riouxsvn.com/touch4ll",
"svn_user" => ENV["svn_log_user"],
"svn_password" => ENV["svn_log_password"],
"commit_url" => "https://view.commit.com?rev=",
"user" => "https://user.com?id=",
"to" => ENV["LL_SMTP_TO"],
"since_rev" => "1",
"subject" => "[SVN] Changed since rev1",
"template" => "lib/messages/svn_log.erb",
"template-attachment" => "lib/messages/svn_log_attachment.erb"
)
)
end
end
end

0 comments on commit 76f38e7

Please sign in to comment.