Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(AppApi): Advanced Deploy Options for ExApps #1510

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions nextcloudappstore/api/v1/release/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from nextcloudappstore.core.facades import any_match
from nextcloudappstore.core.models import (
App,
AppApiEnvironmentVariable,
AppApiReleaseApiScope,
AppApiReleaseDeployMethod,
AppAuthor,
Expand Down Expand Up @@ -194,6 +195,15 @@ def import_data(self, key: str, value: Any, obj: Any) -> None:
)
for scope in value.get("scopes", []):
AppApiReleaseApiScope.objects.get_or_create(app_release=obj, scope_name=scope["value"])
for env_var_struct in value.get("environment_variables", []):
env_var = env_var_struct["variable"]
AppApiEnvironmentVariable.objects.get_or_create(
app_release=obj,
env_name=env_var["name"],
display_name=env_var["display_name"],
description=env_var.get("description", ""),
default=env_var.get("default", ""),
)


class AppReleaseImporter(Importer):
Expand Down
16 changes: 16 additions & 0 deletions nextcloudappstore/api/v1/release/info.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,27 @@
</xs:sequence>
</xs:complexType>

<xs:complexType name="environment-variable">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="display-name" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="default" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="environment-variables">
<xs:sequence>
<xs:element name="variable" type="environment-variable" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="external-app">
<xs:sequence>
<xs:element name="docker-install" type="docker-install" minOccurs="0" maxOccurs="1"/>
<xs:element name="scopes" type="scopes" minOccurs="0" maxOccurs="1"/>
<xs:element name="system" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
<xs:element name="environment-variables" type="environment-variables" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>

Expand Down
20 changes: 20 additions & 0 deletions nextcloudappstore/api/v1/release/info.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@
<system type="boolean">
<xsl:value-of select="system"/>
</system>
<environment-variables type="list">
<xsl:for-each select="environment-variables/variable">
<variable>
<name>
<xsl:value-of select="name"/>
</name>
<display-name>
<xsl:value-of select="display-name"/>
</display-name>
<description>
<xsl:value-of select="description"/>
</description>
<xsl:if test="default">
<default>
<xsl:value-of select="default"/>
</default>
</xsl:if>
</variable>
</xsl:for-each>
</environment-variables>
</external-app>
</xsl:template>

Expand Down
16 changes: 16 additions & 0 deletions nextcloudappstore/api/v1/release/pre-info.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<xsl:apply-templates select="docker-install"/>
<xsl:copy-of select="scopes"/>
<xsl:copy-of select="system"/>
<xsl:apply-templates select="environment-variables"/>
</external-app>
</xsl:template>

Expand All @@ -167,4 +168,19 @@
</docker-install>
</xsl:template>

<xsl:template match="environment-variables">
<environment-variables>
<xsl:apply-templates select="variable"/>
</environment-variables>
</xsl:template>

<xsl:template match="variable">
<variable>
<xsl:copy-of select="name"/>
<xsl:copy-of select="display-name"/>
<xsl:copy-of select="description"/>
<xsl:copy-of select="default"/>
</variable>
</xsl:template>

</xsl:stylesheet>
9 changes: 9 additions & 0 deletions nextcloudappstore/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from nextcloudappstore.core.models import (
App,
AppApiEnvironmentVariable,
AppApiReleaseApiScope,
AppApiReleaseDeployMethod,
AppAuthor,
Expand Down Expand Up @@ -128,9 +129,16 @@ class Meta:
fields = ("scope_name",)


class AppApiEnvironmentVariableSerializer(serializers.ModelSerializer):
class Meta:
model = AppApiEnvironmentVariable
fields = ("env_name", "display_name", "description", "default")


class AppApiAppReleaseSerializer(AppReleaseSerializer):
deploy_methods = DeployMethodSerializer(many=True, read_only=True)
api_scopes = ApiScopeSerializer(many=True, read_only=True)
environment_variables = AppApiEnvironmentVariableSerializer(many=True, read_only=True)

class Meta:
model = AppRelease
Expand All @@ -155,6 +163,7 @@ class Meta:
"aa_is_system",
"deploy_methods",
"api_scopes",
"environment_variables",
)


Expand Down
13 changes: 13 additions & 0 deletions nextcloudappstore/api/v1/tests/data/infoxmls/app_api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,18 @@
<value>TALK</value>
</scopes>
<system>true</system>
<environment-variables>
<variable>
<name>SOME_VALUE1</name>
<display-name>Display name 1</display-name>
<description>Some useful description of it</description>
</variable>
<variable>
<name>SOME_VALUE2</name>
<display-name>Display name 2</display-name>
<description>Example of second variable with the 'default' value</description>
<default>0</default>
</variable>
</environment-variables>
</external-app>
</info>
18 changes: 18 additions & 0 deletions nextcloudappstore/api/v1/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,23 @@ def test_appapi(self):
}
assert r_ex_app["scopes"] == [{"value": "FILES"}, {"value": "NOTIFICATIONS"}, {"value": "TALK"}]
assert r_ex_app["system"] == "true"
assert r_ex_app["environment_variables"] == [
{
"variable": {
"name": "SOME_VALUE1",
"display_name": "Display name 1",
"description": "Some useful description of it",
}
},
{
"variable": {
"name": "SOME_VALUE2",
"display_name": "Display name 2",
"description": "Example of second variable with the 'default' value",
"default": "0",
}
},
]

