def zzz(ctx, force=False): """ Step 6: Cleanup. Remove local branches and close Jira ticket :param force: Force delete the local branch even if it's not fully merged """ helpers.check_mongo_repo_root() ticket_conf = helpers.get_ticket_conf(ctx) feature_branch = git.cur_branch_name(ctx) base_branch = ticket_conf.base_branch get_logger().info(f'🍦 Congrats on completing {feature_branch.upper()}! 🍦') input(actionable(f'Press any key to remove local branches and close the Jira ticket')) config.Config().in_progress_tickets.pop(feature_branch) ctx.run(f'git checkout {base_branch}') try: if force: ctx.run(f'git branch -D {feature_branch}') else: ctx.run(f'git branch --delete {feature_branch}') except UnexpectedExit as e: get_logger().error(e) get_logger().error(f'Failed to delete branch, please manually delete your local branch {feature_branch}') jira.transition_ticket( feature_branch.upper(), 'In Code Review', 'Close Issue' )
def get_ticket_conf(ctx): branch = cur_branch_name(ctx) ticket_conf = config.Config().in_progress_tickets.get(branch) if not ticket_conf: get_logger().critical( 'Unknown branch "%s". Please ensure the branch is created with the "start" command', branch) raise InvalidConfigError() return ticket_conf
def upload(existing_cr, repo_name): has_changes = ctx.run(f'git diff {ticket_conf.base_branch}').stdout.strip() if has_changes: get_logger().info(f'Submitting code review for the {repo_name} repo') else: get_logger().info(f'There are no changes in the {repo_name} repository, skipping code review') return cmd = f'python {str(config.UPLOAD_PY)} --rev {ticket_conf.base_branch}...' # Yes three dots. cmd += ' --nojira -y --git_similarity 90 --check-clang-format --check-eslint' if existing_cr is not None: # Continue an existing CR. cmd += f' -i {existing_cr}' else: # Start a new CR. commit_msg = ctx.run('git log -1 --pretty=%B').stdout.strip() cr_title = input(f'Please enter the title for this code review (without the ticket number). ' f'Default: {commit_msg}') if not cr_title: cr_title = commit_msg else: # Add the ticket number to the description. cr_title = f'{git.cur_branch_name(ctx).upper()} {commit_msg}' cmd += f' -t "{cr_title}"' get_logger().info('Opening browser to authenticate with OAuth2... ') # Simulate some newline inputs to get the browser to open. sim_stdin = io.StringIO(initial_value='\n\n') res = ctx.run(cmd, in_stream=sim_stdin) if existing_cr is None: cr_url = re.search('Issue created. URL: (.*)', res.stdout.strip()).group(1) cr_issue_number = cr_url.split('/')[-1] get_logger().info(f'Code review created: {cr_url}') ticket_number = git.cur_branch_name(ctx).upper() jira.transition_ticket(ticket_number, 'In Progress', 'Start Code Review') jira.add_comment( ticket_number, f'Code Review: {cr_url}', visibility={'type': 'role', 'value': 'Developers'} ) return cr_issue_number else: get_logger().info(f'Code review updated') return existing_cr
def do_commit(repo): has_changes = ctx.run('git diff HEAD').stdout.strip() if has_changes: branch = git.cur_branch_name(ctx) ctx.run('git add -u', echo=True, hide=False) ctx.run(f'git commit -m "{branch.upper()} {raw_commit_msg}"', echo=True, hide=False) commit_hash = ctx.run('git rev-parse --verify HEAD').stdout.strip() if repo == 'community': commit_info.community = commit_hash elif repo == 'enterprise': commit_info.enterprise = commit_hash else: raise ValueError('Unknown repo type %s', repo) else: get_logger().info('No changes found in repo: %s', repo)
def delete_branch(ctx): """ Delete the current branch on the community and enterprise repos. It's usually harmless to keep local branches around. But this command is available if you want to delete a branch for any reason. """ check_mongo_repo_root() branch = cur_branch_name(ctx) ticket_conf = get_ticket_conf(ctx) def run(): ctx.run(f'git checkout {ticket_conf.base_branch}') ctx.run(f'git branch --delete --force {branch}', echo=True, hide=False) run() with ctx.cd(git.ent_repo_rel_path): run() config.Config().in_progress_tickets.pop(branch)
def ship(ctx): """ Step 5: Provide instructions on pushing your changes to master Also add the URL of the latest patch build to the Jira ticket. """ helpers.check_mongo_repo_root() ticket_conf = helpers.get_ticket_conf(ctx) cur_branch = git.cur_branch_name(ctx) if ticket_conf.patch_ids: jira.add_comment( cur_branch.upper(), f'Patch Build: {urllib.parse.urljoin(config.EVG_PATCH_URL_BASE, ticket_conf.patch_ids[-1])}', visibility={'type': 'role', 'value': 'Developers'} ) else: get_logger().warning('No patch builds were created for this ticket, not adding patch build URL to Jira') # This will implicitly check for uncommitted changes on the feature branch git.refresh_repos(ctx, ticket_conf.base_branch) lines = [ actionable('Please run the following commands to push your changes to the upstream MongoDB repository:'), '', f' git rebase --interactive {ticket_conf.base_branch}', f' git checkout {ticket_conf.base_branch}', f' git merge --ff-only {cur_branch}', f' git push origin {ticket_conf.base_branch} --dry-run', f' git push origin {ticket_conf.base_branch}', '', 'As part of `git rebase --interactive`, you should squash your local commits into one commit. Please refer to', 'this guide for an intro to interactive rebase: https://git-scm.com/docs/git-rebase#_interactive_mode', '', 'If you encounter errors during any of the above steps, please ask your mentor for advice.', '', 'Finally, when you\'ve pushed your changes, run `workflow zzz` to delete your local branches' ] log.log_multiline(get_logger().info, lines)