def generate_release_notes_cli( repo_dir: str, github_cfg_name: str, github_repository_owner: str, github_repository_name: str, repository_branch: str, commit_range: str=None ): github_cfg = ctx().cfg_factory().github(github_cfg_name) githubrepobranch = GitHubRepoBranch( github_config=github_cfg, repo_owner=github_repository_owner, repo_name=github_repository_name, branch=repository_branch, ) helper = GitHubRepositoryHelper.from_githubrepobranch( githubrepobranch=githubrepobranch, ) git_helper = GitHelper.from_githubrepobranch( repo_path=repo_dir, githubrepobranch=githubrepobranch, ) ReleaseNotes.create( github_helper=helper, git_helper=git_helper, repository_branch=repository_branch, commit_range=commit_range ).to_markdown()
def _add_all_and_create_commit(git_helper: GitHelper, message: str): commit = git_helper.index_to_commit(message=message, ) git_helper.repo.head.reset( commit=commit, working_tree=True, ) return commit
def calculate_range( repository_branch: str, git_helper: GitHelper, github_helper: GitHubRepositoryHelper, ) -> str: repo = git_helper.repo branch_head = git_helper.fetch_head(ref=repository_branch) if not branch_head: fail('could not determine branch head of {branch} branch'.format( branch=repository_branch)) range_start = _.head( reachable_release_tags_from_commit(github_helper, repo, branch_head)) try: # better readable range_end by describing head commit range_end = repo.git.describe(branch_head) except GitError as err: warning( 'failed to describe branch head, maybe the repository has no tags? ' 'GitError: {err}. Falling back to branch head commit hash.'.format( err=err)) range_end = branch_head.hexsha commit_range = "{start}..{end}".format(start=range_start, end=range_end) return commit_range
def git_helper_from_github_access( github_access: gci.componentmodel.GithubAccess, repo_path: str, ): logger.info(f'Creating Git-helper for {github_access.repoUrl}') return GitHelper( repo=repo_path, github_cfg=ccc.github.github_cfg_for_repo_url(github_access.repoUrl), github_repo_path= f'{github_access.org_name()}/{github_access.repository_name()}', )
def git_helper_from_github_access( github_access: gci.componentmodel.GithubAccess, repo_path: str, ): return GitHelper( repo=repo_path, github_cfg=ccc.github.github_cfg_for_hostname( github_access.hostname()), github_repo_path= f'{github_access.org_name()}/{github_access.repository_name()}', )
def cloneForkedRepo(): github_cfg = f.github('github_com') subprocess.run([ "git", "config", "--global", "user.name", github_cfg.credentials().username() ]) subprocess.run([ "git", "config", "--global", "user.email", github_cfg.credentials().email_address() ]) gitHelper = GitHelper.clone_into(repo_name, github_cfg, repo_path) print('INFO: Cloned ' + repo_path + ' repository into ' + repo_name + ' directory') return gitHelper
def fetch_release_notes( github_repository_owner: str, github_repository_name: str, github_cfg: str, repo_dir: str, github_helper: GitHubRepositoryHelper, repository_branch: str, ): repo_path = github_repo_path(owner=github_repository_owner, name=github_repository_name) git_helper = GitHelper(repo=repo_dir, github_cfg=github_cfg, github_repo_path=repo_path) return ReleaseNotes.create(github_helper=github_helper, git_helper=git_helper, repository_branch=repository_branch)
def release_and_prepare_next_dev_cycle( githubrepobranch: GitHubRepoBranch, repository_version_file_path: str, release_version: str, repo_dir: str, release_notes_policy: str, release_commit_publishing_policy: str, release_commit_callback: str = None, next_version_callback: str = None, version_operation: str = "bump_minor", prerelease_suffix: str = "dev", author_name: str = "gardener-ci", author_email: str = "*****@*****.**", component_descriptor_file_path: str = None, slack_cfg_name: str = None, slack_channel: str = None, rebase_before_release: bool = False, commit_message_prefix: str = None, ): transaction_ctx = TransactionContext() # shared between all steps/trxs release_notes_policy = ReleaseNotesPolicy(release_notes_policy) release_commit_publishing_policy = ReleaseCommitPublishingPolicy( release_commit_publishing_policy) github_helper = GitHubRepositoryHelper.from_githubrepobranch( githubrepobranch) git_helper = GitHelper.from_githubrepobranch( githubrepobranch=githubrepobranch, repo_path=repo_dir, ) step_list = [] if rebase_before_release: rebase_step = RebaseStep( git_helper=git_helper, repository_branch=githubrepobranch.branch(), ) step_list.append(rebase_step) release_commit_step = ReleaseCommitStep( git_helper=git_helper, repo_dir=repo_dir, release_version=release_version, repository_version_file_path=repository_version_file_path, repository_branch=githubrepobranch.branch(), commit_message_prefix=commit_message_prefix, release_commit_callback=release_commit_callback, publishing_policy=release_commit_publishing_policy, ) step_list.append(release_commit_step) if version_operation != version.NOOP: next_cycle_commit_step = NextDevCycleCommitStep( git_helper=git_helper, repo_dir=repo_dir, release_version=release_version, repository_version_file_path=repository_version_file_path, repository_branch=githubrepobranch.branch(), version_operation=version_operation, prerelease_suffix=prerelease_suffix, next_version_callback=next_version_callback, publishing_policy=release_commit_publishing_policy, ) step_list.append(next_cycle_commit_step) github_release_step = GitHubReleaseStep( github_helper=github_helper, githubrepobranch=githubrepobranch, repo_dir=repo_dir, release_version=release_version, component_descriptor_file_path=component_descriptor_file_path, ) step_list.append(github_release_step) release_transaction = Transaction(ctx=transaction_ctx, steps=step_list) release_transaction.validate() if not release_transaction.execute(): raise RuntimeError('An error occurred while creating the Release.') publish_release_notes_step = PublishReleaseNotesStep( githubrepobranch=githubrepobranch, github_helper=github_helper, release_version=release_version, repo_dir=repo_dir, ) cleanup_draft_releases_step = TryCleanupDraftReleasesStep( github_helper=github_helper, ) cleanup_draft_releases_transaction = Transaction( ctx=transaction_ctx, steps=(cleanup_draft_releases_step, ), ) if not cleanup_draft_releases_transaction.execute(): ci.util.warning('An error occured while cleaning up draft releases') if release_notes_policy == ReleaseNotesPolicy.DISABLED: return info('release notes were disabled - skipping') elif release_notes_policy == ReleaseNotesPolicy.DEFAULT: pass else: raise NotImplementedError(release_notes_policy) release_notes_transaction = Transaction( ctx=transaction_ctx, steps=(publish_release_notes_step, ), ) release_notes_transaction.validate() if not release_notes_transaction.execute(): raise RuntimeError( 'An error occurred while publishing the release notes.') if slack_cfg_name and slack_channel: release_notes = transaction_ctx.step_output( publish_release_notes_step.name()).get('release notes') post_to_slack_step = PostSlackReleaseStep( slack_cfg_name=slack_cfg_name, slack_channel=slack_channel, release_version=release_version, release_notes=release_notes, githubrepobranch=githubrepobranch, ) slack_transaction = Transaction( ctx=transaction_ctx, steps=(post_to_slack_step, ), ) slack_transaction.validate() if not slack_transaction.execute(): raise RuntimeError( 'An error occurred while posting the release notes to Slack.')
def release_and_prepare_next_dev_cycle( githubrepobranch: GitHubRepoBranch, repository_version_file_path: str, release_version: str, repo_dir: str, release_notes_policy: str, release_commit_callback: str = None, next_version_callback: str = None, version_operation: str = "bump_minor", prerelease_suffix: str = "dev", author_name: str = "gardener-ci", author_email: str = "*****@*****.**", component_descriptor_file_path: str = None, slack_cfg_name: str = None, slack_channel: str = None, rebase_before_release: bool = False, ): release_notes_policy = ReleaseNotesPolicy(release_notes_policy) github_helper = GitHubRepositoryHelper.from_githubrepobranch( githubrepobranch) git_helper = GitHelper.from_githubrepobranch( githubrepobranch=githubrepobranch, repo_path=repo_dir, ) release_commits_step = ReleaseCommitsStep( git_helper=git_helper, repo_dir=repo_dir, release_version=release_version, repository_version_file_path=repository_version_file_path, repository_branch=githubrepobranch.branch(), version_operation=version_operation, prerelease_suffix=prerelease_suffix, release_commit_callback=release_commit_callback, next_version_callback=next_version_callback, rebase_before_release=rebase_before_release, ) github_release_step = GitHubReleaseStep( github_helper=github_helper, githubrepobranch=githubrepobranch, repo_dir=repo_dir, release_version=release_version, component_descriptor_file_path=component_descriptor_file_path, ) release_transaction = Transaction( release_commits_step, github_release_step, ) release_transaction.validate() if not release_transaction.execute(): raise RuntimeError('An error occurred while creating the Release.') publish_release_notes_step = PublishReleaseNotesStep( githubrepobranch=githubrepobranch, github_helper=github_helper, release_version=release_version, repo_dir=repo_dir, ) cleanup_draft_releases_step = CleanupDraftReleaseStep( github_helper=github_helper, release_version=release_version, ) if release_notes_policy == ReleaseNotesPolicy.DISABLED: return info('release notes were disabled - skipping') elif release_notes_policy == ReleaseNotesPolicy.DEFAULT: pass else: raise NotImplementedError(release_notes_policy) release_notes_steps = [ publish_release_notes_step, cleanup_draft_releases_step, ] release_notes_transaction = Transaction(*release_notes_steps) release_notes_transaction.validate() if not release_notes_transaction.execute(): raise RuntimeError( 'An error occurred while publishing the release notes.') if slack_cfg_name and slack_channel: context = release_notes_transaction.context() release_notes = context.step_output( publish_release_notes_step.name()).get('release notes') post_to_slack_step = PostSlackReleaseStep( slack_cfg_name=slack_cfg_name, slack_channel=slack_channel, release_version=release_version, release_notes=release_notes, githubrepobranch=githubrepobranch, ) slack_transaction = Transaction(post_to_slack_step) slack_transaction.validate() if not slack_transaction.execute(): raise RuntimeError( 'An error occurred while posting the release notes to Slack.')
import ctx from gitutil import (GitHelper) from github.util import ( GitHubRepositoryHelper, ) it_label = "test/integration" source_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../") repo_owner_name = os.getenv("SOURCE_GITHUB_REPO_OWNER_AND_NAME") github_repository_owner, github_repository_name = repo_owner_name.split("/") cfg_set = ctx.cfg_factory().cfg_set(os.getenv("CONCOURSE_CURRENT_CFG")) github_cfg = cfg_set.github() git_helper = GitHelper(repo=os.path.join(source_path, ".git"), github_cfg=github_cfg, github_repo_path=repo_owner_name) pull_request_number = git_helper.repo.git.config("--get", "pullrequest.id") github_helper = GitHubRepositoryHelper( owner=github_repository_owner, name=github_repository_name, github_cfg=github_cfg, ) pull_request = github_helper.repository.pull_request(pull_request_number) labels = [str(label) for label in pull_request.issue().labels()] print("Found labels {}".format(labels))
def release_and_prepare_next_dev_cycle( component_name: str, githubrepobranch: GitHubRepoBranch, release_commit_publishing_policy: str, release_notes_policy: str, release_version: str, repo_hostname: str, repo_path: str, repo_dir: str, repository_version_file_path: str, git_tags: list, github_release_tag: dict, release_commit_callback_image_reference: str, component_descriptor_v2_path: str = None, ctf_path: str = None, next_cycle_commit_message_prefix: str = None, next_version_callback: str = None, prerelease_suffix: str = "dev", rebase_before_release: bool = False, release_on_github: bool = True, release_commit_callback: str = None, release_commit_message_prefix: str = None, slack_channel_configs: list = [], version_operation: str = "bump_minor", ): transaction_ctx = TransactionContext() # shared between all steps/trxs release_notes_policy = ReleaseNotesPolicy(release_notes_policy) release_commit_publishing_policy = ReleaseCommitPublishingPolicy( release_commit_publishing_policy) github_helper = GitHubRepositoryHelper.from_githubrepobranch( githubrepobranch) git_helper = GitHelper.from_githubrepobranch( githubrepobranch=githubrepobranch, repo_path=repo_dir, ) step_list = [] if rebase_before_release: rebase_step = RebaseStep( git_helper=git_helper, repository_branch=githubrepobranch.branch(), ) step_list.append(rebase_step) release_commit_step = ReleaseCommitStep( git_helper=git_helper, repo_dir=repo_dir, release_version=release_version, repository_version_file_path=repository_version_file_path, repository_branch=githubrepobranch.branch(), release_commit_message_prefix=release_commit_message_prefix, release_commit_callback=release_commit_callback, release_commit_callback_image_reference= release_commit_callback_image_reference, publishing_policy=release_commit_publishing_policy, ) step_list.append(release_commit_step) create_tag_step = CreateTagsStep( git_tags=git_tags, github_release_tag=github_release_tag, git_helper=git_helper, github_helper=github_helper, release_version=release_version, publishing_policy=release_commit_publishing_policy, ) step_list.append(create_tag_step) if version_operation != version.NOOP: next_cycle_commit_step = NextDevCycleCommitStep( git_helper=git_helper, repo_dir=repo_dir, release_version=release_version, repository_version_file_path=repository_version_file_path, repository_branch=githubrepobranch.branch(), version_operation=version_operation, prerelease_suffix=prerelease_suffix, next_version_callback=next_version_callback, publishing_policy=release_commit_publishing_policy, next_cycle_commit_message_prefix=next_cycle_commit_message_prefix, ) step_list.append(next_cycle_commit_step) if release_on_github: github_release_step = GitHubReleaseStep( github_helper=github_helper, githubrepobranch=githubrepobranch, repo_dir=repo_dir, component_name=component_name, release_version=release_version, ) step_list.append(github_release_step) upload_component_descriptor_step = UploadComponentDescriptorStep( github_helper=github_helper, component_descriptor_v2_path=component_descriptor_v2_path, ctf_path=ctf_path, release_on_github=release_on_github, ) step_list.append(upload_component_descriptor_step) release_transaction = Transaction( ctx=transaction_ctx, steps=step_list, ) release_transaction.validate() if not release_transaction.execute(): raise RuntimeError('An error occurred while creating the Release.') if release_on_github: publish_release_notes_step = PublishReleaseNotesStep( githubrepobranch=githubrepobranch, github_helper=github_helper, repository_hostname=repo_hostname, repository_path=repo_path, release_version=release_version, component_descriptor_v2_path=component_descriptor_v2_path, ctf_path=ctf_path, repo_dir=repo_dir, ) cleanup_draft_releases_step = TryCleanupDraftReleasesStep( github_helper=github_helper, ) cleanup_draft_releases_transaction = Transaction( ctx=transaction_ctx, steps=(cleanup_draft_releases_step, ), ) if not cleanup_draft_releases_transaction.execute(): logger.warning('An error occured while cleaning up draft releases') if release_notes_policy == ReleaseNotesPolicy.DISABLED: return logger.info('release notes were disabled - skipping') elif release_notes_policy == ReleaseNotesPolicy.DEFAULT: pass else: raise NotImplementedError(release_notes_policy) if release_on_github: release_notes_transaction = Transaction( ctx=transaction_ctx, steps=(publish_release_notes_step, ), ) release_notes_transaction.validate() if not release_notes_transaction.execute(): raise RuntimeError( 'An error occurred while publishing the release notes.') if slack_channel_configs: if not release_on_github: raise RuntimeError('Cannot post to slack without a github release') release_notes = transaction_ctx.step_output( publish_release_notes_step.name()).get('release notes') all_slack_releases_successful = True for slack_cfg in slack_channel_configs: slack_cfg_name = slack_cfg['slack_cfg_name'] slack_channel = slack_cfg['channel_name'] post_to_slack_step = PostSlackReleaseStep( slack_cfg_name=slack_cfg_name, slack_channel=slack_channel, release_version=release_version, release_notes=release_notes, githubrepobranch=githubrepobranch, ) slack_transaction = Transaction( ctx=transaction_ctx, steps=(post_to_slack_step, ), ) slack_transaction.validate() all_slack_releases_successful = (all_slack_releases_successful and slack_transaction.execute()) if not all_slack_releases_successful: raise RuntimeError( 'An error occurred while posting the release notes to Slack.')