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

fix credentials block name #34

Merged
merged 1 commit into from
Feb 12, 2025
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
5 changes: 4 additions & 1 deletion src/prefect_cloud/cli/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from prefect_cloud.utilities.flows import get_parameter_schema_from_content
from prefect_cloud.utilities.tui import redacted
from prefect_cloud.utilities.blocks import safe_block_name


app = PrefectCloudTyper()
Expand Down Expand Up @@ -130,7 +131,9 @@ async def deploy(
credentials_name = None
if credentials:
progress.update(task, description="Syncing credentials...")
credentials_name = f"{github_ref.owner}-{github_ref.repo}-credentials"
credentials_name = safe_block_name(
f"{github_ref.owner}-{github_ref.repo}-credentials"
)
await client.create_credentials_secret(credentials_name, credentials)

pull_steps = [to_pull_step(github_ref, credentials_name)]
Expand Down
15 changes: 15 additions & 0 deletions src/prefect_cloud/utilities/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def safe_block_name(name: str) -> str:
"""Sanitize a block name to conform to Prefect Cloud's naming requirements.

Block names must only contain lowercase letters, numbers, and dashes.

Args:
name: The name to sanitize

Returns:
A sanitized name containing only lowercase letters, numbers, and dashes
"""
# Replace any non-alphanumeric chars with dashes and ensure lowercase
sanitized = "".join(c if c.isalnum() else "-" for c in name.lower())
# Remove consecutive dashes and strip dashes from ends
return "-".join(filter(None, sanitized.split("-")))
37 changes: 37 additions & 0 deletions tests/test_utilities/test_blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from prefect_cloud.utilities.blocks import safe_block_name


def test_safe_block_name():
# Test basic lowercase conversion
assert safe_block_name("MyBlock") == "myblock"

# Test special characters replaced with dashes
assert safe_block_name("my@block!name") == "my-block-name"

# Test multiple consecutive special chars become single dash
assert safe_block_name("my!!block##name") == "my-block-name"

# Test handling of existing dashes
assert safe_block_name("my-block--name") == "my-block-name"

# Test stripping dashes from ends
assert safe_block_name("-my-block-") == "my-block"

# Test real world GitHub examples
assert (
safe_block_name("PrefectHQ/prefect-cloud-credentials")
== "prefecthq-prefect-cloud-credentials"
)
assert (
safe_block_name("User123/My-Repo!!-credentials")
== "user123-my-repo-credentials"
)

# Test empty segments are removed
assert safe_block_name("my---block") == "my-block"

# Test spaces handled correctly
assert safe_block_name("my block name") == "my-block-name"

# Test mixed case with special chars
assert safe_block_name("My!!BLOCK@@name") == "my-block-name"