Skip to content

Instantly share code, notes, and snippets.

@patricksnape
Created August 25, 2011 08:53
Show Gist options
  • Save patricksnape/1170268 to your computer and use it in GitHub Desktop.
Save patricksnape/1170268 to your computer and use it in GitHub Desktop.
Set git tags at push
@ECHO OFF
python %HOMEDRIVE%%HOMEPATH%\development\push.py
git push
#!/usr/bin/env python
import urllib, urllib2, base64
import re
import subprocess
import json
import os
import platform
USER=''
API_TOKEN=''
GIT_API_URL='https://github.com/api/v2/json'
def run():
shell = (platform.system() == "Windows")
try:
branch_name = subprocess.Popen(['git', 'symbolic-ref', 'HEAD'], stdout=subprocess.PIPE, shell=shell).stdout.read().split("/")[2].strip()
messages = subprocess.Popen(['git', 'log', 'origin/%s..HEAD' % (branch_name), '--pretty=format:%B'], stdout=subprocess.PIPE, shell=shell).stdout.read().split("\n\n")
# If you wanted to get your own username
global USER
global API_TOKEN
USER = subprocess.Popen(['git', 'config', '--get', 'github.user'], stdout=subprocess.PIPE, shell=shell).stdout.read().strip()
API_TOKEN = subprocess.Popen(['git', 'config', '--get', 'github.token'], stdout=subprocess.PIPE, shell=shell).stdout.read().strip()
fetch_url = subprocess.Popen(['git', 'config', '--get', 'remote.origin.url'], stdout=subprocess.PIPE, shell=shell).stdout.read().strip()
except:
print "Failed to get repository information. Are you sure you are on in a valid git repository?"
return 1
try:
# Split always returns empty string matches at the start and end if all groups are totally matched
blank, turbulenz, repo, blank = re.split('git@github.com:([^/]+)/([^/]+)', fetch_url)
if repo[-4:] == '.git':
repo = repo[:-4]
except:
print "Failed to get the remote repository url"
return 1
for message in messages:
issue_ids = re.findall('#([0-9]+)', message)
labels = re.findall('\[([^\[\]]+)\]', message)
for issue in issue_ids:
for label in labels:
set_issue_label(turbulenz, repo, urllib.quote(label), issue)
return 0
def post_api(url, data):
try:
request = urllib2.Request(GIT_API_URL + url)
base64string = base64.encodestring('%s/token:%s' % (USER, API_TOKEN)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request, urllib.urlencode({'payload': data}))
result.close()
except:
print 'Failed to post api request to %s' % url
def get_api(url):
try:
request = urllib2.Request(GIT_API_URL + url)
base64string = base64.encodestring('%s/token:%s' % (USER, API_TOKEN)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
result.close()
except:
print 'Failed to get api request from %s' % url
def set_issue_label(user, repo, label, issue_number):
issue_tag_set_url = '/issues/label/add/%s/%s/%s/%s' % (user, repo, label, issue_number)
get_api(issue_tag_set_url)
if __name__ == '__main__':
exit(run())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment