mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
gitweb: Allow to import from a remote repository
- Allow to enter either name or URL when creating repository - Validate a repository URL, allow only http and https schemes - Set autocomplete off on the name/URL field because URL may contain username:password - Check whether the repository actually exists before cloning - Show progress info while cloning - Actions script: new subcommand check-repo-exists and new arguments for the create-repo: --url, --prepare-only and --skip-prepare - Add test for invalid URLs Closes #1670 Signed-off-by: Veiko Aasa <veiko17@disroot.org> [sunil: Fix validating repo name in edit form] [sunil: Don't pipe stdin of clone process, it may lead to a hang] [sunil: Always run clone process with 'C' locale since we are parsing output] [sunil: Cosmetic changes] Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
1 parent
ca0c991562
commit
c99b33b40e
6 files changed
+378
-88
No files matched your search
@@ -19,37 +19,65 @@ Django form for configuring Gitweb.
|
||||
"""
|
||||
|
||||
import re
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import URLValidator
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth.modules import gitweb
|
||||
|
||||
|
||||
def validate_repository(name):
|
||||
"""Validate a Git repository name."""
|
||||
|
||||
if not re.match(r'^[a-zA-Z0-9-._]+$', name):
|
||||
raise ValidationError(_('Invalid repository name.'))
|
||||
|
||||
if name.startswith(('-', '.')):
|
||||
raise ValidationError(_('Invalid repository name.'))
|
||||
|
||||
if name.endswith('.git.git'):
|
||||
raise ValidationError(_('Invalid repository name.'))
|
||||
def get_name_from_url(url):
|
||||
"""Get a repository name from URL"""
|
||||
return urlparse(url).path.split('/')[-1]
|
||||
|
||||
|
||||
class EditRepoForm(forms.Form):
|
||||
def is_repo_url(url):
|
||||
"""Check if URL is valid."""
|
||||
try:
|
||||
RepositoryValidator(input_should_be='url')(url)
|
||||
except ValidationError:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class RepositoryValidator:
|
||||
input_should_be = 'name'
|
||||
|
||||
def __init__(self, input_should_be=None):
|
||||
if input_should_be is not None:
|
||||
self.input_should_be = input_should_be
|
||||
|
||||
def __call__(self, value):
|
||||
"""Validate that the input is a correct repository name or URL"""
|
||||
if self.input_should_be in ('url', 'url_or_name'):
|
||||
try:
|
||||
URLValidator(schemes=['http', 'https'],
|
||||
message=_('Invalid repository URL.'))(value)
|
||||
except ValidationError:
|
||||
if self.input_should_be == 'url':
|
||||
raise
|
||||
else:
|
||||
value = get_name_from_url(value)
|
||||
|
||||
if (not re.match(r'^[a-zA-Z0-9-._]+$', value)) \
|
||||
or value.startswith(('-', '.')) \
|
||||
or value.endswith('.git.git'):
|
||||
raise ValidationError(_('Invalid repository name.'), 'invalid')
|
||||
|
||||
|
||||
class CreateRepoForm(forms.Form):
|
||||
"""Form to create and edit a new repository."""
|
||||
|
||||
name = forms.CharField(
|
||||
label=_('Name of the repository'),
|
||||
strip=True,
|
||||
validators=[validate_repository],
|
||||
help_text=_(
|
||||
'An alpha-numeric string that uniquely identifies a repository.'),
|
||||
)
|
||||
label=_(
|
||||
'Name of a new repository or URL to import an existing repository.'
|
||||
), strip=True,
|
||||
validators=[RepositoryValidator(input_should_be='url_or_name')],
|
||||
widget=forms.TextInput(attrs={'autocomplete': 'off'}))
|
||||
|
||||
description = forms.CharField(
|
||||
label=_('Description of the repository'), strip=True, required=False,
|
||||
@@ -68,6 +96,40 @@ class EditRepoForm(forms.Form):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['name'].widget.attrs.update({'autofocus': 'autofocus'})
|
||||
|
||||
def clean_name(self):
|
||||
"""Check if the name is valid."""
|
||||
name = self.cleaned_data['name']
|
||||
|
||||
repo_name = name
|
||||
if is_repo_url(name):
|
||||
repo_name = get_name_from_url(name)
|
||||
|
||||
if repo_name.endswith('.git'):
|
||||
repo_name = repo_name[:-4]
|
||||
|
||||
for repo in gitweb.app.get_repo_list():
|
||||
if repo_name == repo['name']:
|
||||
raise ValidationError(
|
||||
_('A repository with this name already exists.'))
|
||||
|
||||
if is_repo_url(name):
|
||||
if not gitweb.repo_exists(name):
|
||||
raise ValidationError('Remote repository is not available.')
|
||||
|
||||
return name
|
||||
|
||||
|
||||
class EditRepoForm(CreateRepoForm):
|
||||
"""Form to create and edit a new repository."""
|
||||
|
||||
name = forms.CharField(
|
||||
label=_('Name of the repository'),
|
||||
strip=True,
|
||||
validators=[RepositoryValidator()],
|
||||
help_text=_(
|
||||
'An alpha-numeric string that uniquely identifies a repository.'),
|
||||
)
|
||||
|
||||
def clean_name(self):
|
||||
"""Check if the name is valid."""
|
||||
name = self.cleaned_data['name']
|
||||
|
||||
Reference in new issue
Block a user