def pull(user, repo, number): token = session['token'] commits = github.get_pull_request_commits(token, user, repo, number) pr = github.get_pull_request(token, user, repo, number) comments = github.get_pull_request_comments(token, user, repo, number) commit_to_comments = defaultdict(int) for comment in comments['diff_level']: commit_to_comments[comment['original_commit_id']] += 1 commits.reverse() # Add an entry for the base commit. commits.append({ 'sha': pr['base']['sha'], 'commit': { 'message': '(%s)' % pr['base']['ref'], 'author': {'date': ''} }, 'author': {'login': ''} }) for commit in commits: commit['comment_count'] = commit_to_comments[commit['sha']] return render_template('pull_request.html', commits=commits, user=user, repo=repo, pull_request=pr, comments=comments)
def file_diff(user, repo, number): path = request.args.get('path', '') sha1 = request.args.get('sha1', '') sha2 = request.args.get('sha2', '') if not (path and sha1 and sha2): return "Incomplete request (need path, sha1, sha2)" # TODO(danvk): consolidate this code with the pull route token = session['token'] commits = github.get_pull_request_commits(token, user, repo, number) pr = github.get_pull_request(token, user, repo, number) comments = github.get_pull_request_comments(token, user, repo, number) open('/tmp/commits.txt', 'wb').write(json.dumps(commits, indent=2)) commit_to_comments = defaultdict(int) for comment in comments['diff_level']: commit_to_comments[comment['original_commit_id']] += 1 commits.reverse() # Add an entry for the base commit. commits.append({ 'sha': pr['base']['sha'], 'commit': { 'message': '(%s)' % pr['base']['ref'], 'author': {'date': ''} }, 'author': {'login': ''} }) # github excludes the first four header lines of "git diff" diff_info = github.get_diff_info(token, user, repo, sha1, sha2) unified_diff = github.get_file_diff(token, user, repo, path, sha1, sha2) if not unified_diff or not diff_info: return "Unable to get diff for %s..%s" % (sha1, sha2) github_diff = '\n'.join(unified_diff.split('\n')[4:]) # TODO(danvk): only annotate comments on this file. github_comments.add_line_numbers_to_comments(token, user, repo, pr['base']['sha'], comments['diff_level']) differing_files = [f['filename'] for f in diff_info['files']] before = github.get_file_at_ref(token, user, repo, path, sha1) after = github.get_file_at_ref(token, user, repo, path, sha2) def diff_url(path): return (url_for('file_diff', user=user, repo=repo, number=number) + '?path=' + urllib.quote(path) + '&sha1=' + urllib.quote(sha1) + '&sha2=' + urllib.quote(sha2)) linked_files = [{'path':p, 'link': diff_url(p)} for p in differing_files] if path in differing_files: file_idx = differing_files.index(path) prev_file = linked_files[file_idx - 1] if file_idx > 0 else None next_file = linked_files[file_idx + 1] if file_idx < len(linked_files) - 1 else None else: # The current file is not part of this diff. # Just do something sensible. prev_file = None next_file = linked_files[0] if len(linked_files) > 0 else None pull_request_url = url_for('pull', user=user, repo=repo, number=number) return render_template('file_diff.html', commits=commits, user=user, repo=repo, pull_request=pr, comments=comments, path=path, sha1=sha1, sha2=sha2, before_contents=before, after_contents=after, differing_files=linked_files, prev_file=prev_file, next_file=next_file, github_diff=github_diff, pull_request_url=pull_request_url)