コード例 #1
0
def main():
    args = parse_args()
    if args.verbose:
        print('[INFO] args: ' + str(args))

    global config
    result = config.initialize(args)
    if result != 0:
        return result

    result = fetch_origin_check_staged(BRAVE_CORE_ROOT)
    if result != 0:
        return result

    # get all channel branches (starting at master)
    brave_browser_version = get_remote_version('master')
    remote_branches = get_remote_channel_branches(brave_browser_version)
    top_level_base = 'master'

    # if starting point is NOT nightly, remove options which aren't desired
    # also, find the branch which should be used for diffs (for cherry-picking)
    if not is_nightly(args.start_from):
        top_level_base = remote_branches[args.start_from]
        try:
            start_index = config.channel_names.index(args.start_from)
            config.channels_to_process = config.channel_names[start_index:]
        except Exception as e:
            print('[ERROR] specified `start-from` value "' + args.start_from +
                  '" not found in channel list')
            return 1

    # optionally (instead of having a local branch), allow uplifting a specific PR
    # this pulls down the pr locally (in a special branch)
    if args.uplift_using_pr:
        try:
            pr_number = int(args.uplift_using_pr)
            repo = GitHub(config.github_token).repos(BRAVE_CORE_REPO)
            # get enough details from PR to check out locally
            response = repo.pulls(pr_number).get()
            head = response['head']
            local_branch = 'pr' + str(pr_number) + '_' + head['ref']
            head_sha = head['sha']
            top_level_base = response['base']['ref']
            top_level_sha = response['base']['sha']
            merged_at = str(response['merged_at']).strip()
            config.title = str(response['title']).strip()

        except Exception as e:
            print(
                '[ERROR] Error parsing or error returned from API when looking up pull request "'
                + str(args.uplift_using_pr) + '":\n' + str(e))
            return 1

        # set starting point AHEAD of the PR provided
        config.master_pr_number = pr_number
        if top_level_base == 'master':
            config.channels_to_process = config.channel_names[1:]
        else:
            branch_index = remote_branches.index(top_level_base)
            config.channels_to_process = config.channel_names[branch_index:]

        # if PR was already merged, use the SHA it was PRed against
        if merged_at != 'None' and len(merged_at) > 0:
            print('pr was already merged at ' + merged_at + '; using "' +
                  top_level_sha + '" instead of "' + top_level_base + '"')
            top_level_base = top_level_sha
        else:
            # don't allow uplift of PRs which are not merged
            print('[ERROR] Pull request ' + str(pr_number) +
                  ' has not been merged yet. ' +
                  'Only merged requests can be uplifted.')
            return 1

        # create local branch which matches the contents of the PR
        with scoped_cwd(BRAVE_CORE_ROOT):
            # check if branch exists already
            try:
                branch_sha = execute(
                    ['git', 'rev-parse', '-q', '--verify', local_branch])
            except Exception as e:
                branch_sha = ''
            if len(branch_sha) > 0:
                # branch exists; reset it
                print('branch "' + local_branch +
                      '" exists; resetting to origin/' + head['ref'] + ' (' +
                      head_sha + ')')
                execute(['git', 'checkout', local_branch])
                execute(['git', 'reset', '--hard', head_sha])
            else:
                # create the branch
                print('creating branch "' + local_branch + '" using origin/' +
                      head['ref'] + ' (' + head_sha + ')')
                execute(['git', 'checkout', '-b', local_branch, head_sha])

    # If title isn't set already, generate one from first commit
    local_branch = get_local_branch_name(BRAVE_CORE_ROOT)
    if not config.title and not args.uplift_using_pr:
        config.title = get_title_from_first_commit(BRAVE_CORE_ROOT,
                                                   top_level_base)

    # Create a branch for each channel
    print('\nCreating branches...')
    fancy_print('NOTE: Commits are being detected by diffing "' +
                local_branch + '" against "' + top_level_base + '"')
    local_branches = {}
    branch = ''
    try:
        for channel in config.channels_to_process:
            branch = create_branch(channel, top_level_base,
                                   remote_branches[channel], local_branch)
            local_branches[channel] = branch
            if channel == args.uplift_to:
                break
    except Exception as e:
        print('[ERROR] cherry-pick failed for branch "' + branch +
              '". Please resolve manually:\n' + str(e))
        return 1

    print('\nPushing local branches to remote...')
    push_branches_to_remote(BRAVE_CORE_ROOT,
                            config.branches_to_push,
                            dryrun=config.is_dryrun,
                            token=config.github_token)

    try:
        print('\nCreating the pull requests...')
        for channel in config.channels_to_process:
            submit_pr(channel, top_level_base, remote_branches[channel],
                      local_branches[channel])
            if channel == args.uplift_to:
                break
        print('\nDone!')
    except Exception as e:
        print('\n[ERROR] Unhandled error while creating pull request; ' +
              str(e))
        return 1

    return 0
