Skip to content

Commit

Permalink
Merge pull request #3 from fordfrog/master
Browse files Browse the repository at this point in the history
updated to work with github api v3
  • Loading branch information
ttencate committed Aug 18, 2012
2 parents 2dfa541 + 0e54ccd commit ba821d2
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions issues.py
Expand Up @@ -25,33 +25,31 @@
trackers = soup.document.find('trackers', recursive=False).findAll('tracker', recursive=False) trackers = soup.document.find('trackers', recursive=False).findAll('tracker', recursive=False)


from urllib import urlencode from urllib import urlencode
from urllib2 import Request, urlopen, HTTPError from urllib2 import HTTPError
from base64 import b64encode from base64 import b64encode
from time import sleep from time import sleep
from getpass import getpass from getpass import getpass
import requests
from requests.auth import HTTPBasicAuth
import json
import re 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 global github_repo, github_user, github_password
url = 'https://github.com/api/v2/xml/%s/%s/%s' % (before, github_repo, after) url = 'https://api.github.com/repos/%s/%s' % (github_repo, request)
if data_dict is None: if method == 'PATCH':
data = None response = requests.patch(url, data=json.dumps(data), auth=HTTPBasicAuth(github_user, github_password))
else: else:
data = urlencode([(unicode(key).encode('utf-8'), unicode(value).encode('utf-8')) for key, value in data_dict.iteritems()]) response = requests.post(url, data=json.dumps(data), auth=HTTPBasicAuth(github_user, github_password))
headers = {
'Authorization': 'Basic %s' % b64encode('%s:%s' % (github_user, github_password)),
}
request = Request(url, data, headers)
response = urlopen(request)
# GitHub limits API calls to 60 per minute # GitHub limits API calls to 60 per minute
sleep(1) sleep(1)
return response return response


def rest_call(before, after, data_dict=None): def rest_call(method, request, data=None):
count500err = 0 count500err = 0
while True: while True:
try: try:
return __rest_call_unchecked(before, after, data_dict) return __rest_call_unchecked(method, request, data)
except HTTPError, e: except HTTPError, e:
print "Got HTTPError:", e print "Got HTTPError:", e
l = data_dict and max(map(len, data_dict.itervalues())) or 0 l = data_dict and max(map(len, data_dict.itervalues())) or 0
Expand Down Expand Up @@ -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) 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}) response = rest_call('POST', 'issues', {'title': title, 'body': body})
issue = BeautifulStoneSoup(response, convertEntities=BeautifulStoneSoup.ALL_ENTITIES) if response.status_code == 500:
number = issue.number.string print "ISSUE CAUSED SERVER SIDE ERROR AND WAS NOT SAVED!!! Import will continue."
for label in labels: else:
print 'Attaching label: %s' % label issue = response.json
rest_call('issues/label/add', '%s/%s' % (label, number)) number = issue['number']
for comment in comments: print 'Attaching labels: %s' % labels
print 'Creating comment: %s' % comment[:50].replace('\n', ' ').replace(chr(13), '') rest_call('POST', 'issues/%s/labels' % (number), labels)
rest_call('issues/comment', number, {'comment': comment}) for comment in comments:
if closed: print 'Creating comment: %s' % comment[:50].replace('\n', ' ').replace(chr(13), '')
print 'Closing...' rest_call('POST', 'issues/%s/comments' % (number), {'body': comment})
rest_call('issues/close', number) if closed:
print 'Closing...'
rest_call('PATCH', 'issues/%s' % (number), {'state': 'closed'})




import signal import signal
Expand Down

0 comments on commit ba821d2

Please sign in to comment.