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 create_release_notes( from_component: gci.componentmodel.Component, from_github_cfg, from_repo_owner: str, from_repo_name: str, to_version: str, ): from_version = from_component.version try: with tempfile.TemporaryDirectory() as temp_dir: gitutil.GitHelper.clone_into( target_directory=temp_dir, github_cfg=from_github_cfg, github_repo_path=f'{from_repo_owner}/{from_repo_name}') commit_range = '{from_version}..{to_version}'.format( from_version=from_version, to_version=to_version, ) release_notes = ReleaseNotes( component=from_component, repo_dir=temp_dir, ) release_notes.create( start_ref=None, # the repo's default branch commit_range=commit_range) release_note_blocks = release_notes.release_note_blocks() if release_note_blocks: return f'**Release Notes**:\n{release_note_blocks}' except: logger.warning( 'an error occurred during release notes processing (ignoring)') import traceback logger.warning(traceback.format_exc())
def test_multiple_release_note_objs_to_block_str(self): hostname = 'github.com' org_name = 's' repo_name = 'repo' rls_note_objs = [ release_note_block_with_defaults(), release_note_block_with_defaults( reference_type=REF_TYPE_COMMIT, reference_id='commit-id', text='another one', source_component_hostname=hostname, source_component_org_name=org_name, source_component_repo_name=repo_name, ), ] exp_release_note_block = \ '``` improvement user github.com/madeup/current-repo #42 @foo\n'\ 'default release note text'\ '\n```'\ '\n'\ '\n'\ f'``` improvement user {hostname}/{org_name}/{repo_name} $commit-id @foo\n'\ 'another one'\ '\n```' release_notes = ReleaseNotes( component=CURRENT_COMPONENT, repo_dir='', ) release_notes.release_note_objs = rls_note_objs self.assertEqual( exp_release_note_block, release_notes.release_note_blocks(), )
def test_no_release_note_obj_to_block_str(self): rls_note_objs = [] exp_release_note_block = '' self.assertEqual(exp_release_note_block, ReleaseNotes(rls_note_objs).release_note_blocks()) rls_note_objs = None exp_release_note_block = '' self.assertEqual(exp_release_note_block, ReleaseNotes(rls_note_objs).release_note_blocks())
def test_single_release_note_obj_to_block_str(self): rls_note_objs = [release_note_block_with_defaults()] exp_release_note_block = \ '``` improvement user github.com/madeup/current-repo #42 @foo\n'\ 'default release note text'\ '\n```' release_notes = ReleaseNotes( component=CURRENT_COMPONENT, repo_dir='', ) release_notes.release_note_objs = rls_note_objs self.assertEqual(exp_release_note_block, release_notes.release_note_blocks())
def test_single_release_note_obj_to_block_str(self): rls_note_objs = [release_note_block_with_defaults()] exp_release_note_block = \ '``` improvement user github.com/madeup/current-repo #42 @foo\n'\ 'default release note text'\ '\n```' self.assertEqual(exp_release_note_block, ReleaseNotes(rls_note_objs).release_note_blocks())
def _examinee( slack_cfg_name='test_config', slack_channel='test_channel', githubrepobranch=GitHubRepoBranch( github_config='test_config', repo_owner='test_owner', repo_name='test_name', branch='master', ), release_notes=ReleaseNotes(None), release_version='1.0.0', ): return concourse.steps.release.PostSlackReleaseStep( slack_cfg_name=slack_cfg_name, slack_channel=slack_channel, githubrepobranch=githubrepobranch, release_notes=release_notes, release_version=release_version, )
def test_multiple_release_note_objs_to_block_str(self): rls_note_objs = [ release_note_block_with_defaults(), release_note_block_with_defaults( reference_type=REF_TYPE_COMMIT, reference_id='commit-id', text='another one', source_repo='github.com/s/repo', ), ] exp_release_note_block = \ '``` improvement user github.com/madeup/current-repo #42 @foo\n'\ 'default release note text'\ '\n```'\ '\n'\ '\n'\ '``` improvement user github.com/s/repo $commit-id @foo\n'\ 'another one'\ '\n```' self.assertEqual(exp_release_note_block, ReleaseNotes(rls_note_objs).release_note_blocks())
def test_no_release_note_obj_to_block_str(self): rls_note_objs = [] exp_release_note_block = '' empty_notes = ReleaseNotes(CURRENT_COMPONENT, '') empty_notes.release_note_objs = rls_note_objs self.assertEqual( exp_release_note_block, empty_notes.release_note_blocks(), ) empty_notes.release_note_objs = None exp_release_note_block = '' self.assertEqual( exp_release_note_block, empty_notes.release_note_blocks(), )
def create_upgrade_pr( from_ref, to_ref, pull_request_util, upgrade_script_path, githubrepobranch: GitHubRepoBranch, repo_dir, github_cfg_name, cfg_factory, merge_policy, after_merge_callback=None, ): ls_repo = pull_request_util.repository # have component create upgradation diff cmd_env = os.environ.copy() cmd_env['DEPENDENCY_TYPE'] = to_ref.type_name() cmd_env['DEPENDENCY_NAME'] = to_ref.name() cmd_env['DEPENDENCY_VERSION'] = to_ref.version() cmd_env['REPO_DIR'] = repo_dir cmd_env['GITHUB_CFG_NAME'] = github_cfg_name # pass type-specific attributes if to_ref.type_name() == 'container_image': cmd_env['DEPENDENCY_IMAGE_REFERENCE'] = to_ref.image_reference() subprocess.run( [str(upgrade_script_path)], check=True, env=cmd_env ) commit_msg = 'Upgrade {cn}\n\nfrom {ov} to {nv}'.format( cn=to_ref.name(), ov=from_ref.version(), nv=to_ref.version(), ) # 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_msg) ci.util.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 ls_repo.create_ref(f'refs/heads/{new_branch_name}', head_sha) def rm_pr_branch(): ls_repo.ref(f'heads/{new_branch_name}').delete() try: helper.push(from_ref=commit.hexsha, to_ref=f'refs/heads/{new_branch_name}') except: ci.util.warning('an error occurred - removing now useless pr-branch') rm_pr_branch() raise helper.repo.git.checkout('.') try: with tempfile.TemporaryDirectory() as temp_dir: from_github_cfg = cfg_factory.github(from_ref.config_name()) from_github_helper = GitHubRepositoryHelper( github_cfg=from_github_cfg, owner=from_ref.github_organisation(), name=from_ref.github_repo(), ) from_git_helper = gitutil.GitHelper.clone_into( target_directory=temp_dir, github_cfg=from_github_cfg, github_repo_path=from_ref.github_repo_path() ) commit_range = '{from_version}..{to_version}'.format( from_version=from_ref.version(), to_version=to_ref.version() ) release_note_blocks = ReleaseNotes.create( github_helper=from_github_helper, git_helper=from_git_helper, commit_range=commit_range ).release_note_blocks() if release_note_blocks: text = '*Release Notes*:\n{blocks}'.format( blocks=release_note_blocks ) else: text = pull_request_util.retrieve_pr_template_text() except: ci.util.warning('an error occurred during release notes processing (ignoring)') text = None import traceback ci.util.warning(traceback.format_exc()) pull_request = ls_repo.create_pull( title=github.util.PullRequestUtil.calculate_pr_title( reference=to_ref, from_version=from_ref.version(), to_version=to_ref.version() ), base=repo_branch, head=new_branch_name, body=text, ) if merge_policy is MergePolicy.MANUAL: return # auto-merge - todo: make configurable (e.g. merge method) pull_request.merge() rm_pr_branch() if after_merge_callback: subprocess.run( [os.path.join(repo_dir, after_merge_callback)], check=True, env=cmd_env )