Ejemplo n.º 1
0
def main():
    config = get_config()
    git_tool = GitTool()

    filter_group = config.group if config.group else None

    git_tool.generate_install_locally(filter_group=filter_group)
def main():
    git_tool = GitTool()
    config = get_config()

    upstream_name = config.upstream
    lst_repo = git_tool.get_repo_info(config.dir, add_root=True)
    i = 0
    total = len(lst_repo)
    for repo in lst_repo:
        i += 1
        print(f"Nb element {i}/{total}")
        repo_name = repo.get("name")
        relative_path = os.path.join(new_path, repo_name)
        if not os.path.isdir(relative_path):
            print(f"Ignore repo {relative_path}")
            continue
        repo_sm = Repo(repo_name)
        if upstream_name:
            remote_upstream_name = [
                a for a in repo_sm.remotes if upstream_name == a.name
            ]
        else:
            remote_upstream_name = [a for a in repo_sm.remotes]

        for remote in remote_upstream_name:
            url, url_https, url_git = git_tool.get_url(remote.url)
            new_url = url_https if config.git_to_https else url_git
            remote.set_url(new_url)
            print(f'Remote "{remote.name}" update for {new_url}')
Ejemplo n.º 3
0
def main():
    config = get_config()
    git_tool = GitTool()

    (
        dct_remote_1,
        dct_project_1,
        default_remote_1,
    ) = git_tool.get_manifest_xml_info(filename=config.input1, add_root=True)
    (
        dct_remote_2,
        dct_project_2,
        default_remote_2,
    ) = git_tool.get_manifest_xml_info(filename=config.input2, add_root=True)

    set_project_1 = set(dct_project_1.keys())
    set_project_2 = set(dct_project_2.keys())
    lst_same_name_normalize = set_project_1.intersection(set_project_2)
    lst_missing_name_normalize = set_project_2.difference(set_project_1)
    lst_over_name_normalize = set_project_1.difference(set_project_2)

    i = 0
    total = len(lst_same_name_normalize)
    for key in lst_missing_name_normalize:
        i += 1
        print(f"{i}/{total} - {key} from input1 not in input2.")

    i = 0
    total = len(lst_over_name_normalize)
    for key in lst_over_name_normalize:
        i += 1
        print(f"{i}/{total} - {key} from input2 not in input1.")

    i = 0
    total = len(lst_same_name_normalize)
    for key in lst_same_name_normalize:
        value1 = dct_project_1.get(key)
        value2 = dct_project_2.get(key)
        old_revision = value1.get("@revision", git_tool.default_branch)
        new_revision = value2.get("@revision", git_tool.default_branch)

        path1 = value1.get("@path")
        path2 = value2.get("@path")
        if path1 != path2:
            print(f"WARNING id {i}, path of git are different. "
                  f"Input1 {path1}, input2 {path2}")
            continue

        i += 1
        result = "same" if old_revision == new_revision else "diff"
        print(f"{i}/{total} - {result} - "
              f"{path1} {key} old {old_revision} new {new_revision}")
        default_arg = [f"{old_revision}..{new_revision}"]
        if old_revision != new_revision:
            # get git diff
            repo = Repo(path1)
            status = repo.git.diff(*default_arg)
            print(status)
Ejemplo n.º 4
0
def main():
    config = get_config()
    git_tool = GitTool()

    lst_repo = git_tool.get_source_repo_addons(repo_path=config.dir,
                                               add_repo_root=True)
    lst_repo_organization = [
        git_tool.get_transformed_repo_info_from_url(
            a.get("url"),
            repo_path=config.dir,
            get_obj=True,
            is_submodule=a.get("is_submodule"),
            sub_path=a.get("sub_path"),
            revision=a.get("revision"),
            clone_depth=a.get("clone_depth")) for a in lst_repo
    ]

    # Update origin to new repo
    if not config.clear:
        dct_remote, dct_project, _ = git_tool.get_manifest_xml_info(
            repo_path=config.dir, add_root=True)
    else:
        dct_remote = {}
        dct_project = {}
    git_tool.generate_repo_manifest(lst_repo_organization,
                                    output=f"{config.dir}{config.manifest}",
                                    dct_remote=dct_remote,
                                    dct_project=dct_project,
                                    keep_original=True)
    git_tool.generate_install_locally()
