feat: returned the installed release

This commit is contained in:
Vlad Stan 2023-01-17 14:51:09 +02:00
parent 12060b67ad
commit 6b360b927c
5 changed files with 50 additions and 30 deletions

View File

@ -6,7 +6,6 @@ from uuid import uuid4
from lnbits import bolt11
from lnbits.db import COCKROACH, POSTGRES, Connection
from lnbits.extension_manger import ExtensionRelease, InstallableExtension
from lnbits.settings import AdminSettings, EditableSettings, SuperSettings, settings
from . import db
@ -122,9 +121,7 @@ async def delete_installed_extension(
)
async def get_installed_extension(
ext_id: str, conn: Optional[Connection] = None
) -> InstallableExtension:
async def get_installed_extension(ext_id: str, conn: Optional[Connection] = None):
row = await (conn or db).fetchone(
"SELECT * FROM installed_extensions WHERE id = ?",
(ext_id,),
@ -132,12 +129,7 @@ async def get_installed_extension(
if not row:
return None
data = dict(row)
meta = json.loads(data["meta"])
ext = InstallableExtension(**data)
if "installed_release" in meta:
ext.installed_release = ExtensionRelease(**meta["installed_release"])
return ext
return dict(row)
async def get_inactive_extensions(*, conn: Optional[Connection] = None) -> List[str]:

View File

@ -6,9 +6,8 @@ import time
from sqlite3 import Row
from typing import Callable, Dict, List, Optional
from ecdsa import SECP256k1, SigningKey
from fastapi import Query
from lnurl import encode as lnurl_encode
from ecdsa import SECP256k1, SigningKey # type: ignore
from lnurl import encode as lnurl_encode # type: ignore
from loguru import logger
from pydantic import BaseModel

View File

@ -726,15 +726,15 @@ async def api_install_extension(
data: CreateExtension, user: User = Depends(check_admin)
):
installed_release = await InstallableExtension.get_extension_release(
release = await InstallableExtension.get_extension_release(
data.ext_id, data.source_repo, data.archive
)
if not installed_release:
if not release:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Release not found"
)
ext_info = InstallableExtension(
id=data.ext_id, name=data.ext_id, installed_release=installed_release
id=data.ext_id, name=data.ext_id, installed_release=release
)
ext_info.download_archive()
@ -749,10 +749,10 @@ async def api_install_extension(
await add_installed_extension(
ext_id=data.ext_id,
version=installed_release.version,
version=release.version,
name=ext_info.name,
active=False,
meta={"installed_release": dict(installed_release)},
meta={"installed_release": dict(release)},
)
settings.lnbits_disabled_extensions += [data.ext_id]

View File

@ -113,6 +113,9 @@ async def extensions_install(
"latestRelease": dict(ext.latest_release)
if ext.latest_release
else None,
"installedRelease": dict(ext.installed_release)
if ext.installed_release
else None,
},
extension_list,
)

View File

@ -15,6 +15,7 @@ from loguru import logger
from pydantic import BaseModel
from starlette.types import ASGIApp, Receive, Scope, Send
from lnbits.core.crud import get_installed_extension
from lnbits.settings import settings
@ -253,12 +254,24 @@ class InstallableExtension(BaseModel):
shutil.rmtree(self.ext_upgrade_dir, True)
@classmethod
async def from_row(cls, data: dict) -> Optional["InstallableExtension"]:
if not data:
return None
meta = json.loads(data["meta"])
ext = InstallableExtension(**data)
if "installed_release" in meta:
ext.installed_release = ExtensionRelease(**meta["installed_release"])
return ext
@classmethod
async def from_repo(
cls, ext_id, org, repository
) -> Optional["InstallableExtension"]:
try:
# installed_release = await get_installed_extension(ext_id)
installed_release = await InstallableExtension.from_row(
await get_installed_extension(ext_id)
)
repo, latest_release, config = await fetch_github_repo_info(org, repository)
return InstallableExtension(
@ -268,6 +281,7 @@ class InstallableExtension(BaseModel):
version="0",
stars=repo["stargazers_count"],
icon_url=icon_to_github_url(org, config.get("tile")),
installed_release=installed_release,
latest_release=ExtensionRelease.from_github_release(
repo["html_url"], latest_release
),
@ -276,6 +290,28 @@ class InstallableExtension(BaseModel):
logger.warning(e)
return None
@classmethod
async def from_manifest(cls, e: dict) -> Optional["InstallableExtension"]:
try:
installed_ext = await InstallableExtension.from_row(
await get_installed_extension(e["id"])
)
return InstallableExtension(
id=e["id"],
name=e["name"],
archive=e["archive"],
hash=e["hash"],
short_description=e["shortDescription"],
icon=e["icon"],
installed_release=installed_ext.installed_release
if installed_ext
else None,
dependencies=e["dependencies"] if "dependencies" in e else [],
)
except Exception as e:
logger.warning(e)
return None
@classmethod # todo: remove
async def get_extension_info(
cls, ext_id: str, archive: str
@ -331,17 +367,7 @@ class InstallableExtension(BaseModel):
if e["id"] in extension_id_list:
continue
extension_list += [
InstallableExtension(
id=e["id"],
name=e["name"],
archive=e["archive"],
hash=e["hash"],
short_description=e["shortDescription"],
icon=e["icon"],
dependencies=e["dependencies"]
if "dependencies" in e
else [],
)
await InstallableExtension.from_manifest(e)
]
extension_id_list += [e["id"]]
except Exception as e: