diff options
author | Thomas ten Cate <ttencate@gmail.com> | 2012-08-18 09:00:49 -0700 |
---|---|---|
committer | Thomas ten Cate <ttencate@gmail.com> | 2012-08-18 09:00:49 -0700 |
commit | ba821d25b6dba3f466597b82fb9de520e0814299 (patch) | |
tree | a3f3e0cc3f52625a3c08be69920fec49cfc6c8ee | |
parent | 2dfa541dd173b8cd2227c22c45ebbbbed9c0ab30 (diff) | |
parent | 0e54ccdb0f757f629fa0b4ca8f7a840b1de4d49e (diff) |
Merge pull request #3 from fordfrog/master
updated to work with github api v3
-rwxr-xr-x | issues.py | 50 |
1 files changed, 25 insertions, 25 deletions
@@ -25,33 +25,31 @@ soup = BeautifulStoneSoup(open(xml_file_name, 'r'), convertEntities=BeautifulSto trackers = soup.document.find('trackers', recursive=False).findAll('tracker', recursive=False) from urllib import urlencode -from urllib2 import Request, urlopen, HTTPError +from urllib2 import HTTPError from base64 import b64encode from time import sleep from getpass import getpass +import requests +from requests.auth import HTTPBasicAuth +import json import re -def __rest_call_unchecked(before, after, data_dict=None): +def __rest_call_unchecked(method, request, data=None): global github_repo, github_user, github_password - url = 'https://github.com/api/v2/xml/%s/%s/%s' % (before, github_repo, after) - if data_dict is None: - data = None + url = 'https://api.github.com/repos/%s/%s' % (github_repo, request) + if method == 'PATCH': + response = requests.patch(url, data=json.dumps(data), auth=HTTPBasicAuth(github_user, github_password)) else: - data = urlencode([(unicode(key).encode('utf-8'), unicode(value).encode('utf-8')) for key, value in data_dict.iteritems()]) - headers = { - 'Authorization': 'Basic %s' % b64encode('%s:%s' % (github_user, github_password)), - } - request = Request(url, data, headers) - response = urlopen(request) + response = requests.post(url, data=json.dumps(data), auth=HTTPBasicAuth(github_user, github_password)) # GitHub limits API calls to 60 per minute sleep(1) return response -def rest_call(before, after, data_dict=None): +def rest_call(method, request, data=None): count500err = 0 while True: try: - return __rest_call_unchecked(before, after, data_dict) + return __rest_call_unchecked(method, request, data) except HTTPError, e: print "Got HTTPError:", e l = data_dict and max(map(len, data_dict.itervalues())) or 0 @@ -121,18 +119,20 @@ def handle_tracker_item(item, issue_title_prefix): ])) print 'Creating: %s [%s] (%d comments)%s for SF #%s' % (title, ','.join(labels), len(comments), ' (closed)' if closed else '', item.id.string) - response = rest_call('issues/open', '', {'title': title, 'body': body}) - issue = BeautifulStoneSoup(response, convertEntities=BeautifulStoneSoup.ALL_ENTITIES) - number = issue.number.string - for label in labels: - print 'Attaching label: %s' % label - rest_call('issues/label/add', '%s/%s' % (label, number)) - for comment in comments: - print 'Creating comment: %s' % comment[:50].replace('\n', ' ').replace(chr(13), '') - rest_call('issues/comment', number, {'comment': comment}) - if closed: - print 'Closing...' - rest_call('issues/close', number) + response = rest_call('POST', 'issues', {'title': title, 'body': body}) + if response.status_code == 500: + print "ISSUE CAUSED SERVER SIDE ERROR AND WAS NOT SAVED!!! Import will continue." + else: + issue = response.json + number = issue['number'] + print 'Attaching labels: %s' % labels + rest_call('POST', 'issues/%s/labels' % (number), labels) + for comment in comments: + print 'Creating comment: %s' % comment[:50].replace('\n', ' ').replace(chr(13), '') + rest_call('POST', 'issues/%s/comments' % (number), {'body': comment}) + if closed: + print 'Closing...' + rest_call('PATCH', 'issues/%s' % (number), {'state': 'closed'}) import signal |