Skip to content

Instantly share code, notes, and snippets.

@zombience
Created December 29, 2015 00:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zombience/7ed1972022b2f8a55b8e to your computer and use it in GitHub Desktop.
Save zombience/7ed1972022b2f8a55b8e to your computer and use it in GitHub Desktop.
simple python post-commit hook for slack subversion integration
#! /usr/bin/python
#############################################################
# add a new integration and generate a new token before using with new repo
# https://your_team_name.slack.com/apps/manage/
#############################################################
import os
import sys
import subprocess
import urllib
import urllib2
import json
#Set slack info
TOKEN = '' #api token for slack integration
DOMAIN = 'your_team_name.slack.com' #add your team's slack URL here
#svnlook location
LOOK='svnlook'
def main(argv):
import os.path
stream = sys.stderr or sys.stdout
status = 0
result = 'posted commit to slack'
payload = get_commit_info(argv[1], argv[2])
try:
notify_slack(DOMAIN, TOKEN, payload)
except:
status += 1
result = sys.exc_info()[0]
raise
stream.write(result)
sys.exit(status)
def get_commit_info( repo, revision ):
log = svn_look('log', repo, '-r', revision)
author = svn_look('author', repo, '-r', revision)
repo_name = repo.split('/')
payload = {'revision' : 'repo: ' + repo_name[-1] + '\nrevision: ' + revision,
'log' : 'commit message: \n' + log,
'author': '\nauthor: ' + author}
return payload
def svn_look( *args ):
p = subprocess.Popen(' '.join([LOOK] + list(args)), stdout=subprocess.PIPE, shell=True, stderr=subprocess.STDOUT )
out, err = p.communicate()
return out
def notify_slack(domain, token, payload ):
#create request url
url = 'https://' + domain + '/services/hooks/subversion?token=' + token
#urlencode and post
urllib2.urlopen( url, urllib.urlencode( { 'payload' : json.dumps( payload ) } ) )
if __name__ == '__main__':
main(sys.argv)
@zombience
Copy link
Author

post-commit.py

this script is a simple notification for slack integration

usage

depending on your SVN configuration, you'll likely have to rename this file from 'post-commit.py' to 'post-commit' with no suffix
copy this file to directory:
path/to/your/repo/hooks/
and svn should take care of the rest

this script uses urllib rather than the request module in order to avoid admin issues around adding python modules to servers.
adding a hook script requires less permissions than installing python modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment