Skip to content

Commit

Permalink
Feature/rcs/branch targets (#106)
Browse files Browse the repository at this point in the history
* add optional branch field to deployment subscription pipelien config

* implement gitref_patterns for subscription filtering
  • Loading branch information
blueskyjunkie authored Nov 19, 2021
1 parent 3733d24 commit 1fe4b64
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 75 deletions.
1 change: 1 addition & 0 deletions foodx_devops_tools/deploy_me/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def deploy_me(
commit_sha = get_sha()
base_context = DeploymentContext(
commit_sha=commit_sha,
git_ref=git_ref,
pipeline_id=pipeline_id,
release_id=release_id,
release_state=release_state.name,
Expand Down
1 change: 1 addition & 0 deletions foodx_devops_tools/pipeline_config/_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async def _prepare_deployment_files(
for release_state in pipeline_configuration.release_states:
base_context = DeploymentContext(
commit_sha="abc123",
git_ref=None,
pipeline_id="000",
release_id="0.0.0+local",
release_state=release_state,
Expand Down
1 change: 1 addition & 0 deletions foodx_devops_tools/pipeline_config/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DeploymentLocations(pydantic.BaseModel):
class DeploymentSubscriptionReference(pydantic.BaseModel):
"""A subscription reference in a deployment definition."""

gitref_patterns: typing.Optional[typing.List[str]]
locations: typing.List[DeploymentLocations]
root_fqdn: str

Expand Down
60 changes: 50 additions & 10 deletions foodx_devops_tools/pipeline_config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DeploymentContext:
"""Deployment context data expected to be applied to resource tags."""

commit_sha: str
git_ref: typing.Optional[str]
pipeline_id: str
release_id: str
release_state: str
Expand All @@ -62,12 +63,14 @@ class DeploymentContext:
def __init__(
self: Y,
commit_sha: str,
git_ref: typing.Optional[str],
pipeline_id: str,
release_id: str,
release_state: str,
) -> None:
"""Construct ``DeploymentContext`` object."""
self.commit_sha = commit_sha
self.git_ref = git_ref
self.pipeline_id = pipeline_id
self.release_id = release_id
self.release_state = release_state
Expand Down Expand Up @@ -647,20 +650,57 @@ def __init__(

self._validate_deployment_tuple()

def __matched_subscription_patterns(
self: U,
subscription_name: str,
gitref_patterns: typing.Optional[typing.List[str]],
) -> bool:
"""
Check that the subscription matches any specified patterns.
If a git reference has not been specified then no conditioning is
applied and any specified subscriptions will be considered a "match".
"""
result = True
this_ref = self.release_view.deployment_context.git_ref
if this_ref and gitref_patterns:
not_match = [
(re.match(x, this_ref) is None) for x in gitref_patterns
]
if all(not_match):
result = False

return result

@property
def subscriptions(self: U) -> typing.List[SubscriptionView]:
"""Provide the subscriptions in this deployment."""
"""
Provide the subscriptions in this deployment.
Qualifies subscriptions both by their presence in the deployment
definitions for the project, and the optional branch patterns
specified for the subscription.
Returns:
List of subscription views in this deployment.
"""
result: typing.List[SubscriptionView] = list()
if (
str(self.deployment_tuple)
in self.release_view.configuration.deployments.deployment_tuples
):
for (
this_subscription
) in self.release_view.configuration.deployments.deployment_tuples[
str(self.deployment_tuple)
this_id = str(self.deployment_tuple)
deployment_ids = (
self.release_view.configuration.deployments.deployment_tuples
)
if this_id in deployment_ids:
for subscription_name in deployment_ids[
this_id
].subscriptions.keys():
result.append(SubscriptionView(self, this_subscription))
this_subscription = deployment_ids[this_id].subscriptions[
subscription_name
]
gitref_patterns = this_subscription.gitref_patterns
if self.__matched_subscription_patterns(
subscription_name, gitref_patterns
):
result.append(SubscriptionView(self, subscription_name))

return result

Expand Down
1 change: 1 addition & 0 deletions tests/ci/support/pipeline_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

MOCK_CONTEXT = DeploymentContext(
commit_sha="abc123",
git_ref="refs/heads/some/branch",
pipeline_id="12345",
release_id="0.0.0-dev.3",
release_state="r1",
Expand Down
62 changes: 62 additions & 0 deletions tests/ci/unit_tests/pipeline_config/test_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,65 @@ def test_empty_list_raises(apply_deployments_test):
match=r"Error validating deployments definition",
):
apply_deployments_test(file_text)


def test_branch_option_absent(apply_deployments_test):
file_text = """
---
deployments:
deployment_tuples:
name:
subscriptions:
some-name:
locations:
- primary: ploc1
secondary: sloc1
- primary: ploc2
root_fqdn: some.where
url_endpoints: ["a","b"]
"""

result = apply_deployments_test(file_text)

assert (
result.deployments.deployment_tuples["name"]
.subscriptions["some-name"]
.gitref_patterns
is None
)


def test_branch_option_present(apply_deployments_test):
file_text = """
---
deployments:
deployment_tuples:
name:
subscriptions:
some-name:
gitref_patterns:
- main
locations:
- primary: ploc1
secondary: sloc1
- primary: ploc2
root_fqdn: some.where
url_endpoints: ["a","b"]
"""

result = apply_deployments_test(file_text)

assert (
len(
result.deployments.deployment_tuples["name"]
.subscriptions["some-name"]
.gitref_patterns
)
== 1
)
assert (
result.deployments.deployment_tuples["name"]
.subscriptions["some-name"]
.gitref_patterns[0]
== "main"
)
Loading

0 comments on commit 1fe4b64

Please sign in to comment.