Skip to content

Commit

Permalink
refactor/cleanup: mv main_source -> ocm.util
Browse files Browse the repository at this point in the history
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
ccwienk committed Dec 3, 2024
1 parent dec64a7 commit cdb7413
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cli/gardener_ci/compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import ci.util
import cnudie.iter
import cnudie.retrieve
import cnudie.util
import ctx
import ocm
import ocm.util
import reutil


Expand Down Expand Up @@ -196,7 +196,7 @@ def iter_resources_with_ids(components):
]

def resource_as_dict(component, resource, resource_id):
if (main_src := cnudie.util.main_source(component, absent_ok=True)):
if (main_src := ocm.util.main_source(component, no_source_ok=True)):
src_url = main_src.access.repoUrl
elif isinstance(resource.access, ocm.OciAccess):
src_url = resource.access.imageReference
Expand Down
3 changes: 3 additions & 0 deletions cnudie/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import graphlib
import textwrap

import deprecated

import ci.util
import ocm
import oci.model as om
Expand Down Expand Up @@ -260,6 +262,7 @@ def target_oci_ref(
)


@deprecated.deprecated('use ocm.main_source instead')
def main_source(
component: ocm.Component,
absent_ok: bool=True,
Expand Down
3 changes: 2 additions & 1 deletion concourse/steps/release.mako
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import concourse.model.traits.release
import concourse.util
import ocm
import ocm.upload
import ocm.util
import github.util
import gitutil
Expand Down Expand Up @@ -217,7 +218,7 @@ build_plan = current_build.plan()
## assuming buildlogs are typically not of interest for gh-releases, hardcode to omit those
github_assets = []
main_source = cnudie.util.main_source(component)
main_source = ocm.util.main_source(component)
main_source_ref = {
'name': main_source.name,
'version': main_source.version,
Expand Down
3 changes: 2 additions & 1 deletion concourse/steps/update_component_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import traceback

import ocm
import ocm.util
import github3.exceptions
import github3.repos.repo

Expand Down Expand Up @@ -357,7 +358,7 @@ def _import_release_notes(
)
return None

main_source = cnudie.util.determine_main_source_for_component(component)
main_source = ocm.util.main_source(component)
github_cfg = ccc.github.github_cfg_for_repo_url(main_source.access.repoUrl)
org_name = main_source.access.org_name()
repository_name = main_source.access.repository_name()
Expand Down
4 changes: 2 additions & 2 deletions github/compliance/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import github3.repos

import ocm
import ocm.util
import requests

import ccc.github
import checkmarx.model
import cfg_mgmt.model as cmm
import ci.util
import cnudie.util
import concourse.model.traits.image_scan as image_scan
import delivery.client
import delivery.model
Expand Down Expand Up @@ -340,7 +340,7 @@ def _scanned_element_repository(
scanned_element: gcm.Target,
) -> github3.repos.repo.Repository:
if gcm.is_ocm_artefact_node(scanned_element):
source = cnudie.util.main_source(component=scanned_element.component)
source = ocm.util.main_source(component=scanned_element.component)

if not source.access.type is ocm.AccessType.GITHUB:
raise NotImplementedError(source)
Expand Down
7 changes: 3 additions & 4 deletions mailutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import typing

import ocm
import ocm.util

import cnudie.retrieve
import cnudie.util
from model.email import EmailConfig
from ci.util import (
existing_dir,
Expand Down Expand Up @@ -275,9 +274,9 @@ def _codeowners_parser_from_component(
component: ocm.Component,
branch_name: str='master',
):
main_source = cnudie.util.main_source(
main_source = ocm.util.main_source(
component=component,
absent_ok=False,
no_source_ok=False,
)
if not main_source.access.type is ocm.AccessType.GITHUB:
raise NotImplementedError(main_source.access.type)
Expand Down
58 changes: 58 additions & 0 deletions ocm/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import ocm


def as_component(
component: ocm.Component | ocm.ComponentDescriptor,
/,
):
if isinstance(component, ocm.ComponentDescriptor):
component = component.component
if isinstance(component, ocm.Component):
return 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)
6 changes: 3 additions & 3 deletions release_notes/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import enum

import ocm
import ocm.util
import git
import github3.repos

import cnudie.retrieve
import cnudie.util
import gitutil
import github.util
import release_notes.model as rnm
Expand Down Expand Up @@ -257,7 +257,7 @@ def fetch_draft_release_notes(
f'Creating draft-release notes from {previous_version} to current HEAD'
)

source = cnudie.util.determine_main_source_for_component(component)
source = ocm.util.main_source(component)
github_helper = rnu.github_helper_from_github_access(source.access)
git_helper = rnu.git_helper_from_github_access(source.access, repo_path)

Expand Down Expand Up @@ -364,7 +364,7 @@ def fetch_release_notes(
current_version or SpecialVersion.HEAD
)

source = cnudie.util.determine_main_source_for_component(component)
source = ocm.util.main_source(component)
github_helper = rnu.github_helper_from_github_access(source.access)
git_helper = rnu.git_helper_from_github_access(source.access, repo_path)

Expand Down
13 changes: 7 additions & 6 deletions release_notes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import typing

import ocm
import ocm.util
import git
import github3.pulls

import cnudie.util
import cnudie.retrieve
import version

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -326,14 +325,16 @@ def create_release_notes_obj(
)

# access
source_component_access = cnudie.util.determine_main_source_for_component(
source_component_access = ocm.util.main_source(
component=source_component,
absent_ok=False
no_source_ok=False,
honour_label=True,
).access

current_src_access = cnudie.util.determine_main_source_for_component(
current_src_access = ocm.util.main_source(
component=current_component,
absent_ok=False
no_source_ok=False,
honour_label=True,
).access

from_same_github_instance = current_src_access.hostname() in source_component_access.hostname()
Expand Down

0 comments on commit cdb7413

Please sign in to comment.