Ejemplo n.º 5
0
def main():
    config = get_config()
    git_tool = GitTool()

    lst_repo = git_tool.get_source_repo_addons(repo_path=config.dir,
                                               add_repo_root=False)
    lst_repo_organization = [
        git_tool.get_transformed_repo_info_from_url(
            a.get("url"),
            repo_path=config.dir,
            get_obj=True,
            is_submodule=a.get("is_submodule"),
            sub_path=a.get("sub_path"),
            revision=a.get("revision"),
            clone_depth=a.get("clone_depth"),
        ) for a in lst_repo
    ]

    lst_ignore_repo = ["odoo"]

    i = 0
    total = len(lst_repo)
    branch_name = "12.0_dev"
    for repo in lst_repo_organization:
        i += 1
        print(f"\nNb element {i}/{total} - {repo.path}")
        is_checkout_branch = False

        if repo.repo_name in lst_ignore_repo:
            print(f"Ignore {repo.repo_name}.")
            continue

        git_repo = Repo(repo.relative_path)
        # Force checkout branch if exist
        try:
            git_repo.git.checkout(branch_name)
            is_checkout_branch = True
        except GitCommandError:
            try:
                git_repo.git.checkout("-t",
                                      f"{repo.organization}/{branch_name}")
                is_checkout_branch = True
            except GitCommandError:
                pass

        has_change = get_manifest_external_dependencies(repo)

        if has_change:
            if not is_checkout_branch:
                git_repo.git.checkout("-b", branch_name)
            # change branch, commit and push
            git_repo.git.add(".")
            git_repo.git.commit("-m", "Set all module auto_install at False")
            git_repo.git.push("-u", repo.organization, branch_name)
Ejemplo n.º 6
0
def get_config():
    """Parse command line arguments, extracting the config file name,
    returning the union of config file and command line arguments

    :return: dict of config file settings and command line arguments
    """
    config = GitTool.get_project_config()

    # TODO update description
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description='''\
''',
        epilog='''\
'''
    )
    parser.add_argument('-d', '--dir', dest="dir", default="./",
                        help="Path of repo to change remote, including submodule.")
    parser.add_argument('--organization', dest="organization", default="ERPLibre",
                        help="Choose organization to fork and change all repo.")
    parser.add_argument("--skip_fork", action="store_true",
                        help="Ignore fork to generate only manifest.")
    parser.add_argument('-f', "--force", action="store_true",
                        help="Force rewrite remote.")
    parser.add_argument("--fast", action="store_true",
                        help="Ignore if repo already exist.")
    parser.add_argument('--github_token', dest="github_token",
                        default=config.get(CST_EL_GITHUB_TOKEN),
                        help="GitHub token generated by user")
    args = parser.parse_args()
    return args
def get_config():
    """Parse command line arguments, extracting the config file name,
    returning the union of config file and command line arguments

    :return: dict of config file settings and command line arguments
    """
    config = GitTool.get_project_config()

    # TODO update description
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""\
        Transform a xml file in code writer format xml file.
""",
        epilog="""\
