Skip to content

Commit

Permalink
[IMP] Automatically shorten app name if it's too long
Browse files Browse the repository at this point in the history
  • Loading branch information
tarteo committed Oct 4, 2024
1 parent 0526fdb commit fd98060
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 316 deletions.
21 changes: 1 addition & 20 deletions argocd_deployer/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,10 @@ def _compute_modules(self):
)
]

@api.model
def find_next_available_name(self, name):
"""
Find a name which is available based on name (e.g. greg2)
@param app_set: application set
@param name: a name
@return: first available name
"""
if not self.search([("name", "=", name)], count=True):
return name
i = 0
while self.search(
[("name", "=", name + str(i))],
count=True,
):
i += 1
return name + str(i)

@api.constrains("name")
def _constrain_name(self):
# We actually need to also do this check if `namespace_prefix_id.name` changes, but it never does in practice
# The namespace_prefix is not necessarily part of the app name depends on the application.set.template
# FIXME: The namespace_prefix is not necessarily part of the app name depends on the application.set.template
prefix = self.application_set_id.namespace_prefix_id.name
if not re.match(
"^[a-z0-9-]{1,53}$", prefix + self.name
Expand Down
21 changes: 18 additions & 3 deletions argocd_sale/models/sale_subscription_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@ class SubscriptionLine(models.Model):
)

def _to_application_name(self):
"""
@return: a unique yet human-readable name to use as application name
"""
self.ensure_one()
replacements = {" ": "-", ".": "", "&": "-"}
# It's not possible to have more than one application linked to a
# subscription line because of the sql constraint.
# Let's assume that here.
product = self.product_id
partner = self.sale_subscription_id.partner_id.commercial_partner_id
name = "-".join([partner.display_name, product.default_code or product.name])
# Add id to the end to easily ensure uniqueness
name = "-".join(
[partner.display_name, product.default_code or product.name, str(self.id)]
)
name = name.strip().lower()
for replace in replacements:
name = name.replace(replace, replacements[replace])
return "".join(c for c in name if c.isalnum() or c == "-")
app_name = "".join(c for c in name if c.isalnum() or c == "-")
while "--" in app_name: # Never 2 dashes after each other
app_name = app_name.replace("--", "-")
# FIXME: The namespace_prefix is not necessarily part of the app name depends on the application.set.template
prefix = self.product_id.application_set_id.namespace_prefix_id.name
full_app_name = prefix + app_name
while len(full_app_name) > 53 or app_name[0] == "-":
app_name = app_name[1:]
full_app_name = prefix + app_name
return app_name

def write(self, vals):
to_redeploy = self.env["argocd.application"]
Expand Down Expand Up @@ -72,7 +87,7 @@ def _invoice_paid_hook(self):
if self.application_ids or not self.product_id.application_template_id:
return

name = application_sudo.find_next_available_name(self._to_application_name())
name = self._to_application_name()
application = application_sudo.create(
{
"name": name,
Expand Down
4 changes: 1 addition & 3 deletions argocd_sale/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from . import test_reseller
from . import test_grace_period
from . import test_invoice_to_application
from . import test_subscription
110 changes: 0 additions & 110 deletions argocd_sale/tests/test_grace_period.py

This file was deleted.

131 changes: 0 additions & 131 deletions argocd_sale/tests/test_invoice_to_application.py

This file was deleted.

49 changes: 0 additions & 49 deletions argocd_sale/tests/test_reseller.py

This file was deleted.

Loading

0 comments on commit fd98060

Please sign in to comment.