Add HTTP proxy support to git fetcher

This commit is contained in:
jeremystretch 2023-01-27 14:59:00 -05:00
parent ee35ea439e
commit e950701dee

View File

@ -5,6 +5,7 @@ import tempfile
from fnmatch import fnmatchcase from fnmatch import fnmatchcase
from urllib.parse import quote, urlunparse, urlparse from urllib.parse import quote, urlunparse, urlparse
from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
@ -98,6 +99,10 @@ class DataSource(ChangeLoggedModel):
def get_status_color(self): def get_status_color(self):
return DataSourceStatusChoices.colors.get(self.status) return DataSourceStatusChoices.colors.get(self.status)
@property
def url_scheme(self):
return urlparse(self.url).scheme.lower()
@property @property
def ready_for_sync(self): def ready_for_sync(self):
return self.enabled and self.status not in ( return self.enabled and self.status not in (
@ -108,8 +113,7 @@ class DataSource(ChangeLoggedModel):
def clean(self): def clean(self):
# Ensure URL scheme matches selected type # Ensure URL scheme matches selected type
url_scheme = urlparse(self.url) if self.type == DataSourceTypeChoices.LOCAL and self.url_scheme not in ('file', ''):
if self.type == DataSourceTypeChoices.LOCAL and url_scheme not in ('file', ''):
raise ValidationError({ raise ValidationError({
'url': f"URLs for local sources must start with file:// (or omit the scheme)" 'url': f"URLs for local sources must start with file:// (or omit the scheme)"
}) })
@ -232,9 +236,14 @@ class DataSource(ChangeLoggedModel):
args.extend(['--branch', self.git_branch]) args.extend(['--branch', self.git_branch])
args.extend([url, local_path.name]) args.extend([url, local_path.name])
# Prep environment variables
env_vars = {}
if settings.HTTP_PROXIES and self.url_scheme in ('http', 'https'):
env_vars['http_proxy'] = settings.HTTP_PROXIES.get(self.url_scheme)
logger.debug(f"Cloning git repo: {' '.join(args)}") logger.debug(f"Cloning git repo: {' '.join(args)}")
try: try:
subprocess.run(args, check=True, capture_output=True) subprocess.run(args, check=True, capture_output=True, env=env_vars)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
raise SyncError( raise SyncError(
f"Fetching remote data failed: {e.stderr}" f"Fetching remote data failed: {e.stderr}"