218
218
MAP_RESPONSE = {}
219
219
220
220
221
- def authorize_request (req , token = None , auth_type = "token" ):
221
+ def authorize_request (req , token = None ):
222
222
"""Add the Authorize header to the request"""
223
223
224
224
if token == None :
225
225
token = auth_token
226
226
227
- req .add_header ("Authorization" , "%s %s" % (auth_type , token ))
227
+ req .add_header ("Authorization" , "token %s" % (token ))
228
228
229
229
230
230
def build_branch_name (pull_request ):
@@ -1490,13 +1490,13 @@ def get_work_dir():
1490
1490
return _work_dir
1491
1491
1492
1492
1493
- def github_json_request (url , params = None , authenticate = True ):
1494
- data = json .loads (github_request (url , params , authenticate ))
1493
+ def github_json_request (url , params = None ):
1494
+ data = json .loads (github_request (url , params ))
1495
1495
1496
1496
return data
1497
1497
1498
1498
1499
- def github_request (url , params = None , authenticate = True ):
1499
+ def github_request (url , params = None , token = None ):
1500
1500
if params is not None :
1501
1501
encode_data = params
1502
1502
@@ -1507,14 +1507,7 @@ def github_request(url, params=None, authenticate=True):
1507
1507
else :
1508
1508
req = urllib2 .Request (url )
1509
1509
1510
- if authenticate == "basic" :
1511
- passwd = getpass .getpass ("Github password: " ).strip ()
1512
-
1513
- auth_string = base64 .encodestring ("%s:%s" % (auth_username , passwd )).strip ()
1514
-
1515
- authorize_request (req , auth_string , "Basic" )
1516
- elif authenticate == True :
1517
- authorize_request (req )
1510
+ authorize_request (req , token )
1518
1511
1519
1512
if DEBUG :
1520
1513
print url
@@ -1524,15 +1517,12 @@ def github_request(url, params=None, authenticate=True):
1524
1517
try :
1525
1518
response = urllib2 .urlopen (req )
1526
1519
except urllib2 .URLError , msg :
1527
- if authenticate and msg .code == 401 and auth_token :
1528
- print ""
1529
- print color_text (
1530
- 'Could not authorize you to connect with Github. Try running "git config --global --unset github.oauth-token" and running your command again to reauthenticate.' ,
1531
- "error" ,
1520
+ if msg .code == 401 and auth_token :
1521
+ raise UserWarning (
1522
+ 'Could not authorize you to connect with Github. Try running "git config --global --unset github.oauth-token" and running your command again to reauthenticate.'
1532
1523
)
1533
- print ""
1534
1524
1535
- raise UserWarning ("Error communicating with github: \n %s \n %s" % ( url , msg ) )
1525
+ raise UserWarning ("Could not authorize you to connect with Github." )
1536
1526
1537
1527
data = response .read ()
1538
1528
@@ -1664,7 +1654,7 @@ def main():
1664
1654
1665
1655
global users , DEFAULT_USERNAME
1666
1656
global _work_dir
1667
- global auth_username , auth_token
1657
+ global auth_token
1668
1658
1669
1659
DEBUG = options ["debug-mode" ]
1670
1660
@@ -1724,40 +1714,17 @@ def main():
1724
1714
elif o == "--force-color" :
1725
1715
FORCE_COLOR = True
1726
1716
1727
- if len (username ) == 0 :
1728
- username = raw_input ("Github username: " ).strip ()
1729
- os .system ("git config --global github.user %s" % username )
1730
-
1731
- auth_username = username
1732
-
1733
1717
if len (auth_token ) == 0 :
1734
- # Get a list of the current authorized apps and check if we already have a token
1735
- current_oauth_list = github_json_request (
1736
- "https://api.github.com/authorizations" , None , "basic"
1737
- )
1738
- oauth_token = None
1739
-
1740
- for cur in current_oauth_list :
1741
- if cur ["note" ] == SCRIPT_NOTE :
1742
- oauth_token = cur ["token" ]
1743
-
1744
- # If we don't have a token, let's create one
1745
- if not oauth_token :
1746
- oauth_data = github_json_request (
1747
- "https://api.github.com/authorizations" ,
1748
- '{"scopes": ["repo"],"note": "%s"}' % SCRIPT_NOTE ,
1749
- "basic" ,
1750
- )
1718
+ token = getpass .getpass ("Github token: " ).strip ()
1751
1719
1752
- oauth_token = oauth_data ["token" ]
1720
+ # check if the token is valid
1721
+ github_request ("https://api.github.com/user/repos" , None , token )
1753
1722
1754
- if oauth_token :
1755
- auth_token = oauth_token
1756
- os .system ("git config --global github.oauth-token %s" % oauth_token )
1757
- else :
1758
- raise UserWarning ("Could not authenticate you with Github" )
1723
+ auth_token = token
1724
+
1725
+ os .system ("git config --global github.oauth-token %s" % auth_token )
1759
1726
1760
- # get repo name from git config
1727
+ # get repo name from git config
1761
1728
if repo_name is None or repo_name == "" :
1762
1729
repo_name = get_default_repo_name ()
1763
1730
0 commit comments