コード例 #2
0
ファイル: uplift.py プロジェクト: Snuupy/brave-core
def main():
    args = parse_args()
    if args.verbose:
        print('[INFO] args: ' + str(args))

    global config
    result = config.initialize(args)
    if result != 0:
        return result

    result = fetch_origin_check_staged(BRAVE_CORE_ROOT)
    if result != 0:
        return result

    # get all channel branches (starting at master)
    brave_browser_version = get_remote_version('master')
    remote_branches = get_remote_channel_branches(brave_browser_version)
    top_level_base = 'master'

    # if starting point is NOT nightly, remove options which aren't desired
    # also, find the branch which should be used for diffs (for cherry-picking)
    if not is_nightly(args.start_from):
        top_level_base = remote_branches[args.start_from]
        try:
            start_index = config.channel_names.index(args.start_from)
            config.channels_to_process = config.channel_names[start_index:]
        except Exception as e:
            print('[ERROR] specified `start-from` value "' + args.start_from + '" not found in channel list')
            return 1

    # optionally (instead of having a local branch), allow uplifting a specific PR
    # this pulls down the pr locally (in a special branch)
    if args.uplift_using_pr:
        try:
            pr_number = int(args.uplift_using_pr)
            repo = GitHub(config.github_token).repos(BRAVE_CORE_REPO)
            # get enough details from PR to check out locally
            response = repo.pulls(pr_number).get()
            head = response['head']
            local_branch = 'pr' + str(pr_number) + '_' + head['ref']
            head_sha = head['sha']
            top_level_base = response['base']['ref']
            top_level_sha = response['base']['sha']
            merged_at = str(response['merged_at']).strip()
            config.title = str(response['title']).strip()

        except Exception as e:
            print('[ERROR] Error parsing or error returned from API when looking up pull request "' +
                  str(args.uplift_using_pr) + '":\n' + str(e))
            return 1

        # set starting point AHEAD of the PR provided
        config.master_pr_number = pr_number
        if top_level_base == 'master':
            config.channels_to_process = config.channel_names[1:]
        else:
            branch_index = remote_branches.index(top_level_base)
            config.channels_to_process = config.channel_names[branch_index:]

        # if PR was already merged, use the SHA it was PRed against
        if merged_at != 'None' and len(merged_at) > 0:
            print('pr was already merged at ' + merged_at + '; using "' + top_level_sha +
                  '" instead of "' + top_level_base + '"')
            top_level_base = top_level_sha
        else:
            # don't allow uplift of PRs which are not merged
            print('[ERROR] Pull request ' + str(pr_number) + ' has not been merged yet. ' +
                  'Only merged requests can be uplifted.')
            return 1

        # create local branch which matches the contents of the PR
        with scoped_cwd(BRAVE_CORE_ROOT):
            # check if branch exists already
            try:
                branch_sha = execute(['git', 'rev-parse', '-q', '--verify', local_branch])
            except Exception as e:
                branch_sha = ''
            if len(branch_sha) > 0:
                # branch exists; reset it
                print('branch "' + local_branch + '" exists; resetting to origin/' + head['ref'] +
                      ' (' + head_sha + ')')
                execute(['git', 'checkout', local_branch])
                execute(['git', 'reset', '--hard', head_sha])
            else:
                # create the branch
                print('creating branch "' + local_branch + '" using origin/' + head['ref'] + ' (' + head_sha + ')')
                execute(['git', 'checkout', '-b', local_branch, head_sha])

    # If title isn't set already, generate one from first commit
    local_branch = get_local_branch_name(BRAVE_CORE_ROOT)
    if not config.title and not args.uplift_using_pr:
        config.title = get_title_from_first_commit(BRAVE_CORE_ROOT, top_level_base)

    # Create a branch for each channel
    print('\nCreating branches...')
    fancy_print('NOTE: Commits are being detected by diffing "' + local_branch + '" against "' + top_level_base + '"')
    local_branches = {}
    branch = ''
    try:
        for channel in config.channels_to_process:
            branch = create_branch(channel, top_level_base, remote_branches[channel], local_branch, args)
            local_branches[channel] = branch
            if channel == args.uplift_to:
                break
    except Exception as e:
        print('[ERROR] cherry-pick failed for branch "' + branch + '". Please resolve manually:\n' + str(e))
        return 1

    print('\nPushing local branches to remote...')
    push_branches_to_remote(BRAVE_CORE_ROOT, config.branches_to_push,
                            dryrun=config.is_dryrun, token=config.github_token)

    try:
        print('\nCreating the pull requests...')
        for channel in config.channels_to_process:
            submit_pr(
                channel,
                top_level_base,
                remote_branches[channel],
                local_branches[channel])
            if channel == args.uplift_to:
                break
        print('\nDone!')
    except Exception as e:
        print('\n[ERROR] Unhandled error while creating pull request; ' + str(e))
        return 1

    return 0