""",
    )
    parser.add_argument(
        "-f",
        "--file",
        dest="file",
        required=True,
        help="Path of file to transform to code_writer.",
    )
    parser.add_argument(
        "-o", "--output", dest="output", help="The output file."
    )
    args = parser.parse_args()
    return args
Ejemplo n.º 8
0
def get_config():
    """Parse command line arguments, extracting the config file name,
    returning the union of config file and command line arguments

    :return: dict of config file settings and command line arguments
    """
    config = GitTool.get_project_config()

    # TODO update description
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description='''\
        Update version of docker ready to commit.
''',
        epilog='''\
''')
    parser.add_argument('--version',
                        required=True,
                        help="Version of ERPLibre.")
    parser.add_argument('--base', required=True, help="Docker base name.")
    parser.add_argument('--prod', required=True, help="Docker prod name.")
    parser.add_argument('--docker_compose_file',
                        default="./docker-compose.yml",
                        help="Docker compose file to update.")
    parser.add_argument('--docker_prod',
                        default="./docker/Dockerfile.prod.pkg",
                        help="Docker prod file to update.")
    args = parser.parse_args()
    args.base_version = f"{args.base}:{args.version}"
    args.prod_version = f"{args.prod}:{args.version}"
    return args
Ejemplo n.º 9
0
def main():
    config = get_config()
    github_token = config.github_token
    git_tool = GitTool()

    if not github_token:
        raise ValueError("Missing github_token")

    organization_name = config.organization
    lst_repo = git_tool.get_source_repo_addons(repo_path=config.dir,
                                               add_repo_root=True)
    lst_repo_organization = [
        git_tool.get_transformed_repo_info_from_url(
            a.get("url"),
            repo_path=config.dir,
            organization_force=organization_name,
            is_submodule=a.get("is_submodule"),
            sub_path=a.get("sub_path"),
            revision=a.get("revision"),
            clone_depth=a.get("clone_depth"),
        ) for a in lst_repo
    ]

    url_not_found_count = 0
    url_found_count = 0

    i = 0
    total = len(lst_repo_organization)
    for repo in lst_repo_organization:
        i += 1
        print(f"Nb element {i}/{total} - {repo.project_name}")
        url = repo.url

        status = git_tool.get_pull_request_repo(
            upstream_url=url,
            github_token=github_token,
            organization_name=organization_name,
        )
        if status is False:
            url_not_found_count += 1
        else:
            url_found_count += len(status)

    print(f"Repository not found: {url_not_found_count}")
    print(f"URL found: {url_found_count}")
def main():
    config = get_config()
    git_tool = GitTool()

    dct_remote, dct_project, default_remote = git_tool.get_manifest_xml_info(
        filename=config.manifest, add_root=True)
    default_branch_name = default_remote.get("@revision",
                                             git_tool.default_branch)
    i = 0
    total = len(dct_project)
    for name, project in dct_project.items():
        i += 1
        path = project.get("@path")
        print(f"{i}/{total} - {path}")
        branch_name = project.get("@revision", default_branch_name)
        organization = project.get("@remote")
        if not organization:
            print(f"ERROR missing @remote on project {path}.")
            continue

        git_repo = Repo(path)
        value = git_repo.git.branch("--show-current")
        if not value:
            # TODO maybe need to check divergence with local branch and not remote branch
            commit_head = git_repo.git.rev_parse("HEAD")
            try:
                commit_branch = git_repo.git.rev_parse(
                    f"{organization}/{branch_name}")
            except GitCommandError:
                print("ERROR Something wrong with this repo.")
                continue
            if commit_branch != commit_head:
                print("WARNING Not on specified branch, got a divergence.")
            else:
                print("PASS Not on specified branch, no divergence.")
        elif branch_name != value:
            print(
                f"ERROR, manifest revision is {branch_name} and actual revision is {value}."
            )
        else:
            print("PASS")
Ejemplo n.º 11
0
def main():
    config = get_config()
    git_tool = GitTool()

    (
        dct_remote_1,
        dct_project_1,
        default_remote_1,
    ) = git_tool.get_manifest_xml_info(filename=config.input1, add_root=True)
    (
        dct_remote_2,
        dct_project_2,
        default_remote_2,
    ) = git_tool.get_manifest_xml_info(filename=config.input2, add_root=True)

    dct_remote_3 = copy.deepcopy(dct_remote_2)
    dct_project_3 = copy.deepcopy(dct_project_2)

    for key, value in dct_project_1.items():
        revision = value.get("@revision")
        if revision:
            dct_project_3[key]["@revision"] = revision
        else:
            dct_project_3[key]["@upstream"] = "12.0"
            dct_project_3[key]["@dest-branch"] = "12.0"

    # Update origin to new repo
    git_tool.generate_repo_manifest(
        dct_remote=dct_remote_3,
        dct_project=dct_project_3,
        output=config.output,
        default_remote=default_remote_2,
    )
def main():
    git_tool = GitTool()
    config = get_config()

    upstream_name = config.upstream
    lst_repo = git_tool.get_repo_info_submodule(config.dir,
                                                add_root=True,
                                                upstream=upstream_name)
    i = 0
    total = len(lst_repo)
    for repo in lst_repo:
        i += 1
        print(f"Nb element {i}/{total}")
        repo_sm = Repo(repo.get("name"))
        remote_upstream_name = [
            a for a in repo_sm.remotes if upstream_name == a.name
        ]
        new_url = repo.get("url_https") if config.git_to_https else repo.get(
            "url_git")

        for remote in remote_upstream_name:
            remote.set_url(new_url)
        print('Remote "%s" created for %s' % (upstream_name, new_url))
Ejemplo n.º 13
0
def main():
    config = get_config()
    git_tool = GitTool()

    dct_remote, dct_project, default_remote = git_tool.get_manifest_xml_info(
        filename=config.manifest, add_root=True)
    i = 0
    total = len(dct_project)
    for name, project in dct_project.items():
        i += 1
        path = project.get("@path")
        print(f"{i}/{total} - {path}")
        organization = project.get("@remote", git_tool.default_project_name)

        try:
            git_repo = Repo(path)
            retry(wait_exponential_multiplier=1000,
                  stop_max_delay=15000)(git_repo.git.push)(organization,
                                                           "--tags")
        except:
            print(
                f"{Fore.RED}ERROR{Style.RESET_ALL} cannot push --tags for path"
                f" {path} organization {organization}")
Ejemplo n.º 14
0
def get_config():
    """Parse command line arguments, extracting the config file name,
    returning the union of config file and command line arguments

    :return: dict of config file settings and command line arguments
    """
    config = GitTool.get_project_config()

    # TODO update description
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description='''\
        Update config.conf file with group specified in manifest file.
''',
        epilog='''\
'''
    )
    parser.add_argument('--group', default="",
                        help="Prod by default, use 'dev' for manifest/default.dev.xml")
    args = parser.parse_args()
    return args
Ejemplo n.º 15
0
def get_config():
    """Parse command line arguments, extracting the config file name,
    returning the union of config file and command line arguments

    :return: dict of config file settings and command line arguments
    """
    config = GitTool.get_project_config()

    # TODO update description
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""\
""",
        epilog="""\
""",
    )
    parser.add_argument(
        "-d",
        "--dir",
        dest="dir",
        default="./",
        help="Path of repo to change remote, including submodule.",
    )
    parser.add_argument(
        "--organization",
        dest="organization",
        default="ERPLibre",
        help="Choose organization to fork and change all repo.",
    )
    parser.add_argument(
        "--github_token",
        dest="github_token",
        default=config.get(CST_EL_GITHUB_TOKEN),
        help="GitHub token generated by user",
    )
    args = parser.parse_args()
    return args
