Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuebric committed Oct 5, 2014
0 parents commit 3b48cc6
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "0.11"
- "0.10"
notifications:
email: false
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 hubot-scripts

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hubot Release Announce

A simple Hubot plugin used at AppNeta to send release announcement emails from GitHub PRs.
12 changes: 12 additions & 0 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fs = require 'fs'
path = require 'path'

module.exports = (robot, scripts) ->
scriptsPath = path.resolve(__dirname, 'src')
fs.exists scriptsPath, (exists) ->
if exists
for script in fs.readdirSync(scriptsPath)
if scripts? and '*' not in scripts
robot.loadFile(scriptsPath, script) if script in scripts
else
robot.loadFile(scriptsPath, script)
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "hubot-release-announce",
"description": "A hubot script to send quick release announcement emails.",
"version": "0.1.0",
"author": "Dan Kuebrich <[email protected]>",
"license": "MIT",

"keywords": [
"hubot",
"hubot-scripts"
],

"repository": {
"type": "git",
"url": "git://github.com/dankosaur/hubot-release-announce.git"
},

"bugs": {
"url": "https://github.com/dankosaur/hubot-release-announce/issues"
},

"dependencies": {
"coffee-script": "~1.6",
"githubot": "1.0.0-beta2",
"marked": "0.3.2",
"nodemailer": "1.3.0"
},

"devDependencies": {
"mocha": "*",
"chai": "*",
"sinon-chai": "*",
"sinon": "*"
},

"main": "index.coffee",

"scripts": {
"test": "script/test"
}
}
18 changes: 18 additions & 0 deletions script/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Make sure everything is development forever
export NODE_ENV=development

# Load environment specific environment variables
if [ -f .env ]; then
source .env
fi

if [ -f .env.${NODE_ENV} ]; then
source .env.${NODE_ENV}
fi

npm install

# Make sure coffee and mocha are on the path
export PATH="node_modules/.bin:$PATH"
6 changes: 6 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# bootstrap environment
source script/bootstrap

mocha --compilers coffee:coffee-script
103 changes: 103 additions & 0 deletions src/release-notification.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Description:
# Send an email about a pull request denoting a release which has shipped.
#
# Dependencies:
# github, nodemailer, marked
#
# Configuration:
# HUBOT_GITHUB_TOKEN
# HUBOT_GITHUB_USER
# HUBOT_GITHUB_API
# HUBOT_RELEASE_MAIL_FROM
# HUBOT_RELEASE_MAIL_TO
# HUBOT_RELEASE_SMTP_HOST
# HUBOT_RELEASE_SMTP_USER
# HUBOT_RELEASE_SMTP_PASS
#
# Commands:
# hubot release announce [repo/pr-id] - Sends a release email based on the text of the PR. Repo name is optional; defaults to tracelons.
# hubot release preview [repo/pr-id] - Prints out what you're about to email.
#
# Notes:
# HUBOT_GITHUB_API allows you to set a custom URL path (for Github enterprise users)
#
# Author:
# dankosaur

module.exports = (robot) ->
# depends
github = require("githubot")(robot)
nodemailer = require("nodemailer")
marked = require("marked")

# env vars
unless (url_api_base = process.env.HUBOT_GITHUB_API)?
url_api_base = "https://api.github.com"
unless (from_email = process.env.HUBOT_RELEASE_MAIL_FROM)?
from_email = "[email protected]"
unless (to_email = process.env.HUBOT_RELEASE_MAIL_TO)?
to_email = "[email protected]"
unless (smtp_host = process.env.HUBOT_RELEASE_SMTP_HOST)?
smtp_host = "smtp.sendgrid.net"
unless (smtp_user = process.env.HUBOT_RELEASE_SMTP_USER)?
throw new Error("HUBOT_RELEASE_STMP_USER required")
unless (smtp_pass = process.env.HUBOT_RELEASE_SMTP_PASS)?
throw new Error("HUBOT_RELEASE_STMP_PASS required")

# email transport
mailer = nodemailer.createTransport
service: "SMTP"
host: smtp_host
port: 25
auth:
user: smtp_user
pass: smtp_pass

robot.respond /release\s+(announce|preview)\s+(.+)/i, (msg) ->
mode = msg.match[1]

# parse repo / PR input value
parts = msg.match[2].split "/"
if parts.length == 3
repo = parts[0] + "/" + parts[1]
pr = parts[2]
else
repo = "tracelytics/tracelons"
pr = parts[0]

api_url = "#{url_api_base}/repos/#{repo}/issues/#{pr}"

github.get api_url, (pull) ->

# validate response
if not pull
msg.send "PR not found."
else if not pull.pull_request
msg.send "Not a PR."
else
# construct email
base_issue_url = "https://github.com/#{repo}/issues"
pr_url = "#{base_issue_url}/#{pr}"
linked_body = pull.body.replace /#(\d+)/g, (match) ->
num = match.slice 1
link = "#{base_issue_url}/#{num}"
"[##{num}](#{link})"

email_subject = "[release] #{pull.title}"
email_body = "###{pull.title}\n\n#{linked_body}\n\n[#{pr_url}](#{pr_url})"

mail =
from: from_email
to: to_email
subject: email_subject
text: email_body
html: marked(email_body)

if mode == "preview"
msg.send email_subject + "\n" + email_body
else
mailer.sendMail mail, (error, response) ->
if error
msg.send "Error sending email: #{error}"
else
msg.send "Sent release announcement for #{pr} to #{to_email}"
21 changes: 21 additions & 0 deletions test/release-notification_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
chai = require 'chai'
sinon = require 'sinon'
chai.use require 'sinon-chai'

expect = chai.expect

describe 'release-notification', ->
beforeEach ->
@robot =
respond: sinon.spy()
hear: sinon.spy()

require('../src/release-notification')(@robot)

it 'recognizes release preview command', ->
expect(@robot.respond).to.have.been.calledWith(/release preview appneta/node-traceview#9/)
expect(@robot.respond).to.have.been.calledWith(/release preview #9/)

it 'recognizes release announce command', ->
expect(@robot.respond).to.have.been.calledWith(/release announce appneta/node-traceview#9/)
expect(@robot.respond).to.have.been.calledWith(/release announce #9/)

0 comments on commit 3b48cc6

Please sign in to comment.