diff --git a/ccc/github.py b/ccc/github.py index 12a6d3ff0..09e14b2c8 100644 --- a/ccc/github.py +++ b/ccc/github.py @@ -220,6 +220,10 @@ def github_api( return github_api +def github_api_lookup(repo_url, /) -> github3.GitHub: + return github_api(repo_url=repo_url) + + @functools.lru_cache() def github_cfg_for_repo_url( repo_url: str | urllib.parse.ParseResult=None, diff --git a/cli/gardener_ci/_release_notes.py b/cli/gardener_ci/_release_notes.py index b40a39b22..9a260752a 100644 --- a/cli/gardener_ci/_release_notes.py +++ b/cli/gardener_ci/_release_notes.py @@ -88,6 +88,7 @@ def print_release_notes( component_descriptor_lookup=ocm_lookup, version_lookup=version_lookup, git_helper=git_helper, + github_api_lookup=ccc.github.github_api_lookup, current_version=current_version, previous_version=previous_version, ) diff --git a/concourse/steps/draft_release.mako b/concourse/steps/draft_release.mako index 85f842e15..03075c6cc 100644 --- a/concourse/steps/draft_release.mako +++ b/concourse/steps/draft_release.mako @@ -103,11 +103,12 @@ git_helper = gitutil.GitHelper( ) try: release_note_blocks = release_notes.fetch.fetch_draft_release_notes( - git_helper=git_helper, + current_version=version_str, component=component, component_descriptor_lookup=component_descriptor_lookup, version_lookup=ocm_version_lookup, - current_version=version_str, + git_helper=git_helper, + github_api_lookup=ccc.github.github_api_lookup, ) release_notes_md = '\n'.join( str(i) for i in release_notes.markdown.render(release_note_blocks) diff --git a/concourse/steps/release.py b/concourse/steps/release.py index 070215689..38f4298b2 100644 --- a/concourse/steps/release.py +++ b/concourse/steps/release.py @@ -18,6 +18,7 @@ import ocm +import ccc.github import concourse.steps.version import concourse.model.traits.version as version_trait import dockerutil @@ -143,10 +144,11 @@ def collect_release_notes( version_lookup, ) -> str: release_note_blocks = release_notes.fetch.fetch_release_notes( - git_helper=git_helper, component=component, - version_lookup=version_lookup, component_descriptor_lookup=component_descriptor_lookup, + version_lookup=version_lookup, + git_helper=git_helper, + github_api_lookup=ccc.github.github_api_lookup, current_version=release_version, ) diff --git a/concourse/steps/update_component_deps.py b/concourse/steps/update_component_deps.py index 5ecab4135..5e273aa2e 100644 --- a/concourse/steps/update_component_deps.py +++ b/concourse/steps/update_component_deps.py @@ -698,9 +698,10 @@ def create_release_notes( ) release_note_blocks = release_notes_fetch.fetch_release_notes( component=from_component, - version_lookup=version_lookup, component_descriptor_lookup=component_descriptor_lookup, + version_lookup=version_lookup, git_helper=git_helper, + github_api_lookup=ccc.github.github_api_lookup, current_version=to_version, previous_version=from_version, ) diff --git a/release_notes/fetch.py b/release_notes/fetch.py index af7ad8391..edd2dab33 100644 --- a/release_notes/fetch.py +++ b/release_notes/fetch.py @@ -10,7 +10,6 @@ import cnudie.retrieve import gitutil -import github.util import release_notes.model as rnm import release_notes.utils as rnu import version @@ -144,8 +143,9 @@ def get_release_note_commits_tuple( def _determine_blocks_to_include( filter_in_commits: tuple[git.Commit, ...], filter_out_commits: tuple[git.Commit, ...], + github_access: ocm.GithubAccess, git_helper: gitutil.GitHelper, - github_helper: github.util.GitHubRepositoryHelper, + github_api_lookup: rnu.GithubApiLookup, ) -> set[rnm.SourceBlock]: logger.info( f'Found {(commit_count := len(filter_in_commits))} relevant commits for release notes ' @@ -178,9 +178,8 @@ def _determine_blocks_to_include( # find associated pull requests for commits commit_pulls = rnu.request_pull_requests_from_api( git_helper=git_helper, - gh=github_helper.github, - owner=github_helper.owner, - repo_name=github_helper.repository_name, + github_api_lookup=github_api_lookup, + github_access=github_access, commits=[*filter_in_commits, *filter_out_commits], group_size=commit_processing_group_size, min_seconds_per_group=processing_group_min_seconds, @@ -242,6 +241,7 @@ def fetch_draft_release_notes( component_descriptor_lookup: cnudie.retrieve.ComponentDescriptorLookupById, version_lookup: cnudie.retrieve.VersionLookupByComponent, git_helper: gitutil.GitHelper, + github_api_lookup: rnu.GithubApiLookup, ): known_versions: list[str] = list(version_lookup(component.identity())) @@ -257,15 +257,17 @@ def fetch_draft_release_notes( f'Creating draft-release notes from {previous_version} to current HEAD' ) - source = ocm.util.main_source(component) - github_helper = rnu.github_helper_from_github_access(source.access) + # todo: need to check access-type / handle unsupported types (!= GitHub) + github_access = ocm.util.main_source(component).access + repo_url = github_access.repoUrl # make sure _all_ tags are available locally git_helper.fetch_tags() - github_repo: github3.repos.Repository = github_helper.github.repository( - owner=github_helper.owner, - repository=github_helper.repository_name, + github_api = github_api_lookup(repo_url) + github_repo: github3.repos.Repository = github_api.repository( + owner=github_access.org_name(), + repository=github_access.repository_name(), ) # fetch commits for release @@ -278,8 +280,9 @@ def fetch_draft_release_notes( release_note_blocks = _determine_blocks_to_include( filter_in_commits=filter_in_commits, filter_out_commits=filter_out_commits, + github_access=github_access, git_helper=git_helper, - github_helper=github_helper, + github_api_lookup=github_api_lookup, ) release_notes: set[rnm.ReleaseNote] = { @@ -300,6 +303,7 @@ def fetch_release_notes( component_descriptor_lookup: cnudie.retrieve.ComponentDescriptorLookupById, version_lookup: cnudie.retrieve.VersionLookupByComponent, git_helper: gitutil.GitHelper, + github_api_lookup: rnu.GithubApiLookup, current_version: typing.Optional[str] = None, previous_version: typing.Optional[str] = None, ) -> set[rnm.ReleaseNote]: @@ -363,14 +367,17 @@ def fetch_release_notes( ) source = ocm.util.main_source(component) - github_helper = rnu.github_helper_from_github_access(source.access) + # todo: check access-type / handle unsupported types (non-github) + github_access: ocm.GithubAccess = source.access + + github_api = github_api_lookup(github_access.repoUrl) # make sure _all_ tags are available locally git_helper.fetch_tags() - github_repo: github3.repos.Repository = github_helper.github.repository( - owner=github_helper.owner, - repository=github_helper.repository_name, + github_repo: github3.repos.Repository = github_api.repository( + owner=github_access.org_name(), + repository=github_access.repository_name(), ) # fetch commits for release @@ -383,8 +390,9 @@ def fetch_release_notes( release_note_blocks = _determine_blocks_to_include( filter_in_commits=filter_in_commits, filter_out_commits=filter_out_commits, + github_access=github_access, git_helper=git_helper, - github_helper=github_helper, + github_api_lookup=github_api_lookup, ) release_notes: set[rnm.ReleaseNote] = { diff --git a/release_notes/utils.py b/release_notes/utils.py index 85cb7e171..b4e7cfa30 100644 --- a/release_notes/utils.py +++ b/release_notes/utils.py @@ -14,9 +14,7 @@ import yaml import yaml.scanner -import ccc.github import ocm -import github.util import gitutil import release_notes.model as rnm @@ -24,6 +22,10 @@ logger = logging.getLogger(__name__) +RepoUrl: typing.TypeAlias = str +GithubApiLookup = typing.Callable[[RepoUrl], github3.GitHub] + + # pylint: disable=protected-access # noinspection PyProtectedMember def list_associated_pulls( @@ -160,9 +162,8 @@ def _grouper(iterable, n, *, incomplete='fill', fillvalue=None): def request_pull_requests_from_api( git_helper: gitutil.GitHelper, - gh: github3.GitHub, - owner: str, - repo_name: str, + github_api_lookup: GithubApiLookup, + github_access: ocm.GithubAccess, commits: list[git.Commit], group_size: int = 200, min_seconds_per_group: int = 300, @@ -217,7 +218,11 @@ def request_pull_requests_from_api( pending[num].append(commit.hexsha) continue - if prs := list_associated_pulls(gh, owner, repo_name, commit.hexsha): + owner = github_access.org_name() + repo_name = github_access.repository_name() + github_api = github_api_lookup(github_access.repoUrl) + + if prs := list_associated_pulls(github_api, owner, repo_name, commit.hexsha): # add all found pull requests to the result right away result[commit.hexsha].extend(prs) # only write notes to commit if there are no notes yet, @@ -240,11 +245,12 @@ def request_pull_requests_from_api( f'wait {int(wait_period)} seconds before continuing.' ) time.sleep(wait_period) + # make sure to always use github-user with largest remaining quota - gh = ccc.github.github_api(github_cfg=git_helper.github_cfg) + github_api = github_api_lookup(github_access.repoUrl) if pending: - for pull in list_pulls(gh, owner, repo_name): + for pull in list_pulls(github_api, owner, repo_name): if pull.number in pending: for sha in pending[pull.number]: result[sha].append(pull) @@ -256,14 +262,3 @@ def request_pull_requests_from_api( f'{pending.keys()} is/are either not closed or cannot be found') return result - - -def github_helper_from_github_access( - github_access=ocm.GithubAccess, -): - logger.info(f'Creating GH Repo-helper for {github_access.repoUrl}') - return github.util.GitHubRepositoryHelper( - github_api=ccc.github.github_api_from_gh_access(github_access), - owner=github_access.org_name(), - name=github_access.repository_name(), - )