@@ -48,7 +48,6 @@ import pipes
48
48
49
49
def main ():
50
50
repo , remote = '' , None
51
- token = os .popen ("git config --get github.token" ).read ().rstrip ()
52
51
# parse command line options
53
52
try :
54
53
opts , args = getopt .getopt (sys .argv [1 :], "hr:" , ["help" , "repo:" ])
@@ -70,6 +69,9 @@ def main():
70
69
if remote is None and repo == '' :
71
70
remote = 'origin'
72
71
72
+ # attempt to get token from git config
73
+ token = os .popen ("git config --get github.token" ).read ().rstrip ()
74
+ print token
73
75
# get repo name from git config:
74
76
if (repo == '' ):
75
77
repo = os .popen ('git config github.repo' ).read ().strip ()
@@ -119,12 +121,23 @@ def show(repo, token):
119
121
print "loading open pull requests for %s..." % (repo )
120
122
print
121
123
url = "https://api.github.com/repos/%s/pulls" % (repo )
124
+
125
+ if len (token ):
126
+ headers = {'User-Agent' : 'git-pull-request' , 'Authorization' : 'token %s' % token }
127
+ else :
128
+ headers = {'User-Agent' : 'git-pull-request' }
129
+
122
130
req = urllib2 .Request (
123
- url , headers = { 'User-Agent' : 'Awesome-Octocat-App' , 'Authorization' : 'token ' + token } )
131
+ url , headers = headers )
124
132
try :
125
133
response = urllib2 .urlopen (req )
126
134
except urllib2 .HTTPError , msg :
127
- print "error loading pull requests for repo %s: %s" % (repo , msg )
135
+ if msg .code == 404 :
136
+ # GH replies with 404 when a repo is not found or private and we request without OAUTH
137
+ print "repo %s not found: %s" % (repo , msg )
138
+ print "if this is a private repo, please set github.token to a valid GH oauth token"
139
+ else :
140
+ print "error loading pull requests for repo %s: %s" % (repo , msg )
128
141
exit (1 )
129
142
130
143
data = response .read ()
@@ -144,9 +157,24 @@ def fetch(repo, token, pullreq):
144
157
print "loading pull request info for request %s..." % (pullreq )
145
158
print
146
159
url = "https://api.github.com/repos/%s/pulls/%s" % (repo , pullreq )
160
+
161
+ if len (token ):
162
+ headers = {'User-Agent' : 'git-pull-request' , 'Authorization' : 'token %s' % token }
163
+ else :
164
+ headers = {'User-Agent' : 'git-pull-request' }
165
+
147
166
req = urllib2 .Request (
148
- url , headers = {'User-Agent' : 'Awesome-Octocat-App' , 'Authorization' : 'token ' + token })
149
- response = urllib2 .urlopen (req )
167
+ url , headers = headers )
168
+ try :
169
+ response = urllib2 .urlopen (req )
170
+ except urllib2 .HTTPError , msg :
171
+ if msg .code == 404 :
172
+ # GH replies with 404 when a repo is not found or private and we request without OAUTH
173
+ print "repo %s not found: %s" % (repo , msg )
174
+ print "if this is a private repo, please set github.token to a valid GH oauth token"
175
+ else :
176
+ print "error loading pull requests for repo %s: %s" % (repo , msg )
177
+ exit (1 )
150
178
data = response .read ()
151
179
if (data == '' ):
152
180
print "failed to speak with github."
0 commit comments