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

feat: support ADO #80

Merged
merged 1 commit into from
Oct 24, 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
88 changes: 55 additions & 33 deletions spacemk/exporters/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, config: dict):
}

self.is_gitlab = False
self.is_ado = False
self.experimental_support_variable_sets = self._config.get("experimental_support_variable_sets", False)
if self.experimental_support_variable_sets:
logging.warning("Experimental support for variable sets is enabled")
Expand Down Expand Up @@ -1408,15 +1409,23 @@ def _map_contexts_data(self, src_data: dict) -> dict:
def _map_modules_data(self, src_data: dict) -> dict:
logging.info("Start mapping modules data")

vcs_provider = "gitlab" if self.is_gitlab else "github_custom"
vcs_provider = "github_custom"
if self.is_gitlab:
vcs_provider = "gitlab"
if self.is_ado:
vcs_provider = "azure_devops"

data = []
for module in src_data.get("modules"):
if self.is_gitlab and module.get("attributes.vcs-repo.identifier"):
segments = module.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = "/".join(segments[:-1])
vcs_repository = segments[-1]
elif not self.is_gitlab and module.get("attributes.vcs-repo.identifier"):
elif self.is_ado and module.get("attributes.vcs-repo.identifier"):
segments = module.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = segments[1]
vcs_repository = segments[3]
elif not self.is_gitlab and not self.is_ado and module.get("attributes.vcs-repo.identifier"):
segments = module.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = segments[0]
vcs_repository = segments[1]
Expand Down Expand Up @@ -1526,6 +1535,46 @@ def find_workspace(data: dict, workspace_id: str) -> dict:

return data

def _determine_provider(self, info: dict) -> dict:
provider = info.get("attributes.vcs-repo.service-provider")
supported_providers = {
"github": "github_custom",
"github_app": "github_custom",
"github_enterprise": "github_custom",
"bitbucket_server": "bitbucket_datacenter",
"gitlab_hosted": "gitlab",
"ado_services": "azure_devops",
}

if provider is None:
organization_name = info.get("relationships.organization.data.id")
workspace_name = info.get("attributes.name")
logging.warning(f"Workspace '{organization_name}/{workspace_name}' has no VCS configuration")
elif provider in supported_providers:
provider = supported_providers[provider]
else:
raise ValueError(f"Unknown VCS provider name ({provider})")

if provider == "gitlab" and info.get("attributes.vcs-repo.identifier"):
self.is_gitlab = True
segments = info.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = "/".join(segments[:-1])
vcs_repository = segments[-1]
elif provider == "azure_devops" and info.get("attributes.vcs-repo.identifier"):
self.is_ado = True
segments = info.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = segments[1]
vcs_repository = segments[3]
elif provider != "gitlab" and provider != "azure_devops" and info.get("attributes.vcs-repo.identifier"):
segments = info.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = segments[0]
vcs_repository = segments[1]
else:
vcs_namespace = None
vcs_repository = None

return {"vcs_namespace": vcs_namespace, "vcs_repository": vcs_repository, "provider": provider}

def _map_stacks_data(self, src_data: dict) -> dict:
def find_workspace_variable_with_invalid_name(data: dict, workspace_id: str, type_: str = "plain") -> dict:
prog = re.compile("^[a-zA-Z_]+[a-zA-Z0-9_]*$")
Expand Down Expand Up @@ -1557,37 +1606,10 @@ def find_workspace_variable_with_invalid_name(data: dict, workspace_id: str, typ
data=src_data, type_="secret", workspace_id=workspace.get("id")
)

provider = workspace.get("attributes.vcs-repo.service-provider")

supported_providers = {
"github": "github_custom",
"github_app": "github_custom",
"github_enterprise": "github_custom",
"bitbucket_server": "bitbucket_datacenter",
"gitlab_hosted": "gitlab",
}

if provider is None:
organization_name = workspace.get("relationships.organization.data.id")
workspace_name = workspace.get("attributes.name")
logging.warning(f"Workspace '{organization_name}/{workspace_name}' has no VCS configuration")
elif provider in supported_providers:
provider = supported_providers[provider]
else:
raise ValueError(f"Unknown VCS provider name ({provider})")

if provider == "gitlab" and workspace.get("attributes.vcs-repo.identifier"):
self.is_gitlab = True
segments = workspace.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = "/".join(segments[:-1])
vcs_repository = segments[-1]
elif provider != "gitlab" and workspace.get("attributes.vcs-repo.identifier"):
segments = workspace.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = segments[0]
vcs_repository = segments[1]
else:
vcs_namespace = None
vcs_repository = None
vcs_info = self._determine_provider(workspace)
vcs_namespace = vcs_info.get("vcs_namespace")
vcs_repository = vcs_info.get("vcs_repository")
provider = vcs_info.get("provider")

terraform_version = workspace.get("attributes.terraform-version")
if terraform_version == "latest":
Expand Down
8 changes: 8 additions & 0 deletions spacemk/templates/base.tf.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ resource "spacelift_stack" "{{ stack._relationships.space._migration_id }}_{{ st
{{ argument("namespace", stack.vcs.namespace) }}
}
{% endif %}
{% elif stack.vcs.provider == "azure_devops" %}
azure_devops {
{{ argument("project", stack.vcs.namespace) }}
}
{% elif stack.vcs.provider %}
{{ stack.vcs.provider }} {
{{ argument("namespace", stack.vcs.namespace) }}
Expand Down Expand Up @@ -279,6 +283,10 @@ resource "spacelift_module" "{{ module._relationships.space._migration_id }}_{{
{{ argument("namespace", module.vcs.namespace) }}
}
{% endif %}
{% elif module.vcs.provider == "azure_devops" %}
azure_devops {
{{ argument("project", module.vcs.namespace) }}
}
{% elif module.vcs.provider %}
{{ module.vcs.provider }} {
{{ argument("namespace", module.vcs.namespace) }}
Expand Down