コード例 #1
0
def ticket(branch, ticket, jira_username, jira_token, jenkins_username,
           jenkins_token, github_username, github_token, force):
    "Tells you if a ticket has passed smoke test and has been promoted into a build"

    default_branch = releases['default'][branch]
    issue = Ticket(ticket, branch, jira_username, jira_token, github_username,
                   github_token)
    project = Project(jira_username, jira_token)
    log.debug('Ticket ID: {}'.format(issue.id))
    server = Jenkins(jenkins_username, jenkins_token)
    enterprise_dist = EnterpriseDist(branch)
    vanagon = PeModulesVanagon(branch)

    # Parse PR's into format DataFrame can be created from
    # First PR commit is the merge commit
    pr_commits = [{
        'pr': str(pr),
        'pr_number': pr.number,
        'repo': pr.repo.name,
        'commit': pr.commits[0],
        'is_merged': pr.is_merged
    } for pr in issue.pull_requests]
    pull_requests = pandas.DataFrame(
        pr_commits, columns=['pr', 'pr_number', 'repo', 'commit', 'is_merged'])
    if not pull_requests['is_merged'].all():
        click.echo("Not all PR's for this ticket have been merged")
        exit(1)

    # Build numbers that have passed smoke tests and the Enterprise Dist commit
    build_numbers = pandas.DataFrame(server.smoke_tests(default_branch),
                                     columns=['build_number'])
    build_numbers['commit'] = build_numbers['build_number'].apply(
        parse_build_description)

    # Build numbers that were promoted and the Enterprise Dist commit
    promotion_build_numbers = pandas.DataFrame(
        server.promotions(default_branch), columns=['build_number'])
    promotion_build_numbers['commit'] = promotion_build_numbers[
        'build_number'].apply(parse_build_description)

    # Enterprise Dist commits
    # short_sha corresponds to build number commits. Doing this instead of
    # doing a merge based on starts with.
    enterprise_dist = pandas.DataFrame(enterprise_dist.commits())
    enterprise_dist['date'] = pandas.to_datetime(enterprise_dist['date'])
    enterprise_dist[['repo', 'rc', 'git_ref'
                     ]] = enterprise_dist['message'].apply(extract_repo_commit)
    enterprise_dist['short_sha'] = enterprise_dist['sha'].apply(
        lambda s: s[0:7])

    # Vanagon commits
    vanagon_commits = pandas.DataFrame(vanagon.commits())
    vanagon_commits[['repo', 'rc', 'git_ref'
                     ]] = vanagon_commits['message'].apply(extract_repo_commit)

    # Join all data together
    enterprise_dist = join(enterprise_dist, vanagon_commits, build_numbers,
                           promotion_build_numbers, pull_requests)

    # Filter for commits that have a matching PR
    pr_rows = enterprise_dist[enterprise_dist['commit_pr'].notnull()]

    if not pr_rows.empty and (force
                              or pr_rows['build_number_pbn'].notnull().all()):
        passed_smoke_bn = pr_rows['build_number_bn'].iloc[0]
        passed_smoke_message = 'Ticket {} passed smoke test in build {}'.format(
            ticket, passed_smoke_bn)
        click.echo(passed_smoke_message)

        promoted_bn = pr_rows['build_number_pbn'].iloc[0]
        promoted_message = 'Ticket {} was promoted in build {}'.format(
            ticket, promoted_bn)
        click.echo(promoted_message)

    else:
        click.echo("Unable to find builds for all PR's in this ticket")
        exit(1)

    if click.confirm('Do you wish to update ticket {}?'.format(ticket)):
        if not issue.fix_build:
            issue.fix_build = promoted_bn

        versions = project.fix_versions
        version_choices = [
            '{}. {}'.format(x + 1, v['name']) for x, v in enumerate(versions)
        ]
        click.echo('\n'.join(version_choices))
        fix_version = click.prompt(
            'Please choose which version this change is targetting', type=int)
        click.echo('You chose: {}'.format(versions[fix_version - 1]))
        issue.fix_versions = versions[fix_version - 1]
        issue.comment('\n'.join([passed_smoke_message, promoted_message]))
        click.echo('Comment added')