Skip to content

Commit

Permalink
On November 13, 2020 username and password authentication to the REST…
Browse files Browse the repository at this point in the history
… API and the OAuth Authorizations API were deprecated and no longer work.
  • Loading branch information
inacionery authored and brianchandotcom committed Jan 5, 2021
1 parent 1956636 commit 1a524d7
Showing 1 changed file with 18 additions and 51 deletions.
69 changes: 18 additions & 51 deletions git-pull-request/git-pull-request.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@
MAP_RESPONSE = {}


def authorize_request(req, token=None, auth_type="token"):
def authorize_request(req, token=None):
"""Add the Authorize header to the request"""

if token == None:
token = auth_token

req.add_header("Authorization", "%s %s" % (auth_type, token))
req.add_header("Authorization", "token %s" % (token))


def build_branch_name(pull_request):
Expand Down Expand Up @@ -1490,13 +1490,13 @@ def get_work_dir():
return _work_dir


def github_json_request(url, params=None, authenticate=True):
data = json.loads(github_request(url, params, authenticate))
def github_json_request(url, params=None):
data = json.loads(github_request(url, params))

return data


def github_request(url, params=None, authenticate=True):
def github_request(url, params=None, token=None):
if params is not None:
encode_data = params

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

if authenticate == "basic":
passwd = getpass.getpass("Github password: ").strip()

auth_string = base64.encodestring("%s:%s" % (auth_username, passwd)).strip()

authorize_request(req, auth_string, "Basic")
elif authenticate == True:
authorize_request(req)
authorize_request(req, token)

if DEBUG:
print url
Expand All @@ -1524,15 +1517,12 @@ def github_request(url, params=None, authenticate=True):
try:
response = urllib2.urlopen(req)
except urllib2.URLError, msg:
if authenticate and msg.code == 401 and auth_token:
print ""
print color_text(
'Could not authorize you to connect with Github. Try running "git config --global --unset github.oauth-token" and running your command again to reauthenticate.',
"error",
if msg.code == 401 and auth_token:
raise UserWarning(
'Could not authorize you to connect with Github. Try running "git config --global --unset github.oauth-token" and running your command again to reauthenticate.'
)
print ""

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

data = response.read()

Expand Down Expand Up @@ -1664,7 +1654,7 @@ def main():

global users, DEFAULT_USERNAME
global _work_dir
global auth_username, auth_token
global auth_token

DEBUG = options["debug-mode"]

Expand Down Expand Up @@ -1724,40 +1714,17 @@ def main():
elif o == "--force-color":
FORCE_COLOR = True

if len(username) == 0:
username = raw_input("Github username: ").strip()
os.system("git config --global github.user %s" % username)

auth_username = username

if len(auth_token) == 0:
# Get a list of the current authorized apps and check if we already have a token
current_oauth_list = github_json_request(
"https://api.github.com/authorizations", None, "basic"
)
oauth_token = None

for cur in current_oauth_list:
if cur["note"] == SCRIPT_NOTE:
oauth_token = cur["token"]

# If we don't have a token, let's create one
if not oauth_token:
oauth_data = github_json_request(
"https://api.github.com/authorizations",
'{"scopes": ["repo"],"note": "%s"}' % SCRIPT_NOTE,
"basic",
)
token = getpass.getpass("Github token: ").strip()

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

if oauth_token:
auth_token = oauth_token
os.system("git config --global github.oauth-token %s" % oauth_token)
else:
raise UserWarning("Could not authenticate you with Github")
auth_token = token

os.system("git config --global github.oauth-token %s" % auth_token)

# get repo name from git config
# get repo name from git config
if repo_name is None or repo_name == "":
repo_name = get_default_repo_name()

Expand Down

0 comments on commit 1a524d7

Please sign in to comment.