Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dependencies #1093

Merged
merged 8 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/gardener_ci/githubutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down Expand Up @@ -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)

Expand Down
37 changes: 6 additions & 31 deletions concourse/model/traits/image_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import dataclasses
import enum
import re
import typing

import dacite

import github.issue

from concourse.model.job import (
JobVariant,
)
Expand Down Expand Up @@ -68,32 +69,6 @@ class Notify(enum.Enum):
GITHUB_ISSUES = 'github_issues'


@dataclasses.dataclass
class GithubIssueTemplateCfg:
body: str
type: str


@dataclasses.dataclass
class LicenseCfg:
'''
configures license policies for discovered licences

licenses are configured as lists of regular expressions (matching is done case-insensitive)
'''
prohibited_licenses: typing.Optional[list[str]] = None

def is_allowed(self, license: str):
if not self.prohibited_licenses:
return True

for prohibited in self.prohibited_licenses:
if re.fullmatch(prohibited, license, re.IGNORECASE):
return False
else:
return True


@dataclasses.dataclass(frozen=True)
class IssuePolicies:
max_processing_time_days: gcm.MaxProcessingTimesDays = gcm.MaxProcessingTimesDays()
Expand Down Expand Up @@ -138,7 +113,7 @@ class IssuePolicies:
- delivery_dashboard_url

''',
type=list[GithubIssueTemplateCfg],
type=list[github.issue.GithubIssueTemplateCfg],
),
AttributeSpec.optional(
name='github_issue_labels_to_preserve',
Expand Down Expand Up @@ -187,20 +162,20 @@ def notify(self) -> Notify:
def overwrite_github_issues_tgt_repository_url(self) -> typing.Optional[str]:
return self.raw.get('overwrite_github_issues_tgt_repository_url')

def github_issue_templates(self) -> list[GithubIssueTemplateCfg]:
def github_issue_templates(self) -> list[github.issue.GithubIssueTemplateCfg]:
if not (raw := self.raw.get('github_issue_templates')):
return None

template_cfgs = [
dacite.from_dict(
data_class=GithubIssueTemplateCfg,
data_class=github.issue.GithubIssueTemplateCfg,
data=cfg,
) for cfg in raw
]

return template_cfgs

def github_issue_template(self, type: str) -> typing.Optional[GithubIssueTemplateCfg]:
def github_issue_template(self, type: str) -> github.issue.GithubIssueTemplateCfg | None:
if not (template_cfgs := self.github_issue_templates()):
return None

Expand Down
2 changes: 1 addition & 1 deletion concourse/model/traits/scan_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import concourse.model.traits.component_descriptor

from concourse.model.traits.image_scan import (
GithubIssueTemplateCfg,
IssuePolicies,
Notify,
)
from github.issue import GithubIssueTemplateCfg


CHECKMARX_ATTRIBUTES = (
Expand Down
6 changes: 4 additions & 2 deletions concourse/steps/cfg_reporting.mako
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import dacite

import ccc.delivery
import ccc.github
import concourse.util
import cfg_mgmt.reporting as cmr
import ci.util
import concourse.model.traits.image_scan
import github.compliance.model as gcm
import github.compliance.report as gcr
import github.issue
import model
import model.concourse

Expand Down Expand Up @@ -62,7 +63,7 @@ grouped_no_undefined_policy = scan_result_group_collection_for_undefined_policy(

github_issue_template_cfgs = [
dacite.from_dict(
data_class=concourse.model.traits.image_scan.GithubIssueTemplateCfg,
data_class=github.issue.GithubIssueTemplateCfg,
data=template_cfg_raw,
)
for template_cfg_raw in ${github_issue_template_cfgs_raw}
Expand All @@ -82,6 +83,7 @@ for result_group_collection in (
overwrite_repository=repository,
delivery_svc_client=delivery_svc_client,
github_issue_template_cfgs=github_issue_template_cfgs,
job_url_callback=concourse.util.own_running_build_url,
)

</%def>
15 changes: 5 additions & 10 deletions concourse/steps/draft_release.mako
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import release_notes.markdown
from gitutil import GitHelper
from github.util import (
GitHubRepositoryHelper,
GitHubRepoBranch,
)

logger = logging.getLogger('draft-release')
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions concourse/steps/os_id.mako
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import ccc.delivery
import ccc.github
import ccc.oci
import ci.util
import concourse.model.traits.image_scan as image_scan
import cnudie.retrieve
import concourse.util
import github.compliance.model
import github.compliance.report
import github.issue

${concourse.steps.step_lib('os_id')}
${concourse.steps.step_lib('component_descriptor_util')}
Expand Down Expand Up @@ -76,7 +77,7 @@ overwrite_repository = None

% if github_issue_templates:
github_issue_template_cfgs = [dacite.from_dict(
data_class=image_scan.GithubIssueTemplateCfg,
data_class=github.issue.GithubIssueTemplateCfg,
data=raw
) for raw in ${[dataclasses.asdict(ghit) for ghit in github_issue_templates]}
]
Expand Down Expand Up @@ -129,6 +130,7 @@ github.compliance.report.create_or_update_github_issues(
% endif
delivery_svc_client=delivery_db_client,
delivery_svc_endpoints=delivery_svc_endpoints,
job_url_callback=concourse.util.own_running_build_url,
)

</%def>
26 changes: 14 additions & 12 deletions concourse/steps/release.mako
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,9 @@ github_cfg = ccc.github.github_cfg_for_repo_url(
'${repo.repo_path()}',
)
)

githubrepobranch = github.util.GitHubRepoBranch(
github_config=github_cfg,
repo_owner='${repo.repo_owner()}',
repo_name='${repo.repo_name()}',
branch=repository_branch,
)
github_api = ccc.github.github_api(github_cfg)
repo_owner = '${repo.repo_owner()}'
repo_name = '${repo.repo_name()}'

<%
import concourse.steps
Expand Down Expand Up @@ -362,12 +358,18 @@ 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 = repository_branch
github_helper = github.util.GitHubRepositoryHelper(
owner=repo_owner,
name=repo_name,
github_api=github_api,
default_branch=branch,
)
github_helper = github.util.GitHubRepositoryHelper.from_githubrepobranch(githubrepobranch)
branch = githubrepobranch.branch()

% if release_trait.rebase_before_release():
logger.info(f'will fetch and rebase refs/heads/{branch}')
Expand Down
5 changes: 3 additions & 2 deletions concourse/steps/scan_sources.mako
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ import checkmarx.util
import ci.log
ci.log.configure_default_logging()
import ci.util
import concourse.util
import delivery.client
import github.compliance.model
import github.compliance.report
from concourse.model.traits.image_scan import (
GithubIssueTemplateCfg,
IssuePolicies,
Notify,
)
from github.issue import GithubIssueTemplateCfg
cfg_factory = ci.util.ctx().cfg_factory()
cfg_set = cfg_factory.cfg_set("${cfg_set.name()}")

Expand Down Expand Up @@ -115,6 +115,7 @@ logger.info('Creating and updating github issues')
github.compliance.report.create_or_update_github_issues(
result_group_collection=scan_results_grouped,
max_processing_days=max_processing_days,
job_url_callback=concourse.util.own_running_build_url,
% if issue_tgt_repo_url:
gh_api=gh_api,
overwrite_repository=overwrite_repository,
Expand Down
20 changes: 8 additions & 12 deletions concourse/steps/update_component_deps.mako
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import sys

import dacite

import ccc.github
import ci.util
import cnudie.util
import cnudie.retrieve
Expand Down Expand Up @@ -69,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 @@ -95,15 +95,10 @@ 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
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 @@ -188,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
Loading
Loading