66# Use this PR for testing to see all four types of change at once:
77# https://github.com/danvk/test-repo/pull/2/
88
9- import atexit
109from collections import OrderedDict
11- import errno
1210import os
1311import re
1412import subprocess
15- import sys
16- import tempfile
1713
1814from github import Github , UnknownObjectException
1915
2016from util import memoize
2117
2218
23- class GitHubDiff (object ):
24- def __init__ (self , pr , github_file ):
25- self ._pr = pr
26- self ._file = github_file
27- self .type = {
28- 'modified' : 'change' ,
29- 'renamed' : 'move' ,
30- 'added' : 'add' ,
31- 'removed' : 'delete'
32- }[github_file .status ]
33- self ._a_path = None
34- self ._b_path = None
35-
36- @property
37- def a (self ):
38- if self .type == 'move' :
39- return self ._file .raw_data ['previous_filename' ]
40- elif self .type == 'add' :
41- return None
42- else :
43- return self ._file .filename
44-
45- @property
46- def b (self ):
47- if self .type == 'delete' :
48- return None
49- else :
50- return self ._file .filename
51-
52- # NB: these are @memoize'd via fetch()
53- @property
54- def a_path (self ):
55- return fetch (self ._pr .base .repo , self .a , self ._pr .base .sha )
56-
57- @property
58- def b_path (self ):
59- return fetch (self ._pr .head .repo , self .b , self ._pr .head .sha )
60-
61- def __repr__ (self ):
62- return '%s (%s)' % (self .a or self .b , self .type )
63-
64- # TOOD: diffstats are accessible via file.{changes,additions,deletions}
65-
66-
6719@memoize
68- def fetch (repo , filename , sha ):
69- if filename is None : return None
70- data = repo .get_file_contents (filename , sha ).decoded_content
71- _ , ext = os .path .splitext (filename )
72- fd , path = tempfile .mkstemp (suffix = ext )
73- open (path , 'wb' ).write (data )
74- return path
75-
76-
77- @memoize
78- def _github ():
20+ def github ():
21+ '''Returns a GitHub API object with auth, if it's available.'''
7922 def simple_fallback (message = None ):
8023 if message : sys .stderr .write (message + '\n ' )
8124 return Github ()
@@ -102,17 +45,6 @@ def simple_fallback(message=None):
10245 return Github ()
10346
10447
105- def fetch_pull_request (owner , repo , num ):
106- '''Return a list of Diff objects for a pull request.'''
107- sys .stderr .write ('Loading pull request %s/%s#%s from github...\n ' % (
108- owner , repo , num ))
109- g = _github ()
110- pr = g .get_user (owner ).get_repo (repo ).get_pull (num )
111- files = pr .get_files ()
112-
113- return [GitHubDiff (pr , f ) for f in files ]
114-
115-
11648class NoRemoteError (Exception ):
11749 pass
11850
@@ -132,7 +64,7 @@ def get_pr_repo(num):
13264 'directory. Are you in a git repo? Try running '
13365 '`git remote -v` to debug.' )
13466
135- g = _github ()
67+ g = github ()
13668 for remote in remotes :
13769 owner = remote ['owner' ]
13870 repo = remote ['repo' ]
@@ -189,10 +121,3 @@ def _get_remotes():
189121 ['git' , 'remote' , '-v' ], stdout = subprocess .PIPE ).communicate ()[0 ].split ('\n ' )
190122 return _parse_remotes (remote_lines )
191123
192-
193-
194- @atexit .register
195- def _cleanup ():
196- """Delete any temporary directories which were created by two_folders()."""
197- #for d in temp_dirs:
198- # os.removedirs(d)
0 commit comments