Skip to content

Commit

Permalink
refactor: let release-notes no longer depend on github-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ccwienk committed Dec 13, 2024
1 parent a410477 commit 4b9ef9c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 40 deletions.
4 changes: 4 additions & 0 deletions ccc/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions cli/gardener_ci/_release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
5 changes: 3 additions & 2 deletions concourse/steps/draft_release.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions concourse/steps/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import ocm

import ccc.github
import concourse.steps.version
import concourse.model.traits.version as version_trait
import dockerutil
Expand Down Expand Up @@ -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,
)

Expand Down
3 changes: 2 additions & 1 deletion concourse/steps/update_component_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
40 changes: 24 additions & 16 deletions release_notes/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()))

Expand All @@ -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
Expand All @@ -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] = {
Expand All @@ -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]:
Expand Down Expand Up @@ -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
Expand All @@ -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] = {
Expand Down
33 changes: 14 additions & 19 deletions release_notes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
import yaml
import yaml.scanner

import ccc.github
import ocm
import github.util
import gitutil
import release_notes.model as rnm

_meta_key = 'gardener.cloud/release-notes-metadata/v1'
logger = logging.getLogger(__name__)


RepoUrl: typing.TypeAlias = str
GithubApiLookup = typing.Callable[[RepoUrl], github3.GitHub]


# pylint: disable=protected-access
# noinspection PyProtectedMember
def list_associated_pulls(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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(),
)

0 comments on commit 4b9ef9c

Please sign in to comment.