def base_component_descriptor_v2( component_name_v2: str, component_labels: typing.Iterable[cm.Label], effective_version: str, source_labels: tuple, ctx_repository_base_url: str, commit: str, ): import gci.componentmodel as cm import version as version_util parsed_version = version_util.parse_to_semver(effective_version) if parsed_version.finalize_version() == parsed_version: # "final" version --> there will be a tag, later (XXX hardcoded hack) src_ref = f'refs/tags/{effective_version}' else: # let's hope the version contains something committish if parsed_version.build: src_ref = f'{parsed_version.prerelease}{parsed_version.build}' else: src_ref = f'{parsed_version.prerelease}' # logical names must not contain slashes or dots logical_name = component_name_v2.replace('/', '_').replace('.', '_') base_descriptor_v2 = cm.ComponentDescriptor( meta=cm.Metadata(schemaVersion=cm.SchemaVersion.V2), component=cm.Component( name=component_name_v2, version=effective_version, repositoryContexts=[ cm.RepositoryContext( baseUrl=ctx_repository_base_url, type=cm.AccessType.OCI_REGISTRY, ) ], provider=cm.Provider.INTERNAL, sources=[ cm.ComponentSource( name=logical_name, type=cm.SourceType.GIT, access=cm.GithubAccess( type=cm.AccessType.GITHUB, repoUrl=component_name_v2, ref=src_ref, commit=commit, ), version=effective_version, labels=source_labels, ) ], componentReferences=[], # added later resources=[], # added later labels=list(component_labels), ), ) return base_descriptor_v2
def test_github_access(): gha = cm.GithubAccess( repoUrl='github.com/org/repo', ref='refs/heads/master', type=cm.AccessType.GITHUB, ) assert gha.repository_name() == 'repo' assert gha.org_name() == 'org' assert gha.hostname() == 'github.com'
def _base_component_descriptor( version: str, ctx_repository_base_url: str, commit: str, branch: str, component_name: str = 'github.com/gardenlinux/gardenlinux', ): parsed_version = version_util.parse_to_semver(version) if parsed_version.finalize_version() == parsed_version: # "final" version --> there will be a tag, later src_ref = f'refs/tags/{version}' else: src_ref = f'refs/heads/{branch}' # logical names must not contain slashes or dots logical_name = component_name.replace('/', '_').replace('.', '_') base_descriptor_v2 = cm.ComponentDescriptor( meta=cm.Metadata(schemaVersion=cm.SchemaVersion.V2), component=cm.Component( name=component_name, version=version, repositoryContexts=[ cm.OciRepositoryContext( baseUrl=ctx_repository_base_url, type=cm.AccessType.OCI_REGISTRY, ) ], provider=cm.Provider.INTERNAL, sources=[ cm.ComponentSource( name=logical_name, type=cm.SourceType.GIT, access=cm.GithubAccess( type=cm.AccessType.GITHUB, repoUrl=component_name, ref=src_ref, commit=commit, ), version=version, ) ], componentReferences=[], resources=[], # added later ), ) return base_descriptor_v2
def test_label_usage(): component_name = 'c' component_version = '1.2.3' sources = [ cm.ComponentSource( name='repo_aux_source', access=cm.GithubAccess(type=cm.AccessType.GITHUB, ref='refs/heads/master', repoUrl='github.com/otherOrg/otherRepo'), labels=[ cm.Label( name='cloud.gardener/cicd/source', value={'repository-classification': 'auxiliary'}, ), ], ), cm.ComponentSource( name='repo_main_source', access=cm.GithubAccess(type=cm.AccessType.GITHUB, ref='refs/heads/master', repoUrl='github.com/org/repo'), labels=[ cm.Label( name='cloud.gardener/cicd/source', value={'repository-classification': 'main'}, ), ], ), ] component_with_source_label = cm.Component( name=component_name, version=component_version, sources=sources, componentReferences=[], labels=[], repositoryContexts=[ cm.RepositoryContext( baseUrl= 'eu.gcr.io/sap-se-gcr-k8s-private/cnudie/gardener/landscapes', type='ociRegistry', ), ], resources=[], provider=[], ) main_source = cnudie.util.determine_main_source_for_component( component_with_source_label, ) assert main_source.labels[0].value == {'repository-classification': 'main'} assert main_source.name == 'repo_main_source' component_without_source_label = cm.Component( name=component_name, version=component_version, sources=[ cm.ComponentSource( name='repo_main_source', access=cm.GithubAccess(type=cm.AccessType.GITHUB, ref='refs/heads/master', repoUrl='github.com/org/repo'), ), cm.ComponentSource( name='repo_aux_source', access=cm.GithubAccess( type=cm.AccessType.GITHUB, ref='refs/heads/master', repoUrl='github.com/otherOrg/otherRepo'), ), ], componentReferences=[], labels=[], repositoryContexts=[ cm.RepositoryContext( baseUrl= 'eu.gcr.io/sap-se-gcr-k8s-private/cnudie/gardener/landscapes', type='ociRegistry', ), ], resources=[], provider=[], ) main_source = cnudie.util.determine_main_source_for_component( component_without_source_label) assert main_source.name == 'repo_main_source'