From e5b210a265d1411d7d813a0f74f98fdade9731a4 Mon Sep 17 00:00:00 2001 From: Christian Cwienk Date: Fri, 6 Dec 2024 16:14:17 +0100 Subject: [PATCH] refactor: reduce gh-wrappers' dependencies towards pipeline-template Remove convenience-functions for implicitly creating github-APIs from GitHubCfg-elements. Remove codepaths in github.util that accept those, and instead require passing of github-api-instances. Adjust existing codebase accordingly. Also, purge model/tekton*, which has been unused for quite a while. --- cli/gardener_ci/githubutil.py | 4 +- concourse/steps/draft_release.mako | 15 +-- concourse/steps/release.mako | 14 ++- concourse/steps/update_component_deps.mako | 3 +- github/util.py | 27 +----- model/tekton.py | 102 --------------------- model/tekton_dashboard_ingress.py | 62 ------------- whd/pull_request.py | 3 +- 8 files changed, 26 insertions(+), 204 deletions(-) delete mode 100644 model/tekton.py delete mode 100644 model/tekton_dashboard_ingress.py diff --git a/cli/gardener_ci/githubutil.py b/cli/gardener_ci/githubutil.py index 23c8381c4..fdd20ce18 100644 --- a/cli/gardener_ci/githubutil.py +++ b/cli/gardener_ci/githubutil.py @@ -41,7 +41,7 @@ def list_draft_releases( github_helper = GitHubRepositoryHelper( owner=github_repository_owner, name=github_repository_name, - github_cfg=github_cfg, + github_api=ccc.github.github_api(github_cfg), ) if only_outdated: releases = [release for release in github_helper.repository.releases()] @@ -74,7 +74,7 @@ def delete_releases( github_helper = GitHubRepositoryHelper( owner=github_repository_owner, name=github_repository_name, - github_cfg=github_cfg, + github_api=ccc.github.github_api(github_cfg), ) github_helper.delete_releases(release_names=release_name) diff --git a/concourse/steps/draft_release.mako b/concourse/steps/draft_release.mako index d4de4d819..00642d9ba 100644 --- a/concourse/steps/draft_release.mako +++ b/concourse/steps/draft_release.mako @@ -34,7 +34,6 @@ import release_notes.markdown from gitutil import GitHelper from github.util import ( GitHubRepositoryHelper, - GitHubRepoBranch, ) logger = logging.getLogger('draft-release') @@ -89,15 +88,11 @@ ocm_version_lookup = cnudie.retrieve.version_lookup( ocm_repository_lookup=ocm_repository_lookup, ) -githubrepobranch = GitHubRepoBranch( - github_config=github_cfg, - repo_owner='${repo.repo_owner()}', - repo_name='${repo.repo_name()}', - branch='${repo.branch()}', -) - -github_helper = GitHubRepositoryHelper.from_githubrepobranch( - githubrepobranch=githubrepobranch, +github_helper = github.util.GitHubRepositoryHelper( + owner='${repo.repo_owner()}', + name='${repo.repo_name()}', + github_api=ccc.github.github_api(github_cfg), + default_branch='${repo.branch()}', ) try: release_note_blocks = release_notes.fetch.fetch_draft_release_notes( diff --git a/concourse/steps/release.mako b/concourse/steps/release.mako index 8387ed6bb..b00f5ee05 100644 --- a/concourse/steps/release.mako +++ b/concourse/steps/release.mako @@ -136,11 +136,14 @@ github_cfg = ccc.github.github_cfg_for_repo_url( '${repo.repo_path()}', ) ) +github_api = ccc.github.github_api(github_cfg) +repo_owner = '${repo.repo_owner()}' +repo_name = '${repo.repo_name()}' githubrepobranch = github.util.GitHubRepoBranch( github_config=github_cfg, - repo_owner='${repo.repo_owner()}', - repo_name='${repo.repo_name()}', + repo_owner=repo_owner, + repo_name=repo_name, branch=repository_branch, ) @@ -366,8 +369,13 @@ git_helper = gitutil.GitHelper.from_githubrepobranch( githubrepobranch=githubrepobranch, repo_path=repo_dir, ) -github_helper = github.util.GitHubRepositoryHelper.from_githubrepobranch(githubrepobranch) branch = githubrepobranch.branch() +github_helper = github.util.GitHubRepositoryHelper( + owner=repo_owner, + name=repo_name, + github_api=github_api, + default_branch=branch, +) % if release_trait.rebase_before_release(): logger.info(f'will fetch and rebase refs/heads/{branch}') diff --git a/concourse/steps/update_component_deps.mako b/concourse/steps/update_component_deps.mako index 2f2c7ec87..9cf088a7d 100644 --- a/concourse/steps/update_component_deps.mako +++ b/concourse/steps/update_component_deps.mako @@ -41,6 +41,7 @@ import sys import dacite +import ccc.github import ci.util import cnudie.util import cnudie.retrieve @@ -95,7 +96,7 @@ pull_request_util = github.util.PullRequestUtil( owner=REPO_OWNER, name=REPO_NAME, default_branch=REPO_BRANCH, - github_cfg=github_cfg, + github_api=ccc.github.github_api(github_cfg), ) ## hack / workaround: rebase to workaround concourse sometimes not refreshing git-resource diff --git a/github/util.py b/github/util.py index 40e2a54c7..2ed75bb30 100644 --- a/github/util.py +++ b/github/util.py @@ -75,28 +75,20 @@ def __init__( self, owner: str, name: str, - default_branch: str='master', - github_cfg: GithubConfig=None, github_api: GitHub=None, + default_branch: str='master', ): ''' Args: owner (str): repository owner (also called organisation in GitHub) name (str): repository name default_branch (str): branch to use for operations when not specified - github_cfg (GithubConfig): cfg to construct github api object from github_api (GitHub): github api to use - - Exactly one of `github_cfg` and `github_api` must be passed as argument. - Passing a GitHub object is more flexible (but less convenient). ''' - if not (bool(github_cfg) ^ bool(github_api)): - raise ValueError('exactly one of github_api and github_cfg must be given') + if not github_api: + raise ValueError('must pass github_api') - if github_cfg: - self.github = ccc.github.github_api(github_cfg) - else: - self.github = github_api + self.github = github_api self.repository = self._create_repository( owner=owner, @@ -441,17 +433,6 @@ def create_or_update_file( ) return response['commit'].sha - @staticmethod - def from_githubrepobranch( - githubrepobranch: GitHubRepoBranch, - ): - return GitHubRepositoryHelper( - github_cfg=githubrepobranch.github_config(), - owner=githubrepobranch.repo_owner(), - name=githubrepobranch.repo_name(), - default_branch=githubrepobranch.branch(), - ) - def retrieve_file_contents(self, file_path: str, branch: str=None): if branch is None: branch = self.default_branch diff --git a/model/tekton.py b/model/tekton.py deleted file mode 100644 index 2dec7f2bf..000000000 --- a/model/tekton.py +++ /dev/null @@ -1,102 +0,0 @@ -# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors -# -# SPDX-License-Identifier: Apache-2.0 - - -from model.base import ( - ModelBase, - NamedModelElement, -) - -import github.util - -from github3 import GitHub - -TEKTON_GITHUB_ORG = 'tektoncd' -TEKTON_PIPELINES_REPO_NAME = 'pipeline' -TEKTON_DASHBOARD_REPO_NAME = 'dashboard' - -TEKTON_PIPELINES_RELEASE_ASSET_NAME = 'release.yaml' -TEKTON_DASHBOARD_RELEASE_ASSET_NAME = 'tekton-dashboard-release.yaml' - - -class TektonConfig(NamedModelElement): - '''Not intended to be instantiated by users of this module - ''' - def pipelines_config(self): - if raw_cfg := self.raw.get('pipelines'): - return TektonPipelinesConfig(raw_cfg) - return None - - def dashboard_config(self): - if raw_cfg := self.raw.get('dashboard'): - return TektonDashboardConfig(raw_cfg) - return None - - def kubernetes_config_name(self): - return self.raw['kubernetes_config'] - - def _required_attributes(self): - yield from super()._required_attributes() - yield from [ - 'kubernetes_config', - ] - - def _optional_attributes(self): - yield from super()._optional_attributes() - yield from [ - 'dashboard', - 'pipelines', - ] - - -class TektonPipelinesConfig(ModelBase): - def namespace(self): - return self.raw.get('namespace') - - def version(self): - return self.raw.get('version') - - def install_manifests(self): - gh_helper = github.util.GitHubRepositoryHelper( - owner=TEKTON_GITHUB_ORG, - name=TEKTON_PIPELINES_REPO_NAME, - github_api=GitHub(), - ) - return gh_helper.retrieve_asset_contents( - release_tag=self.version(), - asset_label=TEKTON_PIPELINES_RELEASE_ASSET_NAME, - ) - - def _required_attributes(self): - yield from super()._required_attributes() - yield from [ - 'namespace' - 'version', - ] - - -class TektonDashboardConfig(ModelBase): - def namespace(self): - return self.raw.get('namespace') - - def version(self): - return self.raw.get('version') - - def install_manifests(self): - gh_helper = github.util.GitHubRepositoryHelper( - owner=TEKTON_GITHUB_ORG, - name=TEKTON_DASHBOARD_REPO_NAME, - github_api=GitHub(), - ) - return gh_helper.retrieve_asset_contents( - release_tag=self.version(), - asset_label=TEKTON_DASHBOARD_RELEASE_ASSET_NAME, - ) - - def _required_attributes(self): - yield from super()._required_attributes() - yield from [ - 'namespace' - 'version', - ] diff --git a/model/tekton_dashboard_ingress.py b/model/tekton_dashboard_ingress.py deleted file mode 100644 index ebff1453c..000000000 --- a/model/tekton_dashboard_ingress.py +++ /dev/null @@ -1,62 +0,0 @@ -# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors -# -# SPDX-License-Identifier: Apache-2.0 - - -from . import cluster_domain_from_kubernetes_config -from model.base import NamedModelElement - - -TEKTON_INGRESS_SUBDOMAIN_LABEL = 'tekton-dashboard' - - -class TektonDashboardIngressConfig(NamedModelElement): - '''Not intended to be instantiated by users of this module - ''' - - def namespace(self): - return self.raw.get('namespace') - - def external_url(self, cfg_factory): - return self.ingress_host(cfg_factory) - - def ingress_config(self): - return self.raw.get('ingress_config') - - def ingress_host(self, cfg_factory): - cluster_domain = cluster_domain_from_kubernetes_config( - cfg_factory, - self.kubernetes_config_name(), - ) - return f'{self.subdomain_label()}.{cluster_domain}' - - def subdomain_label(self): - return self.raw.get('subdomain_label', TEKTON_INGRESS_SUBDOMAIN_LABEL) - - def kubernetes_config_name(self): - return self.raw.get('kubernetes_config') - - def service_name(self): - return self.raw.get('service_name') - - def service_port(self): - return self.raw.get('service_port') - - def oauth2_proxy_config_name(self): - return self.raw.get('oauth2_proxy_config_name') - - def _required_attributes(self): - yield from super()._required_attributes() - yield from [ - 'ingress_config', - 'kubernetes_config', - 'namespace', - 'service_name', - 'service_port', - ] - - def _optional_attributes(self): - yield from super()._optional_attributes() - yield from [ - 'subdomain_label', - ] diff --git a/whd/pull_request.py b/whd/pull_request.py index b0ddf7901..77fc4fb9f 100644 --- a/whd/pull_request.py +++ b/whd/pull_request.py @@ -191,13 +191,14 @@ def github_api_for_pr_event( repo_url=ci.util.urljoin(github_host, repository_path), cfg_factory=cfg_set, ) + github_api = ccc.github.github_api(github_cfg) owner, name = repository_path.split('/') try: github_helper = GitHubRepositoryHelper( owner=owner, name=name, - github_cfg=github_cfg, + github_api=github_api, ) except NotFoundError: logger.warning(