diff options
| author | D Delmar Davis <don@suspectdevices.com> | 2019-03-22 23:12:29 -0700 |
|---|---|---|
| committer | D Delmar Davis <don@suspectdevices.com> | 2019-03-22 23:12:29 -0700 |
| commit | 54ab7d02d5fcadee6ee11b27c3b654804f4686de (patch) | |
| tree | ad9e1d3600bf69cac2b736ca9315f375a42dd72a | |
| parent | d08d1839237bf997b74da0c5da6a696bc66d96df (diff) | |
Initial Code Commit
| -rwxr-xr-x | bbpost-hook.wsgi | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/bbpost-hook.wsgi b/bbpost-hook.wsgi new file mode 100755 index 0000000..b0c9965 --- /dev/null +++ b/bbpost-hook.wsgi @@ -0,0 +1,44 @@ +#--------------------------------------------------------------------------- bbpost-hook.wsgi +# simeple hook to mirror bitbucket repo. +# assumes web user has ssh key basedrepo access. +# hacked from pre-json sample bitbucket webhookhook. +# +# Add the following to your apache configuratino +# +# WSGIScriptAlias /bitbucket-mirror /var/www/wsgi/bbpost-hook.wsgi +# <Directory /var/www/wsgi> +# WSGIApplicationGroup %{GLOBAL} +# Require all granted +# </Directory> +# +import json +from os import makedirs +from os.path import expanduser, join, isdir +from subprocess import call + +BACKUP_ROOT = '/var/trac/devel/private' # change this according to your server setup + +def application( environ, start_response ): + headers = [ ( 'Content-type', 'text/plain' ) ] + try: + length = int( environ[ 'CONTENT_LENGTH' ] ) + request_body= environ[ 'wsgi.input' ].read( length ) + data=json.loads(str(request_body)) + repository = data[ u'repository' ] + owner,repo = repository[ 'full_name' ].split('/') + owner_dir = join( BACKUP_ROOT, owner ) # os.path.join + repo_dest = join( owner_dir, repo ) + if not isdir( repo_dest ): + if not isdir( owner_dir ): makedirs( owner_dir, 0700 ) +# print >>environ['wsgi.errors'], 'git', 'clone', '--mirror', 'git://bitbucket.com/{0}/{1}.git'.format( owner, repo ), repo_dest + call([ 'git', 'clone', '--mirror', 'git://bitbucket.com/{0}/{1}.git'.format( owner, repo ), repo_dest ]) + else: +# print >> environ['wsgi.errors'], 'git', '--git-dir', repo_dest , 'remote', 'update' + call([ 'git', '--git-dir', repo_dest, 'remote', 'update' ]) + except Exception as e: + print >>environ['wsgi.errors'],e + start_response( '500 Internal server error', headers ) + return [ "Internal server error" ] + else: + start_response( '200 OK', headers ) + return [ "OK" ] |