def test_appapi_minimal(self):
xml = self._get_contents("data/infoxmls/app_api_minimal.xml")
Expand All @@ -583,6 +600,7 @@ def test_appapi_minimal(self):
"registry": "ghcr.io",
}
assert r_ex_app["scopes"] == []
assert r_ex_app["environment_variables"] == []

def _get_contents(self, target):
path = self.get_path(target)
Expand Down
6 changes: 6 additions & 0 deletions nextcloudappstore/api/v1/tests/test_release_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ def test_import_minimal(self):
self.assertEqual(1, release.deploy_methods.count())
all_scopes = release.api_scopes.all()
self.assertEqual(0, len(all_scopes))
all_deploy_env_vars = release.environment_variables.all()
self.assertEqual(0, len(all_deploy_env_vars))

def test_full(self):
self._check_removed_translations()
Expand Down Expand Up @@ -281,6 +283,10 @@ def test_full(self):
self.assertEqual("FILES", all_scopes[0].scope_name)
self.assertEqual("NOTIFICATIONS", all_scopes[1].scope_name)
self.assertEqual("TALK", all_scopes[2].scope_name)
all_deploy_env_vars = release.environment_variables.all()
self.assertEqual(2, len(all_deploy_env_vars))
self.assertEqual("SOME_VALUE1", all_deploy_env_vars[0].env_name)
self.assertEqual("SOME_VALUE2", all_deploy_env_vars[1].env_name)

def test_release_no_update(self):
result = parse_app_metadata(self.min, self.config.info_schema, self.config.pre_info_xslt, self.config.info_xslt)
Expand Down
1 change: 1 addition & 0 deletions nextcloudappstore/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
*RELEASES_PREFETCH_LIST,
"releases__deploy_methods",
"releases__api_scopes",
"releases__environment_variables",
]


Expand Down
6 changes: 6 additions & 0 deletions nextcloudappstore/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from nextcloudappstore.core.models import (
App,
AppApiEnvironmentVariable,
AppApiReleaseApiScope,
AppApiReleaseDeployMethod,
AppAuthor,
Expand Down Expand Up @@ -163,3 +164,8 @@ class AppApiReleaseApiScopeAdmin(admin.ModelAdmin):
@admin.register(AppApiReleaseDeployMethod)
class AppApiReleaseDeployMethodAdmin(admin.ModelAdmin):
pass


@admin.register(AppApiEnvironmentVariable)
class AppApiEnvironmentVariable(admin.ModelAdmin):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.16 on 2024-10-17 08:32

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0033_donation'),
]

operations = [
migrations.CreateModel(
name='AppApiEnvironmentVariable',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('env_name', models.CharField(max_length=64, verbose_name='Environment Variable Name')),
('display_name', models.CharField(max_length=128, verbose_name='Display Name')),
('description', models.TextField(blank=True, verbose_name='Description')),
('default', models.CharField(blank=True, max_length=256, verbose_name='Default Value')),
('app_release', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='environment_variables', to='core.apprelease', verbose_name='App Release')),
],
options={
'verbose_name': 'AppAPI Release Environment Variable',
'verbose_name_plural': 'AppAPI Release Environment Variables',
'db_table': 'core_appapi_release_env_vars',
'unique_together': {('app_release', 'env_name')},
},
),
]
20 changes: 20 additions & 0 deletions nextcloudappstore/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,26 @@ class Meta:
verbose_name_plural = _("AppAPI release API Scopes")


class AppApiEnvironmentVariable(Model):
app_release = ForeignKey(
"AppRelease",
on_delete=CASCADE,
verbose_name=_("App Release"),
related_name="environment_variables",
db_index=True,
)
env_name = CharField(max_length=64, verbose_name=_("Environment Variable Name"))
display_name = CharField(max_length=128, verbose_name=_("Display Name"))
description = TextField(verbose_name=_("Description"), blank=True)
default = CharField(max_length=256, verbose_name=_("Default Value"), blank=True)

class Meta:
db_table = "core_appapi_release_env_vars"
verbose_name = _("AppAPI Release Environment Variable")
verbose_name_plural = _("AppAPI Release Environment Variables")
unique_together = (("app_release", "env_name"),)


class Screenshot(Model):
url = URLField(max_length=256, verbose_name=_("Image URL"))
small_thumbnail = URLField(max_length=256, verbose_name=_("Small thumbnail"), default="")
Expand Down
Loading