Ejemplo n.º 16
0
def main():
    config = get_config()
    github_token = config.github_token
    git_tool = GitTool()
    root_repo = Repo(config.dir)

    if not github_token and not config.skip_fork:
        raise ValueError("Missing github_token")

    organization_name = config.organization
    lst_repo = git_tool.get_source_repo_addons(repo_path=config.dir,
                                               add_repo_root=True)
    lst_repo_organization = [
        git_tool.get_transformed_repo_info_from_url(
            a.get("url"),
            repo_path=config.dir,
            organization_force=organization_name,
            is_submodule=a.get("is_submodule"),
            sub_path=a.get("sub_path"),
            revision=a.get("revision"),
            clone_depth=a.get("clone_depth"),
        ) for a in lst_repo
    ]

    if not config.skip_fork:
        i = 0
        total = len(lst_repo)
        for repo in lst_repo:
            i += 1
            print(f"Nb element {i}/{total} - {repo.get('project_name')}")
            if (config.fast and repo.get("is_submodule")
                    and os.path.isdir(repo.get("path"))):
                continue
            url = repo.get("url")

            try:
                working_repo = Repo(repo.get("relative_path"))
            except git.exc.NoSuchPathError:
                # Check to fork
                # repo_info = git_tool.get_transformed_repo_info_from_url(
                #     url, organization_force="ERPLibre",
                #     is_submodule=repo.get("is_submodule"),
                #     sub_path=repo.get("sub_path"))
                git_tool.fork_repo(
                    upstream_url=url,
                    github_token=github_token,
                    organization_name="ERPLibre",
                )
                repo_info = git_tool.get_transformed_repo_info_from_url(
                    url,
                    organization_force=organization_name,
                    is_submodule=repo.get("is_submodule"),
                    sub_path=repo.get("sub_path"),
                )
                git_tool.fork_repo(
                    upstream_url=url,
                    github_token=github_token,
                    organization_name=organization_name,
                )

                # git_tool.add_and_fetch_remote(repo_info, root_repo=root_repo)
                continue

            dct_remote_name = {a.name: a for a in working_repo.remotes}
            remote_origin = dct_remote_name.get("origin")
            remote_erplibre = dct_remote_name.get("ERPLibre")
            remote_organization = dct_remote_name.get(organization_name)

            if not remote_erplibre:
                repo_info = git_tool.get_transformed_repo_info_from_url(
                    url,
                    organization_force="ERPLibre",
                    is_submodule=repo.get("is_submodule"),
                    sub_path=repo.get("sub_path"),
                )
                # git_tool.add_and_fetch_remote(repo_info)

            git_tool.fork_repo(
                url,
                github_token,
                organization_name=organization_name,
            )

            repo_info = git_tool.get_transformed_repo_info_from_url(
                url,
                organization_force=organization_name,
                is_submodule=repo.get("is_submodule"),
                sub_path=repo.get("sub_path"),
            )
            if remote_origin:
                working_repo.git.remote("remove", "origin")
            repo_info.organization = "origin"
            # try:
            #     git_tool.add_and_fetch_remote(repo_info, root_repo=root_repo)
            # except Exception as e:
            #     print(e)
            if config.force and remote_organization:
                working_repo.git.remote("remove", organization_name)

            repo_info.organization = organization_name
            # git_tool.add_and_fetch_remote(repo_info)

    # Update origin to new repo
    # git_tool.generate_git_modules(lst_repo_organization, repo_path=config.dir)
    git_tool.generate_repo_manifest(
        lst_repo_organization, output=f"{config.dir}manifest/default.dev.xml")
    git_tool.generate_generate_config()
Ejemplo n.º 17
0
def main():
    config = get_config()
    lst_repo = GitTool.get_repo_info_submodule(repo_path=config.dir)
    branch_search = "12.0"
    # repo = Repo(root_path)
    # repo_root = Repo(".")
    lst_result = []

    i = 0
    total = len(lst_repo)
    for repo in lst_repo:
        i += 1
        print(f"Nb element {i}/{total}")
        repo_dir_root = repo.get("path")
        remote_path = f"{config.dir}/{repo_dir_root}"
        repo_root = Repo(remote_path)
        repo_branch_search_sha = [
            a.object.hexsha for a in repo_root.branches
            if branch_search in a.name
        ]
        if repo_branch_search_sha:
            repo_branch_search_sha = repo_branch_search_sha[0]
        else:
            print(f"Error, missing branch {branch_search} in {remote_path}")
            continue
        lst_result.append(
            (remote_path, repo_root.head.commit.hexsha,
             repo_root.head.commit.hexsha != repo_branch_search_sha))

        # print(repo_root)

        # # Create the remote upstream
        # split_url = url.split("/")
        # split_url[-2] = upstream_name
        # upstream_url = "/".join(split_url)
        #
        # cloned_repo = Repo(repo_dir_root)
        # try:
        #     upstream_remote = cloned_repo.remote(upstream_name)
        #     print('Remote "%s" already exists in %s' %
        #           (upstream_name, repo_dir_root))
        # except ValueError:
        #     upstream_remote = retry(
        #         wait_exponential_multiplier=1000,
        #         stop_max_delay=15000
        #     )(cloned_repo.create_remote)(upstream_name, upstream_url)
        #     print('Remote "%s" created for %s' % (upstream_name, upstream_url))
        #
        # try:
        #     # Fetch the remote upstream
        #     retry(wait_exponential_multiplier=1000, stop_max_delay=15000)(
        #         upstream_remote.fetch)()
        #     print('Remote "%s" fetched' % upstream_name)
        # except Exception:
        #     print(f"ERROR git {repo_dir_root} remote {upstream_name} not exist.")
        #     upstream_remote.remove(upstream_remote, upstream_name)

    i = 0
    i_len = len(lst_result)
    for path, hash, diff in lst_result:
        i += 1
        if diff:
            print(f"{i:02d}/{i_len} {diff} {path}\t{hash}")
Ejemplo n.º 18
0
def main():
    config = get_config()
    git_tool = GitTool()

    lst_repo = git_tool.get_source_repo_addons(repo_path=config.dir,
                                               add_repo_root=False)
    lst_repo_organization = [
        git_tool.get_transformed_repo_info_from_url(
            a.get("url"),
            repo_path=config.dir,
            get_obj=True,
            is_submodule=a.get("is_submodule"),
            sub_path=a.get("sub_path"),
            revision=a.get("revision"),
            clone_depth=a.get("clone_depth"),
        ) for a in lst_repo
    ]

    i = 0
    total = len(lst_repo)
    for repo in lst_repo_organization:
        i += 1
        print(f"\nNb element {i}/{total} - {repo.path}")

        # TODO validate with default to ignore duplicate url, if same remote repo
        if not repo.is_submodule:
            continue
        upstream_name = f"ERPLibre_update_12/{git_tool.default_branch}"
        remote_branch_name = f"{upstream_name}/{git_tool.default_branch}"
        git_repo = Repo(repo.relative_path)
        # 1. Add remote if not exist
        try:
            upstream_remote = git_repo.remote(upstream_name)
            print(f'Remote "{upstream_name}" already exists in'
                  f" {repo.relative_path}")
        except ValueError:
            upstream_remote = retry(wait_exponential_multiplier=1000,
                                    stop_max_delay=15000)(
                                        git_repo.create_remote)(upstream_name,
                                                                repo.url_https)
            print('Remote "%s" created for %s' %
                  (upstream_name, repo.url_https))

        # 2. Fetch the remote source
        retry(wait_exponential_multiplier=1000,
              stop_max_delay=15000)(upstream_remote.fetch)()
        print('Remote "%s" fetched' % upstream_name)

        # 3. Rebase actual branch with new branch
        rev = repo.revision if repo.revision else git_tool.default_branch
        try:
            git_repo.git.checkout(rev)
        except:
            if repo.revision:
                rev = f"{repo.original_organization}/{repo.revision}"
            else:
                rev = f"{repo.original_organization}/{git_tool.default_branch}"

            git_repo.git.checkout("--track", rev)
        actual_commit = git_repo.git.rev_parse("HEAD")
        try:
            git_repo.git.rebase(remote_branch_name)
        except Exception as e:
            print(e, file=sys.stderr)
        new_commit = git_repo.git.rev_parse("HEAD")

        if actual_commit == new_commit:
            print("== No diff ==")
        else:
            print(f"== Old commit {actual_commit} - new commit {new_commit}")
            # push
            try:
                retry(wait_exponential_multiplier=1000, stop_max_delay=15000)(
                    git_repo.git.push)(repo.organization, rev)
            except:
                print(
                    "Cannot push, maybe need to push force or resolv rebase"
                    " conflict",
                    file=sys.stderr,
                )
                print(f"cd {repo.path}")
                print(f"git diff ERPLibre/v#.#.#..HEAD")
                print(f"git push --force {repo.organization} {rev}")
                print(f"cd -")
Ejemplo n.º 19
0
def main():
    config = get_config()
    github_token = config.github_token

    if not github_token:
        raise ValueError("Missing github_token")
    repo_root = Repo(config.dir)
    lst_repo = GitTool().get_source_repo_addons(repo_path=config.dir)
    i = 0
    total = len(lst_repo)
    for repo in lst_repo:
        i += 1
        print(f"Nb element {i}/{total}")
        url = repo.get("url")
        repo_dir_root = repo.get("path")
        branch_name = None
        upstream_name = "upstream"
        organization_name = config.organization

        # Exception, if repo existing, create appropriate remote to keep access
        # and remove origin
        # TODO do it
        # Don't overwrite upstream_name if already exist
        # Create origin of organization of upstream if not exist
        # Move actual origin to is organization
        working_repo = Repo(repo.get("relative_path"))

        dct_remote_name = {a.name: a for a in working_repo.remotes}
        remote_origin = dct_remote_name.get("origin")

        # for remote in working_repo.remotes:
        if remote_origin:
            repo_origin_info = GitTool.get_transformed_repo_info_from_url(
                remote_origin.url, repo_path=repo.get("relative_path"))

            if repo_origin_info.organization not in dct_remote_name.keys():
                # Add remote
                upstream_remote = retry(wait_exponential_multiplier=1000,
                                        stop_max_delay=15000)(
                                            working_repo.create_remote)(
                                                repo_origin_info.organization,
                                                repo_origin_info.url_https)
                print('Remote "%s" created for %s' % (
                    repo_origin_info.organization,
                    repo_origin_info.url_https,
                ))

                # Fetch the remote
                retry(wait_exponential_multiplier=1000,
                      stop_max_delay=15000)(upstream_remote.fetch)()
                print('Remote "%s" fetched' % repo_origin_info.organization)
            # working_repo.git.remote.remove("origin")
            upstream_name = organization_name
        # TODO a la première création, on choisit le URL source en cas de non existance de upstream,
        #  sinon on choisit celui de ERPLibre
        # [<git.Remote "origin">, <git.Remote "upstream">, <git.Remote "MathBenTech">]

        _logger.info(f"Fork {url} on dir {repo_dir_root} for organization"
                     f" {organization_name}")

        fork_github_repo.fork_and_clone_repo(
            url,
            github_token,
            repo_dir_root,
            branch_name=branch_name,
            upstream_name=upstream_name,
            organization_name=organization_name,
            # fork_only=True,
            repo_root=repo_root,
        )

        # Update origin to new repo

    GitTool().generate_generate_config()
Ejemplo n.º 20
0
def main():
    config = get_config()
    github_token = config.github_token
    git_tool = GitTool()
    root_repo = Repo(config.dir)

    if not github_token:
        raise ValueError("Missing github_token")

    organization_name = config.organization
    lst_repo = git_tool.get_source_repo_addons(repo_path=config.dir,
                                               add_repo_root=True)
    lst_repo_organization = [
        git_tool.get_transformed_repo_info_from_url(
            a.get("url"),
            repo_path=config.dir,
            organization_force=organization_name,
            is_submodule=a.get("is_submodule"),
            sub_path=a.get("sub_path")) for a in lst_repo
    ]
    i = 0
    total = len(lst_repo)
    for repo in lst_repo:
        i += 1
        print(f"Nb element {i}/{total}")
        url = repo.get("url")

        try:
            working_repo = Repo(repo.get("relative_path"))
        except git.exc.NoSuchPathError:
            # Check to fork
            # repo_info = git_tool.get_transformed_repo_info_from_url(
            #     url, organization_force="ERPLibre",
            #     is_submodule=repo.get("is_submodule"),
            #     sub_path=repo.get("sub_path"))
            git_tool.fork_repo(upstream_url=url,
                               github_token=github_token,
                               organization_name="ERPLibre")
            repo_info = git_tool.get_transformed_repo_info_from_url(
                url,
                organization_force=organization_name,
                is_submodule=repo.get("is_submodule"),
                sub_path=repo.get("sub_path"))
            git_tool.fork_repo(upstream_url=url,
                               github_token=github_token,
                               organization_name=organization_name)

            git_tool.add_and_fetch_remote(repo_info, root_repo=root_repo)
            continue

        dct_remote_name = {a.name: a for a in working_repo.remotes}
        remote_origin = dct_remote_name.get("origin")
        remote_erplibre = dct_remote_name.get("ERPLibre")
        remote_organization = dct_remote_name.get(organization_name)

        if not config.skip_fork:
            if not remote_erplibre:
                repo_info = git_tool.get_transformed_repo_info_from_url(
                    url,
                    organization_force="ERPLibre",
                    is_submodule=repo.get("is_submodule"),
                    sub_path=repo.get("sub_path"))
                git_tool.add_and_fetch_remote(repo_info)

            git_tool.fork_repo(
                url,
                github_token,
                organization_name=organization_name,
            )

        repo_info = git_tool.get_transformed_repo_info_from_url(
            url,
            organization_force=organization_name,
            is_submodule=repo.get("is_submodule"),
            sub_path=repo.get("sub_path"))
        if remote_origin:
            working_repo.git.remote("remove", "origin")
        repo_info.organization = "origin"
        try:
            git_tool.add_and_fetch_remote(repo_info, root_repo=root_repo)
        except Exception as e:
            print(e)
        if config.force and remote_organization:
            working_repo.git.remote("remove", organization_name)

        repo_info.organization = organization_name
        git_tool.add_and_fetch_remote(repo_info)

    # Update origin to new repo
    git_tool.generate_git_modules(lst_repo_organization, repo_path=config.dir)
    git_tool.generate_odoo_install_locally()
def main():
    config = get_config()
    git_tool = GitTool()

    dct_remote, dct_project, default_remote = git_tool.get_manifest_xml_info(
        filename=config.manifest, add_root=True)
    default_branch_name = default_remote.get("@revision",
                                             git_tool.default_branch)
    dct_result = defaultdict(int)
    i = 0
    total = len(dct_project)
    for name, project in dct_project.items():
        i += 1
        path = project.get("@path")
        print(f"{i}/{total} - {path}")
        branch_name = project.get("@revision", default_branch_name)
        organization = project.get("@remote", git_tool.default_project_name)

        try:
            git_repo = Repo(path)
        except NoSuchPathError:
            print(f"{Fore.YELLOW}Warning{Style.RESET_ALL} missing project"
                  f" {path}.")
            dct_result["WARNING"] += 1
            continue

        value = git_repo.git.branch("--show-current")
        if not value:
            # TODO maybe need to check divergence with local branch and not remote branch
            commit_head = git_repo.git.rev_parse("HEAD")
            try:
                commit_branch = git_repo.git.rev_parse(
                    f"{organization}/{branch_name}")
            except GitCommandError:
                # Cannot get information
                if branch_name == commit_head:
                    print(f"{Fore.GREEN}PASS{Style.RESET_ALL} Not on specified"
                          " branch, no divergence")
                    dct_result["PASS"] += 1
                else:
                    print(f"{Fore.RED}ERROR{Style.RESET_ALL} manifest revision"
                          f" is {branch_name} and commit {commit_head}.")
                    dct_result["ERROR"] += 1
                continue
            if commit_branch != commit_head:
                print(f"{Fore.YELLOW}WARNING{Style.RESET_ALL} Not on specified"
                      " branch, got a divergence.")
                dct_result["WARNING"] += 1
            else:
                print(f"{Fore.GREEN}PASS{Style.RESET_ALL} Not on specified"
                      " branch, no divergence")
                dct_result["PASS"] += 1
        elif branch_name != value:
            value_hash = git_repo.git.rev_parse(value)
            if git_repo.git.rev_parse(branch_name) == value_hash:
                print(f"{Fore.GREEN}PASS{Style.RESET_ALL} Not same branch, no"
                      " divergence")
                dct_result["PASS"] += 1
            else:
                # Check if the new branch is pushed
                commit_branch = git_repo.git.rev_parse(
                    f"{organization}/{value}")
                if commit_branch == value_hash:
                    print(f"{Fore.YELLOW}WARNING{Style.RESET_ALL} New branch"
                          f" '{value}', divergence, but it's push on remote.")
                    dct_result["WARNING"] += 1
                else:
                    print(f"{Fore.RED}ERROR{Style.RESET_ALL} manifest revision"
                          f" is {branch_name} and actual revision is {value}.")
                    dct_result["ERROR"] += 1
        else:
            print(f"{Fore.GREEN}PASS{Style.RESET_ALL}")
            dct_result["PASS"] += 1

    str_result = ""
    if dct_result["PASS"]:
        str_result += (
            f"{Fore.GREEN}PASS: {dct_result['PASS']}{Style.RESET_ALL}")
    if dct_result["WARNING"]:
        if str_result:
            str_result += " "
        str_result += (
            f"{Fore.YELLOW}WARNING: {dct_result['WARNING']}{Style.RESET_ALL}")
    if dct_result["ERROR"]:
        if str_result:
            str_result += " "
        str_result += (
            f"{Fore.RED}ERROR: {dct_result['ERROR']}{Style.RESET_ALL}")
    print(str_result)