Verified Commit bf2767d9 authored by anarcat's avatar anarcat
Browse files

try to get rid of some hardcoded stuff, or at least identify it

parent 6bdd72c6
Loading
Loading
Loading
Loading
Loading
+29 −8
Original line number Diff line number Diff line
@@ -282,6 +282,7 @@ def write_pre_receive_hook(con: Connection, project: str, pre_receive_str: str):
    """write given pre-receive hook for project"""
    pre_receive_stream = StringIO(pre_receive_str)
    logging.debug("uploading %r to %s", pre_receive_str, con.host)
    # XXX: hardcoded path
    pre_receive_path = (
        f"/srv/git.torproject.org/repositories/{project}.git/hooks/pre-receive"
    )
@@ -313,6 +314,7 @@ def destroy_repo_scheduled(
):
    """schedule repository destruction"""
    gitolite_path = gitolite_path.strip("/").removesuffix(".git")
    # XXX: hardcoded path
    repository_path = f"/srv/git.torproject.org/repositories/{gitolite_path}.git"
    logging.info(
        "preparing destroying of Gitolite repository %s in %s",
@@ -325,6 +327,7 @@ def destroy_repo_scheduled(

    when = (datetime.now() + timedelta(days=delay)).isoformat()

    # XXX: hardcoded URL
    pre_receive_str = f"""#!/bin/sh

cat <<EOF
@@ -359,6 +362,7 @@ WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
    schedule_delete(con, repository_path, f"{delay} days")

    gitweb = Connection(gitweb_host)
    # XXX: hardcoded path
    gitweb_repository_path = (
        f"/srv/gitweb.torproject.org/repositories/{gitolite_path}.git"
    )
@@ -405,6 +409,7 @@ def migrate_repo(
    pre_receive_hook: bool = False,
):
    """migrate gitolite repo to gitlab project, con is gitolite server"""
    global CGIT_URL, GITLAB_URL
    gitolite_path = gitolite_path.strip("/").removesuffix(".git")
    gitlab_path = gitlab_path.strip("/").removesuffix(".git")
    gitlab_project = None
@@ -412,6 +417,7 @@ def migrate_repo(
        if name is None:
            name = gitlab_path.rsplit("/", maxsplit=1)[-1]
        logging.info("importing project into GitLab")
        # XXX: hardcoded URL
        import_url = "https://git.torproject.org/" + gitolite_path
        gitlab_project = create_project(
            con,
@@ -425,9 +431,9 @@ def migrate_repo(

    logging.info(
        "migrating Gitolite repository %s%s.git to GitLab project %s%s",
        "https://gitweb.torproject.org/",
        CGIT_URL,
        gitolite_path,
        "https://gitlab.torproject.org/",
        GITLAB_URL,
        gitlab_path,
    )

@@ -436,11 +442,11 @@ def migrate_repo(
cat <<EOF
This repository has been migrated to GitLab:

    https://gitlab.torproject.org/{gitlab_path}/
    {GITLAB_URL}{gitlab_path}/

You can change your Git remote with:

    git remote set-url origin https://gitlab.torproject.org/{gitlab_path}.git
    git remote set-url origin {GITLAB_URL}{gitlab_path}.git
"""
    if issue_url:
        pre_receive_str += f"""
@@ -546,6 +552,7 @@ class UserForkNotFoundError(Exception):

def pick_gitlab_project(gitolite_path: str, user_override: bool = True):
    """helper routine to guess a GitLab project matching the given gitolite repo"""
    global CGIT_URL
    name = gitolite_path.rsplit("/", maxsplit=1)[-1]
    gl_connector = GitLabConnector.singleton()
    gl_projects = {}
@@ -575,9 +582,10 @@ def pick_gitlab_project(gitolite_path: str, user_override: bool = True):
        return None

    logging.info(
        "found %d GitLab projects matching '%s' (https://gitweb.torproject.org/%s.git)",
        "found %d GitLab projects matching '%s' (%s%s.git)",
        len(gl_project_paths),
        name,
        CGIT_URL,
        gitolite_path,
    )
    p = pick(sorted(gl_project_paths))
@@ -656,7 +664,7 @@ def mass_migrate_user_repo(
    confirm: bool = True,
) -> bool:
    """special routine to migrate a user repository"""
    global COMMON_FORKS_MAP
    global COMMON_FORKS_MAP, CGIT_URL
    if not COMMON_FORKS_MAP:
        COMMON_FORKS_MAP = (
            dict(
@@ -667,6 +675,7 @@ def mass_migrate_user_repo(
            | STATIC_FORKS_MAP
        )

    # XXX: hardcoded URL
    issue_url = "https://gitlab.torproject.org/tpo/tpa/team/-/issues/41215"
    assert gitolite_path.startswith("user/")
    name = gitolite_path.rsplit("/", maxsplit=1)[-1]
@@ -708,6 +717,7 @@ def mass_migrate_user_repo(

    gitolite_repo_stats = is_empty_repo(
        con,
        # XXX: hardcoded path
        f"/srv/git.torproject.org/repositories/{gitolite_path}.git",
        sudo=True,
    )
@@ -784,7 +794,8 @@ def mass_migrate_user_repo(

    gitlab_path = fork.path_with_namespace
    if not getattr(con, "host", None):
        logging.info("building a new connect to cupani")
        logging.info("building a new connect to gitolite server")
        # XXX: hardcoded hostname
        con = Connection("cupani.torproject.org")

    logging.info("cloning and force pushing from %s to %s", gitolite_path, gitlab_path)
@@ -838,6 +849,7 @@ def mass_migrate_one_repo(
        description,
        category,
    )
    # XXX: hardcoded URL
    issue_url = "https://gitlab.torproject.org/tpo/tpa/team/-/issues/41215"
    legacy_prefix = "legacy/gitolite"
    gitlab_path = os.path.join(legacy_prefix, gitolite_path)
@@ -861,9 +873,11 @@ def mass_migrate_one_repo(
            import_project = False
        else:
            if not getattr(con, "host", None):
                # XXX: hardcoded hostname
                con = Connection("cupani.torproject.org")
            non_empty_stats = is_empty_repo(
                con,
                # XXX: hardcoded path
                f"/srv/git.torproject.org/repositories/{gitolite_path}.git",
                sudo=True,
            )
@@ -892,6 +906,7 @@ def mass_migrate_one_repo(
    ):
        logging.warning("skipping project %s as requested", gitolite_path)
        return False
    # XXX: hardcoded URL
    gitolite_url = f"https://git.torproject.org/{gitolite_path}"
    logging.info("checking if remote repo %s exists", gitolite_url)
    if (
@@ -929,6 +944,7 @@ def mass_migrate_one_repo(
    )
    if not getattr(con, "host", None):
        logging.info("building a new connect to cupani")
        # XXX: hardcoded hostname
        con = Connection("cupani.torproject.org")
    migrate_repo(
        con,
@@ -945,6 +961,7 @@ def mass_migrate_one_repo(
    return True


# XXX: hardcoded URLs
CGIT_URL = "https://gitweb.torproject.org/"
GITLAB_URL = "https://gitlab.torproject.org/"

@@ -1004,6 +1021,7 @@ def redirection_tests(
        )
        headers = {}

    # XXX: hardcoded URL
    base_gitlab_url = "https://git.torproject.org/" + project_git + ".git"
    logging.info("testing Gitolite remote %s", base_gitlab_url)
    env = os.environ | {"GIT_TERMINAL_PROMPT": "0"}
@@ -1132,6 +1150,7 @@ def check_empty_gitlab_repos(
            if getattr(con, "host", None):
                non_empty_stats = is_empty_repo(
                    con,
                    # XXX: hardcoded path
                    f"/srv/git.torproject.org/repositories/{gitolite_path}.git",
                    sudo=True,
                )
@@ -1164,6 +1183,7 @@ def local_import(
    force: bool = False,
):
    """clone a repo locally and push back into GitLab, hacking perms as we need to"""
    global GITLAB_URL
    gl_connector = GitLabConnector.singleton()

    if not gitlab_project:
@@ -1202,6 +1222,7 @@ def local_import(
        member = None
    # clone repository locally and push back up
    with tempfile.TemporaryDirectory() as tmpdir:
        # XXX: hardcoded hostname and path
        gitolite_remote = f"git-rw.torproject.org:/srv/git.torproject.org/repositories/{gitolite_path}.git"
        local_path = os.path.join(tmpdir, gitolite_path)
        timer = Timer()
@@ -1214,7 +1235,7 @@ def local_import(
        con.run(f"git clone --mirror {gitolite_remote} {local_path}")
        if timer.diff().total_seconds() > 10:
            logging.info("completed: %s", timer)
        gitlab_remote = f"https://gitlab.torproject.org/{gitlab_path}"
        gitlab_remote = f"{GITLAB_URL}{gitlab_path}"
        con.run(f"git -C {local_path} remote add gitlab {gitlab_remote}")
        timer = Timer()
        logging.info("pushing to GitLab %s at %s", gitlab_remote, timer.stamp)