Skip to content

Commit

Permalink
rm clunky GitHubRepoBranch + reduce dependency-convolutionness
Browse files Browse the repository at this point in the history
Remove last reference to model.github.GithubCfg from github.util (via
GitHubRepoBranch). Also, pull-up creations of gitutil
(dependency-injection).
  • Loading branch information
ccwienk committed Dec 9, 2024
1 parent c067aa9 commit 4fbfb96
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 84 deletions.
16 changes: 5 additions & 11 deletions concourse/steps/release.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 6 additions & 11 deletions concourse/steps/update_component_deps.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 12 additions & 17 deletions concourse/steps/update_component_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(),
)
Expand All @@ -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(
Expand Down Expand Up @@ -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

Expand Down
33 changes: 0 additions & 33 deletions github/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,13 @@
import ci.util
import version

from model.github import GithubConfig


class RepoPermission(enum.Enum):
PULL = "pull"
PUSH = "push"
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'

Expand Down
12 changes: 0 additions & 12 deletions gitutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4fbfb96

Please sign in to comment.