repopick: support auth'ing to gerrit and picking drafts
* Use requests if installed. If not fall back to urllib. This is done because users may or may not have requests installed and requiring them to do so for simple http stuff isn't really reasonable. * If requests is installed and a .gerritrc file exists in the user's HOME directory, try to get credentials out of it for the given gerrit instance. If auth for the correct gerrit instance exists, use it to auth to gerrit. If no .gerritrc exists, just use requests with no auth. Example ~/.gerritrc entry: review.lineageos.org|invisiblek|httppasswordhere Change-Id: I95be26d51bfd31b53f3613e8dbfb7bba46324571
This commit is contained in:
@@ -31,6 +31,9 @@ import argparse
|
|||||||
import textwrap
|
import textwrap
|
||||||
from xml.etree import ElementTree
|
from xml.etree import ElementTree
|
||||||
|
|
||||||
|
try:
|
||||||
|
import requests
|
||||||
|
except ImportError:
|
||||||
try:
|
try:
|
||||||
# For python3
|
# For python3
|
||||||
import urllib.error
|
import urllib.error
|
||||||
@@ -100,7 +103,25 @@ def fetch_query_via_ssh(remote_url, query):
|
|||||||
|
|
||||||
|
|
||||||
def fetch_query_via_http(remote_url, query):
|
def fetch_query_via_http(remote_url, query):
|
||||||
|
if "requests" in sys.modules:
|
||||||
|
auth = None
|
||||||
|
if os.path.isfile(os.getenv("HOME") + "/.gerritrc"):
|
||||||
|
f = open(os.getenv("HOME") + "/.gerritrc", "r")
|
||||||
|
for line in f:
|
||||||
|
parts = line.rstrip().split("|")
|
||||||
|
if parts[0] in remote_url:
|
||||||
|
auth = requests.auth.HTTPBasicAuth(username=parts[1], password=parts[2])
|
||||||
|
statusCode = '-1'
|
||||||
|
if auth:
|
||||||
|
url = '{0}/a/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS'.format(remote_url, query)
|
||||||
|
data = requests.get(url, auth=auth)
|
||||||
|
statusCode = str(data.status_code)
|
||||||
|
if statusCode != '200':
|
||||||
|
#They didn't get good authorization or data, Let's try the old way
|
||||||
|
url = '{0}/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS'.format(remote_url, query)
|
||||||
|
data = requests.get(url)
|
||||||
|
reviews = json.loads(data.text[5:])
|
||||||
|
else:
|
||||||
"""Given a query, fetch the change numbers via http"""
|
"""Given a query, fetch the change numbers via http"""
|
||||||
url = '{0}/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS'.format(remote_url, query)
|
url = '{0}/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS'.format(remote_url, query)
|
||||||
data = urllib.request.urlopen(url).read().decode('utf-8')
|
data = urllib.request.urlopen(url).read().decode('utf-8')
|
||||||
|
Reference in New Issue
Block a user