Skip to content

Commit 5e24dfb

Browse files
committed
Read github user/pass from .githubrc (fixes #51)
1 parent c1d0ce1 commit 5e24dfb

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

webdiff/github_fetcher.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,39 @@
1717

1818
temp_dirs = []
1919

20+
_github_memo = None
21+
def _github():
22+
global _github_memo
23+
if _github_memo: return _github_memo
24+
25+
def simple_fallback(message=None):
26+
global _github_memo
27+
if message: sys.stderr.write(message + '\n')
28+
_github_memo = Github()
29+
return _github_memo
30+
31+
github_rc = os.path.join(os.path.expanduser('~'), '.githubrc')
32+
if os.path.exists(github_rc):
33+
try:
34+
pairs = open(github_rc).read()
35+
except IOError:
36+
return simple_fallback('Unable to read .githubrc file. Using anonymous API access.')
37+
else:
38+
kvs = {}
39+
for line in pairs.split('\n'):
40+
if ':' not in line: continue
41+
k, v = line.split(': ', 1)
42+
kvs[k] = v
43+
44+
if not kvs.get('user.login'):
45+
return simple_fallback('.githubrc missing user.login. Using anonymous API access.')
46+
if not kvs.get('user.password'):
47+
return simple_fallback('.githubrc missing user.password. Using anonymous API access.')
48+
_github_memo = Github(kvs['user.login'], kvs['user.password'])
49+
else:
50+
_github_memo = Github()
51+
return _github_memo
52+
2053

2154
def put_in_dir(dirname, filename, contents):
2255
"""Puts contents into filename in dirname.
@@ -39,7 +72,7 @@ def fetch_pull_request(owner, repo, num):
3972
Returns before_dir, after_dir.
4073
"""
4174
sys.stderr.write('Loading pull request %s/%s#%s from github...\n' % (owner, repo, num))
42-
g = Github() # TODO(danvk): add a way to pass an OAuth token here
75+
g = _github()
4376
pr = g.get_user(owner).get_repo(repo).get_pull(num)
4477
base_repo = pr.base.repo
4578
head_repo = pr.head.repo
@@ -80,7 +113,7 @@ def get_pr_repo(num):
80113
'directory. Are you in a git repo? Try running '
81114
'`git remote -v` to debug.')
82115

83-
g = Github()
116+
g = _github()
84117
for remote in remotes:
85118
owner = remote['owner']
86119
repo = remote['repo']

0 commit comments

Comments
 (0)