Skip to content

Commit 017680e

Browse files
committed
On November 13, 2020 username and password authentication to the REST API and the OAuth Authorizations API were deprecated and no longer work.
1 parent f3e7a5a commit 017680e

File tree

1 file changed

+18
-51
lines changed

1 file changed

+18
-51
lines changed

git-pull-request/git-pull-request.py

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@
218218
MAP_RESPONSE = {}
219219

220220

221-
def authorize_request(req, token=None, auth_type="token"):
221+
def authorize_request(req, token=None):
222222
"""Add the Authorize header to the request"""
223223

224224
if token == None:
225225
token = auth_token
226226

227-
req.add_header("Authorization", "%s %s" % (auth_type, token))
227+
req.add_header("Authorization", "token %s" % (token))
228228

229229

230230
def build_branch_name(pull_request):
@@ -1490,13 +1490,13 @@ def get_work_dir():
14901490
return _work_dir
14911491

14921492

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))
14951495

14961496
return data
14971497

14981498

1499-
def github_request(url, params=None, authenticate=True):
1499+
def github_request(url, params=None, token=None):
15001500
if params is not None:
15011501
encode_data = params
15021502

@@ -1507,14 +1507,7 @@ def github_request(url, params=None, authenticate=True):
15071507
else:
15081508
req = urllib2.Request(url)
15091509

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)
15181511

15191512
if DEBUG:
15201513
print url
@@ -1524,15 +1517,12 @@ def github_request(url, params=None, authenticate=True):
15241517
try:
15251518
response = urllib2.urlopen(req)
15261519
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.'
15321523
)
1533-
print ""
15341524

1535-
raise UserWarning("Error communicating with github: \n%s\n%s" % (url, msg))
1525+
raise UserWarning("Could not authorize you to connect with Github.")
15361526

15371527
data = response.read()
15381528

@@ -1664,7 +1654,7 @@ def main():
16641654

16651655
global users, DEFAULT_USERNAME
16661656
global _work_dir
1667-
global auth_username, auth_token
1657+
global auth_token
16681658

16691659
DEBUG = options["debug-mode"]
16701660

@@ -1724,40 +1714,17 @@ def main():
17241714
elif o == "--force-color":
17251715
FORCE_COLOR = True
17261716

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-
17331717
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()
17511719

1752-
oauth_token = oauth_data["token"]
1720+
# check if the token is valid
1721+
github_request("https://api.github.com/user/repos", None, token)
17531722

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)
17591726

1760-
# get repo name from git config
1727+
# get repo name from git config
17611728
if repo_name is None or repo_name == "":
17621729
repo_name = get_default_repo_name()
17631730

0 commit comments

Comments
 (0)