Improve check that arg is a yunohost repo url

This commit is contained in:
Alexandre Aubin 2021-10-01 16:13:57 +02:00
parent 2bda85c800
commit 171b3b25d9
2 changed files with 52 additions and 3 deletions

View file

@ -69,6 +69,10 @@ re_app_instance_name = re.compile(
r"^(?P<appid>[\w-]+?)(__(?P<appinstancenb>[1-9][0-9]*))?$" r"^(?P<appid>[\w-]+?)(__(?P<appinstancenb>[1-9][0-9]*))?$"
) )
APP_REPO_URL = re.compile(
r"^https://[a-zA-Z0-9-_.]+/[a-zA-Z0-9-_./]+/[a-zA-Z0-9-_.]+_ynh(/?(-/)?tree/[a-zA-Z0-9-_]+)?(\.git)?/?$"
)
APP_FILES_TO_COPY = [ APP_FILES_TO_COPY = [
"manifest.json", "manifest.json",
"manifest.toml", "manifest.toml",
@ -1971,13 +1975,24 @@ def _set_default_ask_questions(arguments):
return arguments return arguments
def _is_app_repo_url(string: str) -> bool:
string = string.strip()
# Dummy test for ssh-based stuff ... should probably be improved somehow
if '@' in string:
return True
return APP_REPO_URL.match(string)
def _app_quality(app: str) -> str: def _app_quality(app: str) -> str:
""" """
app may in fact be an app name, an url, or a path app may in fact be an app name, an url, or a path
""" """
raw_app_catalog = _load_apps_catalog()["apps"] raw_app_catalog = _load_apps_catalog()["apps"]
if app in raw_app_catalog or ("@" in app) or ("http://" in app) or ("https://" in app): if app in raw_app_catalog or _is_app_repo_url(app):
# If we got an app name directly (e.g. just "wordpress"), we gonna test this name # If we got an app name directly (e.g. just "wordpress"), we gonna test this name
if app in raw_app_catalog: if app in raw_app_catalog:
@ -2027,10 +2042,14 @@ def _extract_app(src: str) -> Tuple[Dict, str]:
revision = str(app_info["git"]["revision"]) revision = str(app_info["git"]["revision"])
return _extract_app_from_gitrepo(url, branch, revision, app_info) return _extract_app_from_gitrepo(url, branch, revision, app_info)
# App is a git repo url # App is a git repo url
elif ("@" in src) or ("http://" in src) or ("https://" in src): elif _is_app_repo_url(src):
url = src url = src
branch = "master" branch = "master"
revision = "HEAD" revision = "HEAD"
# gitlab urls may look like 'https://domain/org/group/repo/-/tree/testing'
# compated to github urls looking like 'https://domain/org/repo/tree/testing'
if '/-/' in url:
url = url.replace('/-/', '/')
if "/tree/" in url: if "/tree/" in url:
url, branch = url.split("/tree/", 1) url, branch = url.split("/tree/", 1)
return _extract_app_from_gitrepo(url, branch, revision, {}) return _extract_app_from_gitrepo(url, branch, revision, {})
@ -2038,6 +2057,8 @@ def _extract_app(src: str) -> Tuple[Dict, str]:
elif os.path.exists(src): elif os.path.exists(src):
return _extract_app_from_folder(src) return _extract_app_from_folder(src)
else: else:
if "http://" in src or "https://" in src:
logger.error(f"{src} is not a valid app url: app url are expected to look like https://domain.tld/path/to/repo_ynh")
raise YunohostValidationError("app_unknown") raise YunohostValidationError("app_unknown")

View file

@ -4,7 +4,7 @@ import os
from .conftest import get_test_apps_dir from .conftest import get_test_apps_dir
from yunohost.utils.error import YunohostError from yunohost.utils.error import YunohostError
from yunohost.app import app_install, app_remove from yunohost.app import app_install, app_remove, _is_app_repo_url
from yunohost.domain import _get_maindomain, domain_url_available from yunohost.domain import _get_maindomain, domain_url_available
from yunohost.permission import _validate_and_sanitize_permission_url from yunohost.permission import _validate_and_sanitize_permission_url
@ -28,6 +28,34 @@ def teardown_function(function):
pass pass
def test_repo_url_definition():
assert _is_app_repo_url("https://github.com/YunoHost-Apps/foobar123_ynh")
assert _is_app_repo_url("https://github.com/YunoHost-Apps/foobar123_ynh/")
assert _is_app_repo_url("https://github.com/YunoHost-Apps/foobar123_ynh.git")
assert _is_app_repo_url("https://github.com/YunoHost-Apps/foobar123_ynh/tree/testing")
assert _is_app_repo_url("https://github.com/YunoHost-Apps/foobar123_ynh/tree/testing/")
assert _is_app_repo_url("https://github.com/YunoHost-Apps/foo-bar-123_ynh")
assert _is_app_repo_url("https://github.com/YunoHost-Apps/foo_bar_123_ynh")
assert _is_app_repo_url("https://github.com/YunoHost-Apps/FooBar123_ynh")
assert _is_app_repo_url("https://github.com/labriqueinternet/vpnclient_ynh")
assert _is_app_repo_url("https://framagit.org/YunoHost/apps/nodebb_ynh")
assert _is_app_repo_url("https://framagit.org/YunoHost/apps/nodebb_ynh/-/tree/testing")
assert _is_app_repo_url("https://gitlab.com/yunohost-apps/foobar_ynh")
assert _is_app_repo_url("https://code.antopie.org/miraty/qr_ynh")
assert _is_app_repo_url("https://gitlab.domainepublic.net/Neutrinet/neutrinet_ynh/-/tree/unstable")
assert _is_app_repo_url("git@github.com:YunoHost-Apps/foobar_ynh.git")
assert not _is_app_repo_url("github.com/YunoHost-Apps/foobar_ynh")
assert not _is_app_repo_url("http://github.com/YunoHost-Apps/foobar_ynh")
assert not _is_app_repo_url("https://github.com/YunoHost-Apps/foobar_wat")
assert not _is_app_repo_url("https://github.com/YunoHost-Apps/foobar_ynh_wat")
assert not _is_app_repo_url("https://github.com/YunoHost-Apps/foobar/tree/testing")
assert not _is_app_repo_url("https://github.com/YunoHost-Apps/foobar_ynh_wat/tree/testing")
assert not _is_app_repo_url("https://framagit.org/YunoHost/apps/")
assert not _is_app_repo_url("https://framagit.org/YunoHost/apps/pwet")
assert not _is_app_repo_url("https://framagit.org/YunoHost/apps/pwet_foo")
def test_urlavailable(): def test_urlavailable():
# Except the maindomain/macnuggets to be available # Except the maindomain/macnuggets to be available