Skip to content

Commit 4ccbe7b

Browse files
author
Michael Hobbs
committed
set proper user-agent. better error messaging when github replies with 404
1 parent 93a0566 commit 4ccbe7b

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

git-pull-request

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import pipes
4848

4949
def main():
5050
repo, remote = '', None
51-
token = os.popen("git config --get github.token").read().rstrip()
5251
# parse command line options
5352
try:
5453
opts, args = getopt.getopt(sys.argv[1:], "hr:", ["help", "repo:"])
@@ -70,6 +69,9 @@ def main():
7069
if remote is None and repo == '':
7170
remote = 'origin'
7271

72+
# attempt to get token from git config
73+
token = os.popen("git config --get github.token").read().rstrip()
74+
print token
7375
# get repo name from git config:
7476
if(repo == ''):
7577
repo = os.popen('git config github.repo').read().strip()
@@ -119,12 +121,23 @@ def show(repo, token):
119121
print "loading open pull requests for %s..." % (repo)
120122
print
121123
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+
122130
req = urllib2.Request(
123-
url, headers={'User-Agent': 'Awesome-Octocat-App', 'Authorization': 'token ' + token})
131+
url, headers=headers)
124132
try:
125133
response = urllib2.urlopen(req)
126134
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)
128141
exit(1)
129142

130143
data = response.read()
@@ -144,9 +157,24 @@ def fetch(repo, token, pullreq):
144157
print "loading pull request info for request %s..." % (pullreq)
145158
print
146159
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+
147166
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)
150178
data = response.read()
151179
if (data == ''):
152180
print "failed to speak with github."

0 commit comments

Comments
 (0)