-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/cleanup: mv
main_source
-> ocm.util
As another step towards discarding `cnudie` package, mv cnudie.util's main_source to ocm.util. Slightly overhaul while doing so (give callers more control about lookup/raising behaviour). Replace usages within this repository (there outside usages, hence keeping legacies in cnudie.util for smooth migration). Also, rm some unused imports.
- Loading branch information
Showing
9 changed files
with
82 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import ocm | ||
|
||
|
||
def as_component( | ||
component: ocm.Component | ocm.ComponentDescriptor, | ||
/, | ||
) -> ocm.Component: | ||
if isinstance(component, ocm.Component): | ||
return component | ||
if isinstance(component, ocm.ComponentDescriptor): | ||
return component.component | ||
|
||
raise ValueError(component) | ||
|
||
|
||
def main_source( | ||
component: ocm.Component | ocm.ComponentDescriptor, | ||
*, | ||
no_source_ok: bool=True, | ||
ambiguous_ok: bool=True, | ||
honour_label: bool=False, | ||
) -> ocm.Source | None: | ||
''' | ||
returns the "main source" of the given OCM Component. Typically, components will have exactly | ||
one source, in which the applied logic is to return the sole source-artefact. | ||
For other cases, behaviour can be controlled via kw-(only-)params: | ||
no_source_ok: if component has _no_ sources, return None | ||
ambiguous_ok: if component has more than one source, return first | ||
honour_label: check for presence of source-label `cloud.gardener/cicd/source` | ||
In cases where no main-source can be determined, raises ValueError. | ||
Note: `honour_label` is crafted specifically for Gardener-CICD, and relies on the contract that | ||
at most one source in a component may bear it w/ repository-classification main. | ||
''' | ||
component = as_component(component) | ||
|
||
if len(component.sources) == 1: | ||
return component.sources[0] | ||
elif not component.sources: | ||
if no_source_ok: | ||
return None | ||
else: | ||
raise ValueError('no sources', component) | ||
|
||
# more than one source | ||
if honour_label: | ||
for source in component.sources: | ||
if label := source.find_label('cloud.gardener/cicd/source'): | ||
if label.value.get('repository-classification') == 'main': | ||
return source | ||
|
||
if ambiguous_ok: | ||
return component.sources[0] | ||
|
||
raise ValueError('could not umambiguosly determine main-source', component) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters