Fix import_full_config for adoption configs (#4197)

* Fix git raw url

* Fix setting full config query param

* Force dashboard import urls to have a branch or tag reference for full import
This commit is contained in:
Jesse Hills 2022-12-16 19:46:56 +13:00
parent 4e9606d2e0
commit 1824c8131e
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
2 changed files with 23 additions and 10 deletions

View File

@ -4,7 +4,7 @@ import requests
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components.packages import validate_source_shorthand from esphome.components.packages import validate_source_shorthand
from esphome.const import CONF_WIFI from esphome.const import CONF_WIFI, CONF_REF
from esphome.wizard import wizard_file from esphome.wizard import wizard_file
from esphome.yaml_util import dump from esphome.yaml_util import dump
from esphome import git from esphome import git
@ -21,19 +21,32 @@ CODEOWNERS = ["@esphome/core"]
def validate_import_url(value): def validate_import_url(value):
value = cv.string_strict(value) value = cv.string_strict(value)
value = cv.Length(max=255)(value) value = cv.Length(max=255)(value)
# ignore result, only check if it's a valid shorthand
validate_source_shorthand(value) validate_source_shorthand(value)
return value return value
def validate_full_url(config):
if not config[CONF_IMPORT_FULL_CONFIG]:
return config
source = validate_source_shorthand(config[CONF_PACKAGE_IMPORT_URL])
if CONF_REF not in source:
raise cv.Invalid(
"Must specify a ref (branch or tag) to import from when importing full config"
)
return config
CONF_PACKAGE_IMPORT_URL = "package_import_url" CONF_PACKAGE_IMPORT_URL = "package_import_url"
CONF_IMPORT_FULL_CONFIG = "import_full_config" CONF_IMPORT_FULL_CONFIG = "import_full_config"
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.All(
{ cv.Schema(
cv.Required(CONF_PACKAGE_IMPORT_URL): validate_import_url, {
cv.Optional(CONF_IMPORT_FULL_CONFIG, default=False): cv.boolean, cv.Required(CONF_PACKAGE_IMPORT_URL): validate_import_url,
} cv.Optional(CONF_IMPORT_FULL_CONFIG, default=False): cv.boolean,
}
),
validate_full_url,
) )
WIFI_CONFIG = """ WIFI_CONFIG = """
@ -49,7 +62,7 @@ async def to_code(config):
url = config[CONF_PACKAGE_IMPORT_URL] url = config[CONF_PACKAGE_IMPORT_URL]
if config[CONF_IMPORT_FULL_CONFIG]: if config[CONF_IMPORT_FULL_CONFIG]:
url += "?full_config" url += "?full_config"
cg.add(dashboard_import_ns.set_package_import_url(config[CONF_PACKAGE_IMPORT_URL])) cg.add(dashboard_import_ns.set_package_import_url(url))
def import_config( def import_config(

View File

@ -129,9 +129,9 @@ class GitFile:
def raw_url(self) -> str: def raw_url(self) -> str:
if self.ref is None: if self.ref is None:
raise ValueError("URL has no ref") raise ValueError("URL has no ref")
if self.domain == "github": if self.domain == "github.com":
return f"https://raw.githubusercontent.com/{self.owner}/{self.repo}/{self.ref}/{self.filename}" return f"https://raw.githubusercontent.com/{self.owner}/{self.repo}/{self.ref}/{self.filename}"
if self.domain == "gitlab": if self.domain == "gitlab.com":
return f"https://gitlab.com/{self.owner}/{self.repo}/-/raw/{self.ref}/{self.filename}" return f"https://gitlab.com/{self.owner}/{self.repo}/-/raw/{self.ref}/{self.filename}"
raise NotImplementedError(f"Git domain {self.domain} not supported") raise NotImplementedError(f"Git domain {self.domain} not supported")