#!/usr/bin/env python import sys import urllib import urllib2 import subprocess import re import json ################################# # Begin Configuration variables # ################################# # Url of the ttextpro.exe cgi on your web server URL = 'http://localhost/cgi-bin/ttextpro.exe' # Provider key from the Source Control Providers dialog PROVIDER_KEY = '{aa858562-025a-4236-9bf2-6cf4c81e0468}:{5d723ea4-0501-4343-8ee1-60189bcf3314}' # Tags used to attach to issues, test cases, and requirements. Format is "[TAG-NUMBER]". ISSUE_TAG = 'IS' TEST_CASE_TAG = 'TC' REQUIREMENT_TAG = 'RQ' ############################### # End Configuration variables # ############################### TAG_PATTERN = r'\[(%s|%s|%s)-(\d+)\]' % (ISSUE_TAG, TEST_CASE_TAG, REQUIREMENT_TAG) TAG_REGEX = re.compile(TAG_PATTERN, re.IGNORECASE) TAG_MAP = {ISSUE_TAG: 'issue', TEST_CASE_TAG: 'test case', REQUIREMENT_TAG: 'requirement'} # Get the full CGI URL. def get_url(): return URL + '?' + urllib.urlencode({ 'action': 'VerifyAttachment' }) # Get the object type based on the tag. def get_object_type(tag): return TAG_MAP[tag.upper()] # Get JSON data for a ref. def get_data(commit_message): return {'providerKey': PROVIDER_KEY, 'attachmentList': get_commit(commit_message)} # Get JSON data for commits being pushed to a ref. def get_commit(commit_message): return [{'toAttachList': get_attachments(commit_message)}] # Get JSON data for items to attach to. def get_attachments(commit_message): matches = TAG_REGEX.finditer(commit_message) attachments = [] for match in matches: attachments.append({'objectType': get_object_type(match.group(1)), 'number': int(match.group(2))}) return attachments # Handle attaching for a ref. def handle_commit_message(commit_message): req = urllib2.Request(get_url(), json.dumps(get_data(commit_message)), {'Content-Type': 'application/json'}) f = urllib2.urlopen(req) res = json.loads(f.read()) f.close() if res['errorCode'] > 1: print(res['errorMessage']) elif res['errorCode'] == 1: for err in res['errorList']: print(err['errorMessage']) return res['errorCode'] == 0 if __name__ == '__main__': if len(sys.argv) > 1: f = open(sys.argv[1], 'r') commit_message = f.read() f.close() if not handle_commit_message(commit_message): sys.exit(1)