Skip to content

Instantly share code, notes, and snippets.

@fahim
Created March 11, 2015 23:43
Show Gist options
  • Save fahim/901a46438a647ed7a635 to your computer and use it in GitHub Desktop.
Save fahim/901a46438a647ed7a635 to your computer and use it in GitHub Desktop.
url = require('url')
querystring = require('querystring')
eventActions = require('./event-actions/all')
eventTypes = ["push"]
github = require('octonode')
client = github.client(process.env['HUBOT_GITHUB_ACCESS_TOKEN'])
repoName = process.env['HUBOT_GITHUB_MIGRATION_REPO']
repo = client.repo(repoName)
module.exports = (robot) ->
robot.router.post "/github/receive", (req, res) ->
query = querystring.parse(url.parse(req.url).query)
data = req.body
room = query.room || process.env["HUBOT_GITHUB_EVENT_NOTIFIER_ROOM"]
eventType = req.headers["x-github-event"]
if eventType != "push"
console.log "Can't process event type #{eventType}..."
else
console.log "Processing event type #{eventType}..."
if data? && data.commits?
for commit in data.commits
repo.commit commit.id, (errors, content, headers) ->
migrationFolderPattern = /db\/migrate/
defaultColumnFound = false
defaultColumnPattern = /(\r|\n|.)*\+(\s)*add_column(.)+default(\r|\n|.)+/i
defaultColumnMsg = ">:-( Database migration with a default column found in commit: https://github.com/#{repoName}/commit/#{commit.id}. Do not add columns to heavily accessed or very large tables without taking precautions described in: https://instacart.quip.com/WigAArXOsNWA"
renameColumnFound = false
renameColumnPattern = /(\r|\n|.)*\+(\s)*rename_column(.)+(\r|\n|.)+/i
renameColumnMsg = ">:-( Database migration with a rename_column found in commit: https://github.com/#{repoName}/commit/#{commit.id}. Do not rename columns without taking precautions described in: https://instacart.quip.com/WigAArXOsNWA"
console.log content
for file in content.files
if file.filename.match migrationFolderPattern
if file.patch.match defaultColumnPattern
defaultColumnFound = true
if file.patch.match renameColumnPattern
renameColumnFound = true
if defaultColumnFound
console.log "Migration with default column found!"
messageRoom robot, room, defaultColumnMsg
if renameColumnFound
console.log "Migration with rename column found!"
messageRoom robot, room, renameColumnMsg
if !renameColumnFound && !defaultColumnFound
console.log "No dangerous migration found. You're all good."
res.end ""
messageRoom = (robot, room, message) ->
try
robot.messageRoom room, message
catch error
robot.messageRoom room, "Whoa, I got an error: #{error}"
console.log "github repo event notifier error: #{error}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment