Skip to content

Commit a7d7686

Browse files
committed
Merge pull request tobami#151 from philangist/fix_github_api
Update github.py to relfect newest version of github's API. Change chang...
2 parents cc593d0 + eb7933e commit a7d7686

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

codespeed/github.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
logger = logging.getLogger(__name__)
2020

2121
GITHUB_URL_RE = re.compile(
22-
r'^(?P<proto>\w+)://github.com/(?P<username>[^/]+)/(?P<project>[^/]+)[.]git$')
22+
r'^(?P<proto>\w+)://github.com/(?P<username>[^/]+)/(?P<project>[^/]+)([.]git)?$')
2323

2424
# We currently use a simple linear search of on a single parent to retrieve
2525
# the history. This is often good enough, but might miss the actual starting
@@ -33,7 +33,7 @@ def updaterepo(project, update=True):
3333

3434

3535
def retrieve_revision(commit_id, username, project, revision=None):
36-
commit_url = 'http://github.com/api/v2/json/commits/show/%s/%s/%s' % (
36+
commit_url = 'https://api.github.com/repos/%s/%s/git/commits/%s' % (
3737
username, project, commit_id)
3838

3939
commit_json = cache.get(commit_url)
@@ -46,20 +46,18 @@ def retrieve_revision(commit_id, username, project, revision=None):
4646
commit_url, e, exc_info=True)
4747
raise e
4848

49-
if 'error' in commit_json:
49+
if commit_json["message"] in ("Not Found", "Server Error",):
5050
# We'll still cache these for a brief period of time to avoid making too many requests:
5151
cache.set(commit_url, commit_json, 300)
5252
else:
5353
# We'll cache successes for a very long period of time since
5454
# SCM diffs shouldn't change:
5555
cache.set(commit_url, commit_json, 86400 * 30)
5656

57-
if 'error' in commit_json:
58-
raise RuntimeError("Unable to load %s: %s" % (commit_url, commit_json['error']))
57+
if commit_json["message"] in ("Not Found", "Server Error",):
58+
raise RuntimeError("Unable to load %s: %s" % (commit_url, commit_json["message"]))
5959

60-
commit = commit_json['commit']
61-
62-
date = isodate.parse_datetime(commit['committed_date'])
60+
date = isodate.parse_datetime(commit_json['committer']['date'])
6361

6462
if revision:
6563
# Overwrite any existing data we might have for this revision since
@@ -68,19 +66,19 @@ def retrieve_revision(commit_id, username, project, revision=None):
6866
# We need to convert the timezone-aware date to a naive (i.e.
6967
# timezone-less) date in UTC to avoid killing MySQL:
7068
revision.date = date.astimezone(isodate.tzinfo.Utc()).replace(tzinfo=None)
71-
revision.author = commit['author']['name']
72-
revision.message = commit['message']
69+
revision.author = commit_json['author']['name']
70+
revision.message = commit_json['message']
7371
revision.full_clean()
7472
revision.save()
7573

7674
return {'date': date,
77-
'message': commit['message'],
75+
'message': commit_json['message'],
7876
'body': "", # TODO: pretty-print diffs
79-
'author': commit['author']['name'],
80-
'author_email': commit['author']['email'],
81-
'commitid': commit['id'],
82-
'short_commit_id': commit['id'][0:7],
83-
'parents': commit['parents']}
77+
'author': commit_json['author']['name'],
78+
'author_email': commit_json['author']['email'],
79+
'commitid': commit_json['sha'],
80+
'short_commit_id': commit_json['sha'][0:7],
81+
'parents': commit_json['parents']}
8482

8583

8684
def getlogs(endrev, startrev):
@@ -90,6 +88,9 @@ def getlogs(endrev, startrev):
9088
else:
9189
revisions = [i for i in (startrev, endrev) if i.commitid]
9290

91+
if endrev.branch.project.repo_path[-1] == '/':
92+
endrev.branch.project.repo_path = endrev.branch.project.repo_path[:-1]
93+
9394
m = GITHUB_URL_RE.match(endrev.branch.project.repo_path)
9495

9596
if not m:
@@ -110,16 +111,16 @@ def getlogs(endrev, startrev):
110111
last_rev_data = retrieve_revision(revision.commitid, username, project, revision)
111112
logs.append(last_rev_data)
112113
revision_count += 1
113-
ancestor_found = (startrev.commitid in [rev['id'] for rev in last_rev_data['parents']])
114+
ancestor_found = (startrev.commitid in [rev['sha'] for rev in last_rev_data['parents']])
114115

115116
# Simple approach to find the startrev, stop after found or after
116117
# #GITHUB_REVISION_LIMIT revisions are fetched
117118
while (revision_count < GITHUB_REVISION_LIMIT
118119
and not ancestor_found
119120
and len(last_rev_data['parents']) > 0):
120-
last_rev_data = retrieve_revision(last_rev_data['parents'][0]['id'], username, project)
121+
last_rev_data = retrieve_revision(last_rev_data['parents'][0]['sha'], username, project)
121122
logs.append(last_rev_data)
122123
revision_count += 1
123-
ancestor_found = (startrev.commitid in [rev['id'] for rev in last_rev_data['parents']])
124+
ancestor_found = (startrev.commitid in [rev['sha'] for rev in last_rev_data['parents']])
124125

125126
return sorted(logs, key=lambda i: i['date'], reverse=True)

codespeed/tests/tests.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,14 @@ def test_github_browsing_url(self):
451451
# It should work with https:// as well as git:// urls
452452
self.github_project.save()
453453
self.assertEquals(self.github_project.commit_browsing_url,
454-
'https://github.com/tobami/codespeed/commit/{commitid}')
454+
'https://github.com/tobami/codespeed.git/'
455+
'commit/{commitid}')
455456

456457
self.github_project.repo_path = 'git://github.com/tobami/codespeed.git'
457458
self.github_project.save()
458459
self.assertEquals(self.github_project.commit_browsing_url,
459-
'https://github.com/tobami/codespeed/commit/{commitid}')
460+
'https://github.com/tobami/codespeed.git/'
461+
'commit/{commitid}')
460462

461463
# If filled in, commit browsing url should not change
462464
self.github_project.commit_browsing_url = 'https://example.com/{commitid}'

0 commit comments

Comments
 (0)