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

More refactorings #1099

Merged
merged 9 commits into from
Dec 12, 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
74 changes: 0 additions & 74 deletions ccc/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import enum
import functools
import logging
import traceback
import urllib.parse

import cachecontrol
import deprecated
import ocm
import github3
import github3.github
Expand Down Expand Up @@ -166,10 +164,6 @@ def pr_helper(
)


# XXX remove this alias again
github_repo_helper = repo_helper


def github_api(
github_cfg: model.github.GithubConfig=None,
repo_url: str=None,
Expand Down Expand Up @@ -271,74 +265,6 @@ def github_cfg_for_repo_url(
return gh_cfg


@deprecated.deprecated()
@functools.lru_cache()
def github_cfg_for_hostname(
host_name,
cfg_factory=None,
require_labels=('ci',), # XXX unhardcode label
):
ci.util.not_none(host_name)
if not cfg_factory:
ctx = ci.util.ctx()
cfg_factory = ctx.cfg_factory()

if isinstance(require_labels, str):
require_labels = tuple(require_labels)

def has_required_labels(github_cfg):
if not require_labels:
return True

for required_label in require_labels:
if required_label not in github_cfg.purpose_labels():
return False
return True

for github_cfg in filter(has_required_labels, cfg_factory._cfg_elements(cfg_type_name='github')):
if github_cfg.matches_hostname(host_name=host_name):
return github_cfg

raise RuntimeError(f'no github_cfg for {host_name} with {require_labels}')


def log_stack_trace_information_hook(resp, *args, **kwargs):
'''
This function stores the current stacktrace in elastic search.
It must not return anything, otherwise the return value is assumed to replace the response
'''
if not ci.util._running_on_ci():
return # early exit if not running in ci job

import ccc.elasticsearch
config_set_name = ci.util.check_env('CONCOURSE_CURRENT_CFG')
try:
els_index = 'github_access_stacktrace'
try:
config_set = ci.util.ctx().cfg_factory().cfg_set(config_set_name)
except KeyError:
# do nothing: external concourse does not have config set 'internal_active'
return
elastic_cfg = config_set.elasticsearch()

now = datetime.datetime.utcnow()
json_body = {
'date': now.isoformat(),
'url': resp.url,
'req_method': resp.request.method,
'stacktrace': traceback.format_stack()
}

elastic_client = ccc.elasticsearch.from_cfg(elasticsearch_cfg=elastic_cfg)
elastic_client.store_document(
index=els_index,
body=json_body
)

except Exception as e:
ci.util.info(f'Could not log stack trace information: {e}')


def github_api_from_gh_access(
access: ocm.GithubAccess,
) -> github3.github.GitHub | github3.github.GitHubEnterprise:
Expand Down
10 changes: 6 additions & 4 deletions cfg_mgmt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ def rotate_config_element_and_persist_in_cfg_repo(
'''
git_helper = gitutil.GitHelper(
repo=cfg_dir,
github_cfg=github_cfg,
github_repo_path=github_repo_path,
git_cfg=github_cfg.git_cfg(
repo_path=github_repo_path,
),
)

src_file = _local_cfg_file(cfg_element, cfg_factory)
Expand Down Expand Up @@ -325,8 +326,9 @@ def process_cfg_queue_and_persist_in_repo(
'''
git_helper = gitutil.GitHelper(
repo=cfg_dir,
github_cfg=github_cfg,
github_repo_path=github_repo_path,
git_cfg=github_cfg.git_cfg(
repo_path=github_repo_path,
),
)

updated_element, processing_successful = cmro.delete_expired_secret(
Expand Down
22 changes: 21 additions & 1 deletion cli/gardener_ci/_release_notes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import ccc.github
import cnudie.retrieve
import ctx
import gitutil
import ocm
import ocm.util
import release_notes.fetch
import release_notes.markdown
import version
Expand Down Expand Up @@ -63,11 +66,28 @@ def print_release_notes(
)

component = component_descriptor.component
main_source = ocm.util.main_source(component)
try:
src_access = main_source.access
repo_url = src_access.repoUrl
except:
print(f'unsupported source-access-type: {main_source}')
exit(1)

github_cfg = ccc.github.github_cfg_for_repo_url(repo_url)

git_helper = gitutil.GitHelper.clone_into(
target_directory=repo_path,
git_cfg=github_cfg.git_cfg(
repo_path=f'{src_access.org_name()}/{src_access.repository_name()}',
),
)

blocks = release_notes.fetch.fetch_release_notes(
component=component,
component_descriptor_lookup=ocm_lookup,
version_lookup=version_lookup,
repo_path=repo_path,
git_helper=git_helper,
current_version=current_version,
previous_version=previous_version,
)
Expand Down
2 changes: 1 addition & 1 deletion concourse/replicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def _notify_broken_definition_owners(self, failed_descriptor):
)
github_api = ccc.github.github_api(github_cfg)

repo_helper = ccc.github.github_repo_helper(
repo_helper = ccc.github.repo_helper(
host=main_repo['hostname'],
org=repo_owner,
repo=repo_name,
Expand Down
6 changes: 3 additions & 3 deletions concourse/resources/github.mako
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ credentials = github_cfg.credentials()
disable_tls_validation = not github_cfg.tls_validation()
token_or_passwd = credentials.auth_token() or credentials.passwd()

preferred_protocol = github_cfg.preferred_protocol()
preferred_protocol = github_cfg.preferred_protocol
# repo-specific cfg "wins"
if (overwrite_preferred_protocol := repo_cfg.preferred_protocol()):
preferred_protocol = overwrite_preferred_protocol
Expand Down Expand Up @@ -117,9 +117,9 @@ credentials = github_cfg.credentials()
source:
repo: ${repo_cfg.repo_path()}
base: ${repo_cfg.branch()}
% if github_cfg.preferred_protocol() is Protocol.SSH:
% if github_cfg.preferred_protocol is Protocol.SSH:
uri: ${github_cfg.ssh_url()}/${repo_cfg.repo_path()}
% elif github_cfg.preferred_protocol() is Protocol.HTTPS:
% elif github_cfg.preferred_protocol is Protocol.HTTPS:
uri: ${github_cfg.http_url()}/${repo_cfg.repo_path()}
% else:
<% raise NotImplementedError %>
Expand Down
10 changes: 8 additions & 2 deletions concourse/steps/draft_release.mako
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import ci.util
import cnudie.retrieve
import cnudie.util
import github.util
import gitutil
import ocm
import release_notes.fetch
import release_notes.markdown

from gitutil import GitHelper
from github.util import (
GitHubRepositoryHelper,
)
Expand Down Expand Up @@ -95,9 +95,15 @@ github_helper = github.util.GitHubRepositoryHelper(
github_api=ccc.github.github_api(github_cfg),
default_branch='${repo.branch()}',
)
git_helper = gitutil.GitHelper(
repo=repo_dir,
git_cfg=github_cfg.git_cfg(
repo_path='${repo.repo_owner()}/${repo.repo_name()}',
),
)
try:
release_note_blocks = release_notes.fetch.fetch_draft_release_notes(
repo_path=repo_dir,
git_helper=git_helper,
component=component,
component_descriptor_lookup=component_descriptor_lookup,
version_lookup=ocm_version_lookup,
Expand Down
7 changes: 4 additions & 3 deletions concourse/steps/release.mako
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,9 @@ print(f'{version_interface=}')

git_helper = gitutil.GitHelper(
repo=repo_dir,
github_cfg=github_cfg,
github_repo_path=f'{repo_owner}/{repo_name}',
git_cfg=github_cfg.git_cfg(
repo_path=f'{repo_owner}/{repo_name}',
),
)
branch = repository_branch
github_helper = github.util.GitHubRepositoryHelper(
Expand Down Expand Up @@ -515,7 +516,7 @@ except git.GitCommandError:
% if process_release_notes:
try:
release_notes_md = collect_release_notes(
repo_dir=repo_dir,
git_helper=git_helper,
release_version=version_str,
component=component,
component_descriptor_lookup=component_descriptor_lookup,
Expand Down
4 changes: 2 additions & 2 deletions concourse/steps/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ def _calculate_tags(


def collect_release_notes(
repo_dir,
git_helper: GitHelper,
release_version: str,
component,
component_descriptor_lookup,
version_lookup,
) -> str:
release_note_blocks = release_notes.fetch.fetch_release_notes(
repo_path=repo_dir,
git_helper=git_helper,
component=component,
version_lookup=version_lookup,
component_descriptor_lookup=component_descriptor_lookup,
Expand Down
5 changes: 3 additions & 2 deletions concourse/steps/update_component_deps.mako
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ github_cfg=cfg_factory.github(github_cfg_name)

git_helper = gitutil.GitHelper(
repo=REPO_ROOT,
github_cfg=github_cfg,
github_repo_path=f'{REPO_OWNER}/{REPO_NAME}',
git_cfg=github_cfg.git_cfg(
repo_path=f'{REPO_OWNER}/{REPO_NAME}',
),
)
merge_policy_configs = [
concourse.model.traits.update_component_deps.MergePolicyConfig(cfg)
Expand Down
9 changes: 5 additions & 4 deletions concourse/steps/update_component_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,16 +690,17 @@ def create_release_notes(

try:
with tempfile.TemporaryDirectory() as temp_dir:
gitutil.GitHelper.clone_into(
git_helper = gitutil.GitHelper.clone_into(
target_directory=temp_dir,
github_cfg=from_github_cfg,
github_repo_path=f'{from_repo_owner}/{from_repo_name}'
git_cfg=from_github_cfg.git_cfg(
repo_path=f'{from_repo_owner}/{from_repo_name}',
),
)
release_note_blocks = release_notes_fetch.fetch_release_notes(
component=from_component,
version_lookup=version_lookup,
component_descriptor_lookup=component_descriptor_lookup,
repo_path=temp_dir,
git_helper=git_helper,
current_version=to_version,
previous_version=from_version,
)
Expand Down
11 changes: 0 additions & 11 deletions github/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from github3.repos.release import Release

import ocm
import ccc.github
import ci.util
import version

Expand Down Expand Up @@ -744,16 +743,6 @@ def delete_outdated_draft_releases(self) -> Iterable[Tuple[github3.repos.release
yield release, release.delete()


def branches(
github_cfg,
repo_owner: str,
repo_name: str,
):
github_api = ccc.github.github_api(github_cfg=github_cfg)
repo = github_api.repository(repo_owner, repo_name)
return list(map(lambda r: r.name, repo.branches()))


def _retrieve_team_by_name_or_none(
organization: github3.orgs.Organization,
team_name: str
Expand Down
Loading
Loading