Skip to content

Instantly share code, notes, and snippets.

@nolith
Last active November 8, 2016 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolith/ca4b80c829ac254eb3e7a587b23c62ca to your computer and use it in GitHub Desktop.
Save nolith/ca4b80c829ac254eb3e7a587b23c62ca to your computer and use it in GitHub Desktop.
Parse a changelog and send it via mandrill
source 'https://rubygems.org'
gemspec
PATH
remote: .
specs:
mandrillamelo (0.1.0)
mandrill-api (~> 1.0)
vandamme (~> 0.0.11)
GEM
remote: https://rubygems.org/
specs:
excon (0.54.0)
github-markup (1.4.0)
json (1.8.3)
mandrill-api (1.0.53)
excon (>= 0.16.0, < 1.0)
json (>= 1.7.7, < 2.0)
redcarpet (3.3.4)
vandamme (0.0.11)
github-markup (~> 1.3)
redcarpet (~> 3.3.2)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1.13)
mandrillamelo!
BUNDLED WITH
1.13.5
#!/usr/bin/env ruby
require 'vandamme'
require 'mandrill'
changelog = open('./CHANGELOG.md').read.force_encoding(Encoding::UTF_8)
parser = Vandamme::Parser.new(changelog: changelog, version_header_exp: '(\d\.\d+\.\d+) \(\d{4}-\d{2}-\d{2}\)', format: 'markdown')
last_release = parser.to_html.keys[0]
def send_mail(app_name, release, html_changelog, to, from, api_key, headers = {})
begin
mandrill = Mandrill::API.new(api_key)
message = {
"tags"=>["changelog"],
"to"=> to,
"track_opens"=>nil,
"headers"=>headers,
"view_content_link"=>nil,
"auto_text"=>nil,
"track_clicks"=>nil,
"from_name"=>"Changelog",
"subject"=>"[CHANGELOG] #{app_name} #{release}",
"html"=>"<h1>#{app_name} Changelog</h1><h2>#{release}</h2>#{html_changelog}",
"from_email"=>from,
"preserve_recipients"=>true,
"auto_html"=>nil,
"important"=>false}
async = false
result = mandrill.messages.send(message, async)
rescue Mandrill::Error => e
# Mandrill errors are thrown as exceptions
puts "A mandrill error occurred: #{e.class} - #{e.message}"
# A mandrill error occurred: Mandrill::UnknownSubaccountError - No subaccount exists with the id 'customer-123'
raise
end
end
to = ENV['RECIPIENTS'].split(',').map{ |mail| {'email'=>mail} }
api_key = ENV['MANDRILL_KEY']
headers = {"Reply-To"=>ENV['CHANGELOG_REPLY_TO']}
from = ENV['CHANGELOG_FROM']
app_name = ENV['CHANGELOG_APP_NAME']
puts send_mail(app_name, last_release, parser.to_html[last_release],
to, from, api_key, headers)
Gem::Specification.new do |s|
s.name = 'mandrillamelo'
s.version = '0.2.0'
s.summary = "Changelog sender"
s.description = "Parse a changelog and send it via mandrill"
s.authors = ["Alessio Caiazza"]
s.email = 'alessio@chorally.com'
s.homepage = 'http://chorally.com/'
s.license = 'MIT'
s.bindir = '.'
s.executables << 'mandrillamelo'
s.add_development_dependency "bundler", "~> 1.13"
s.add_runtime_dependency 'vandamme', '~> 0.0.11'
s.add_runtime_dependency 'mandrill-api', '~> 1.0'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment