From 4fbfb96486ba43cd06e6b1bf428428acebaf9b37 Mon Sep 17 00:00:00 2001 From: Christian Cwienk Date: Fri, 6 Dec 2024 16:59:25 +0100 Subject: [PATCH] rm clunky GitHubRepoBranch + reduce dependency-convolutionness Remove last reference to model.github.GithubCfg from github.util (via GitHubRepoBranch). Also, pull-up creations of gitutil (dependency-injection). --- concourse/steps/release.mako | 16 ++++------- concourse/steps/update_component_deps.mako | 17 ++++------- concourse/steps/update_component_deps.py | 29 ++++++++----------- github/util.py | 33 ---------------------- gitutil.py | 12 -------- 5 files changed, 23 insertions(+), 84 deletions(-) diff --git a/concourse/steps/release.mako b/concourse/steps/release.mako index b00f5ee05..bee121006 100644 --- a/concourse/steps/release.mako +++ b/concourse/steps/release.mako @@ -140,13 +140,6 @@ 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_owner, - repo_name=repo_name, - branch=repository_branch, -) - <% import concourse.steps template = concourse.steps.step_template('component_descriptor') @@ -365,11 +358,12 @@ version_path = '${os.path.join(repo.resource_name(), version_trait.write_callbac print(f'{version_path=}') print(f'{version_interface=}') -git_helper = gitutil.GitHelper.from_githubrepobranch( - githubrepobranch=githubrepobranch, - repo_path=repo_dir, +git_helper = gitutil.GitHelper( + repo=repo_dir, + github_cfg=github_cfg, + github_repo_path=f'{repo_owner}/{repo_name}', ) -branch = githubrepobranch.branch() +branch = repository_branch github_helper = github.util.GitHubRepositoryHelper( owner=repo_owner, name=repo_name, diff --git a/concourse/steps/update_component_deps.mako b/concourse/steps/update_component_deps.mako index 9cf088a7d..ad2144562 100644 --- a/concourse/steps/update_component_deps.mako +++ b/concourse/steps/update_component_deps.mako @@ -70,11 +70,10 @@ cfg_factory = ci.util.ctx().cfg_factory() github_cfg_name = '${github_cfg_name}' github_cfg=cfg_factory.github(github_cfg_name) -githubrepobranch = github.util.GitHubRepoBranch( - github_config=github_cfg, - repo_owner=REPO_OWNER, - repo_name=REPO_NAME, - branch=REPO_BRANCH, +git_helper = gitutil.GitHelper( + repo=REPO_ROOT, + github_cfg=github_cfg, + github_repo_path=f'{REPO_OWNER}/{REPO_NAME}', ) merge_policy_configs = [ concourse.model.traits.update_component_deps.MergePolicyConfig(cfg) @@ -100,11 +99,6 @@ pull_request_util = github.util.PullRequestUtil( ) ## hack / workaround: rebase to workaround concourse sometimes not refreshing git-resource -git_helper = gitutil.GitHelper( - repo=REPO_ROOT, - github_cfg=github_cfg, - github_repo_path=f'{REPO_OWNER}/{REPO_NAME}', -) git_helper.rebase( commit_ish=REPO_BRANCH, ) @@ -189,7 +183,8 @@ for from_ref, to_version in determine_upgrade_prs( pull_request_util=pull_request_util, upgrade_script_path=os.path.join(REPO_ROOT, '${set_dependency_version_script_path}'), upgrade_script_relpath='${set_dependency_version_script_path}', - githubrepobranch=githubrepobranch, + git_helper=git_helper, + branch=REPO_BRANCH, repo_dir=REPO_ROOT, github_cfg_name=github_cfg_name, component_descriptor_lookup=ocm_lookup, diff --git a/concourse/steps/update_component_deps.py b/concourse/steps/update_component_deps.py index c2efd85be..d614d310e 100644 --- a/concourse/steps/update_component_deps.py +++ b/concourse/steps/update_component_deps.py @@ -387,8 +387,9 @@ def create_upgrade_pr( pull_request_util: gu.PullRequestUtil, upgrade_script_path, upgrade_script_relpath, - githubrepobranch: gu.GitHubRepoBranch, + branch: str, repo_dir, + git_helper: gitutil.GitHelper, github_cfg_name, merge_policy: ucd.MergePolicy, merge_method: ucd.MergeMethod, @@ -512,9 +513,9 @@ def create_upgrade_pr( upgrade_branch_name = push_upgrade_commit( ls_repo=ls_repo, + git_helper=git_helper, commit_message=commit_message, - githubrepobranch=githubrepobranch, - repo_dir=repo_dir, + branch=branch, ) # branch was created. Cleanup if something fails try: @@ -582,7 +583,7 @@ def create_upgrade_pr( from_version=from_version, to_version=to_version ), - base=githubrepobranch.branch(), + base=branch, head=upgrade_branch_name, body=pr_body.strip(), ) @@ -598,7 +599,7 @@ def create_upgrade_pr( logger.info( f"Merging upgrade-pr #{pull_request.number} ({merge_method=!s}) on branch " - f"'{upgrade_branch_name}' into branch '{githubrepobranch.branch()}'." + f"'{upgrade_branch_name}' into branch '{branch}'." ) def _merge_pr( @@ -654,30 +655,24 @@ def _merge_pr( def push_upgrade_commit( ls_repo: github3.repos.repo.Repository, + git_helper: gitutil.GitHelper, commit_message: str, - githubrepobranch: gu.GitHubRepoBranch, - repo_dir: str, + branch: str, ) -> str: - # mv diff into commit and push it - helper = gitutil.GitHelper.from_githubrepobranch( - githubrepobranch=githubrepobranch, - repo_path=repo_dir, - ) - commit = helper.index_to_commit(message=commit_message) + commit = git_helper.index_to_commit(message=commit_message) logger.info(f'commit for upgrade-PR: {commit.hexsha=}') new_branch_name = ci.util.random_str(prefix='ci-', length=12) - repo_branch = githubrepobranch.branch() - head_sha = ls_repo.ref(f'heads/{repo_branch}').object.sha + head_sha = ls_repo.ref(f'heads/{branch}').object.sha ls_repo.create_ref(f'refs/heads/{new_branch_name}', head_sha) try: - helper.push(from_ref=commit.hexsha, to_ref=f'refs/heads/{new_branch_name}') + git_helper.push(from_ref=commit.hexsha, to_ref=f'refs/heads/{new_branch_name}') except: logger.warning('an error occurred - removing now useless pr-branch') ls_repo.ref(f'heads/{new_branch_name}').delete() raise - helper.repo.git.checkout('.') + git_helper.repo.git.checkout('.') return new_branch_name diff --git a/github/util.py b/github/util.py index b364ffb2f..4268d45ec 100644 --- a/github/util.py +++ b/github/util.py @@ -27,8 +27,6 @@ import ci.util import version -from model.github import GithubConfig - class RepoPermission(enum.Enum): PULL = "pull" @@ -36,37 +34,6 @@ class RepoPermission(enum.Enum): ADMIN = "admin" -class GitHubRepoBranch: - '''Instances of this class represent a specific branch of a given GitHub repository. - ''' - def __init__( - self, - github_config: GithubConfig, - repo_owner: str, - repo_name: str, - branch: str, - ): - self._github_config = ci.util.not_none(github_config) - self._repo_owner = ci.util.not_empty(repo_owner) - self._repo_name = ci.util.not_empty(repo_name) - self._branch = ci.util.not_empty(branch) - - def github_repo_path(self): - return f'{self._repo_owner}/{self._repo_name}' - - def github_config(self): - return self._github_config - - def repo_owner(self): - return self._repo_owner - - def repo_name(self): - return self._repo_name - - def branch(self): - return self._branch - - class RepositoryHelperBase: GITHUB_TIMESTAMP_UTC_FORMAT = '%Y-%m-%dT%H:%M:%SZ' diff --git a/gitutil.py b/gitutil.py index 305f0c81a..4b40a41ee 100644 --- a/gitutil.py +++ b/gitutil.py @@ -16,7 +16,6 @@ import git.objects.util import git.remote -from github.util import GitHubRepoBranch import ci.log from ci.util import not_empty, not_none, existing_dir, fail, random_str, urljoin from model.github import ( @@ -92,17 +91,6 @@ def clone_into( github_repo_path=github_repo_path, ) - @staticmethod - def from_githubrepobranch( - githubrepobranch: GitHubRepoBranch, - repo_path: str, - ): - return GitHelper( - repo=repo_path, - github_cfg=githubrepobranch.github_config(), - github_repo_path=githubrepobranch.github_repo_path(), - ) - @property def is_dirty(self): return len(self._changed_file_paths()) > 0