Skip to content

Commit fd37e69

Browse files
authored
Merge branch 'main' into adesilva/import-fixes
2 parents 748d808 + 596653b commit fd37e69

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

playbooks/projects/base_projects/3_umbrellas.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ sample_umbrella_buf:
2828
{% for buf_child in range(25) %}
2929
{% set buf_child_name = fake.catch_phrase().title() %}
3030
- json:
31-
slug: {{ buf_child_name | lower | replace(" ", "-") }}
31+
slug: {{ slug_from_project_name(buf_child_name) }}
3232
name: {{ buf_child_name }} Project
3333
description: >-
3434
{{ buf_child_name }}
@@ -39,8 +39,8 @@ sample_umbrella_buf:
3939
parent_uid: !ref "sample_umbrella_buf.steps[0]._response.uid"
4040
legal_entity_name: {{ buf_child_name }} Project
4141
legal_entity_type: Subproject
42-
repository_url: https://example.com/{{ buf_child_name | lower | replace(" ", "-") }}
43-
website_url: https://{{ buf_child_name | lower | replace(" ", "-") }}.example/
42+
repository_url: https://example.com/{{ slug_from_project_name(buf_child_name) }}
43+
website_url: https://{{ slug_from_project_name(buf_child_name) }}.example/
4444
{% endfor %}
4545

4646
sample_umbrella_iubp:
@@ -71,7 +71,7 @@ sample_umbrella_iubp:
7171
{% for iubp_child in range(15) %}
7272
{% set iubp_child_name = fake.catch_phrase().title() %}
7373
- json:
74-
slug: {{ iubp_child_name | lower | replace(" ", "-") }}
74+
slug: {{ slug_from_project_name(iubp_child_name) }}
7575
name: {{ iubp_child_name }} Project
7676
description: >-
7777
{{ iubp_child_name }}
@@ -85,6 +85,6 @@ sample_umbrella_iubp:
8585
# Children's parent is the umbrella; legal parent is LF Projects, LLC.
8686
parent_uid: !ref "sample_umbrella_iubp.steps[0]._response.uid"
8787
legal_parent_uid: !ref "base_projects.steps[?json.slug == 'lfprojects']._response.uid | [0]"
88-
repository_url: https://example.com/{{ iubp_child_name | lower | replace(" ", "-") }}
89-
website_url: https://{{ iubp_child_name | lower | replace(" ", "-") }}.example/
88+
repository_url: https://example.com/{{ slug_from_project_name(iubp_child_name) }}
89+
website_url: https://{{ slug_from_project_name(iubp_child_name) }}.example/
9090
{% endfor %}

src/lfx_v2_mockdata/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,31 @@ class UploadMockDataArgs(BaseModel):
102102
logger = structlog.get_logger()
103103

104104

105+
def slug_from_project_name(project_name: str) -> str:
106+
"""Generate a sanitized slug from a project name.
107+
108+
The slug is based on the project name lowercased, with non-alphanumeric
109+
characters replaced with hyphens, consecutive hyphens replaced with a
110+
single hyphen, and leading and trailing hyphens stripped.
111+
"""
112+
if not project_name:
113+
return ""
114+
115+
# Convert to lowercase.
116+
slug = project_name.lower()
117+
118+
# Replace non-alphanumeric characters with hyphens.
119+
slug = re.sub(r"[^a-z0-9]+", "-", slug)
120+
121+
# Replace consecutive hyphens with a single hyphen.
122+
slug = re.sub(r"-+", "-", slug)
123+
124+
# Strip leading and trailing hyphens.
125+
slug = slug.strip("-")
126+
127+
return slug
128+
129+
105130
class JMESPath(yaml.YAMLObject):
106131
"""JMESPath represents a parsed !ref YAML tag.
107132
@@ -521,6 +546,7 @@ def yaml_render(template_dir, yaml_file):
521546
.isoformat("T")
522547
.replace("+00:00", "Z")
523548
)
549+
env.globals["slug_from_project_name"] = slug_from_project_name
524550
# Store the environment in the context for use by the !include
525551
# constructor/macro and remaining YAML files in this context/directory.
526552
jinja_env.set(env)

0 commit comments

Comments
 (0)