Skip to content

Net

net

Functions:

Name Description
clean_url

Strip credentials from a URL and normalise the scheme to https.

encode_url

Re-encode a GitHub repository URL in a given scheme.

get_public_repo_url

Return the public HTTPS URL of a GitHub repository.

make_json_get

Perform an HTTP GET request and return the parsed JSON response.

parse_repository_url

Parse a GitHub repository URL and return its canonical form with owner and repo.

sparse_clone

Clone a remote repository with sparse checkout limited to specific paths.

clean_url

clean_url(url: str) -> str

Strip credentials from a URL and normalise the scheme to https.

Parameters:

Name Type Description Default

url

str

Raw URL string, possibly containing user:password@ credentials.

required

Returns:

Type Description
str

Cleaned URL using https with credentials removed.

Source code in oops/utils/net.py
def clean_url(url: str) -> str:
    """Strip credentials from a URL and normalise the scheme to https.

    Args:
        url: Raw URL string, possibly containing user:password@ credentials.

    Returns:
        Cleaned URL using https with credentials removed.
    """

    # Regex to match URLs with credentials
    pattern = re.compile(r"(https?://|http://)([^@]+@)?(.+)")

    # Substitute the matched pattern to remove credentials and ensure https
    cleaned_url = pattern.sub(lambda m: f"https://{m.group(3)}", url)  # noqa: E231

    return cleaned_url

encode_url

encode_url(url: str, scheme: str, suffix: bool = True) -> str

Re-encode a GitHub repository URL in a given scheme.

Parameters:

Name Type Description Default

url

str

Source repository URL in any supported format.

required

scheme

str

Target scheme, either "https" or "ssh".

required

suffix

bool

If True, append ".git" to HTTPS URLs. Defaults to True.

True

Returns:

Type Description
str

Re-encoded URL string in the requested scheme.

Raises:

Type Description
ValueError

If the scheme is not "https" or "ssh".

Source code in oops/utils/net.py
def encode_url(url: str, scheme: str, suffix: bool = True) -> str:
    """Re-encode a GitHub repository URL in a given scheme.

    Args:
        url: Source repository URL in any supported format.
        scheme: Target scheme, either "https" or "ssh".
        suffix: If True, append ".git" to HTTPS URLs. Defaults to True.

    Returns:
        Re-encoded URL string in the requested scheme.

    Raises:
        ValueError: If the scheme is not "https" or "ssh".
    """

    _, host, owner, repo = _parse_url(url)

    if scheme == "https":
        return f"https://{host}/{owner}/{repo}" + (".git" if suffix else "")
    elif scheme == "ssh":
        return f"git@{host}:{owner}/{repo}.git"
    else:
        raise ValueError(f"Unsupported scheme: {scheme}")

get_public_repo_url

get_public_repo_url(url: str) -> str

Return the public HTTPS URL of a GitHub repository.

Parameters:

Name Type Description Default

url

str

Repository URL in any supported format (HTTPS or SSH).

required

Returns:

Type Description
str

Canonical public HTTPS URL without a .git suffix.

Source code in oops/utils/net.py
def get_public_repo_url(url: str) -> str:
    """Return the public HTTPS URL of a GitHub repository.

    Args:
        url: Repository URL in any supported format (HTTPS or SSH).

    Returns:
        Canonical public HTTPS URL without a .git suffix.
    """
    return encode_url(url, "https", suffix=False)

make_json_get

make_json_get(url: str, headers: Optional[dict] = None, params: Optional[dict] = None) -> dict

Perform an HTTP GET request and return the parsed JSON response.

Parameters:

Name Type Description Default

url

str

URL to request.

required

headers

Optional[dict]

Optional HTTP headers to include.

None

params

Optional[dict]

Optional query parameters to include.

None

Returns:

Type Description
dict

Parsed JSON response as a dict.

Raises:

Type Description
HTTPError

If the response status indicates an error.

Source code in oops/utils/net.py
def make_json_get(url: str, headers: Optional[dict] = None, params: Optional[dict] = None) -> dict:
    """Perform an HTTP GET request and return the parsed JSON response.

    Args:
        url: URL to request.
        headers: Optional HTTP headers to include.
        params: Optional query parameters to include.

    Returns:
        Parsed JSON response as a dict.

    Raises:
        requests.HTTPError: If the response status indicates an error.
    """

    options = {}
    if headers:
        options["headers"] = headers
    if params:
        options["params"] = params

    r = requests.get(
        url,
        **options,
        timeout=config.default_timeout,
    )
    r.raise_for_status()

    return r.json()

parse_repository_url

parse_repository_url(url: str) -> Tuple[str, str, str]

Parse a GitHub repository URL and return its canonical form with owner and repo.

Supported formats: HTTPS (with or without .git, with or without branch path), SSH (git@github.com:owner/repo.git), and ssh:// URLs.

Parameters:

Name Type Description Default

url

str

GitHub repository URL to parse.

required

Returns:

Type Description
Tuple[str, str, str]

Tuple of (canonical_https_url, owner, repo). Owner is upper-cased for OCA.

Raises:

Type Description
ValueError

If the URL cannot be parsed or the host is not github.com.

Source code in oops/utils/net.py
def parse_repository_url(url: str) -> Tuple[str, str, str]:
    """Parse a GitHub repository URL and return its canonical form with owner and repo.

    Supported formats: HTTPS (with or without .git, with or without branch path),
    SSH (``git@github.com:owner/repo.git``), and ``ssh://`` URLs.

    Args:
        url: GitHub repository URL to parse.

    Returns:
        Tuple of (canonical_https_url, owner, repo). Owner is upper-cased for OCA.

    Raises:
        ValueError: If the URL cannot be parsed or the host is not github.com.
    """

    scheme, host, owner, repo = _parse_url(url)

    if host != "github.com":
        raise ValueError(f"Unsupported host: {host}")

    canonical_url = f"https://{host}/{owner}/{repo}"
    normalized_owner = owner.upper() if owner.lower() == "oca" else owner
    return canonical_url, normalized_owner, repo

sparse_clone

sparse_clone(remote_url: str, tmpdir: Path, files: list, branch: Optional[str] = None) -> None

Clone a remote repository with sparse checkout limited to specific paths.

Performs a shallow clone (depth=1) and enables sparse checkout so only the listed files or directories are materialised.

Parameters:

Name Type Description Default

remote_url

str

URL of the remote repository to clone.

required

tmpdir

Path

Local directory where the repository will be cloned.

required

files

list

List of file or directory patterns to include in the sparse checkout.

required

branch

Optional[str]

Branch to clone. If None, clones the remote default branch.

None
Source code in oops/utils/net.py
def sparse_clone(remote_url: str, tmpdir: Path, files: list, branch: Optional[str] = None) -> None:
    """Clone a remote repository with sparse checkout limited to specific paths.

    Performs a shallow clone (depth=1) and enables sparse checkout so only
    the listed files or directories are materialised.

    Args:
        remote_url: URL of the remote repository to clone.
        tmpdir: Local directory where the repository will be cloned.
        files: List of file or directory patterns to include in the sparse checkout.
        branch: Branch to clone. If None, clones the remote default branch.
    """
    kwargs = {"depth": 1, "no_checkout": True}
    if branch:
        kwargs["branch"] = branch
    remote_repo = Repo.clone_from(remote_url, str(tmpdir), **kwargs)

    # Enable sparse checkout
    with remote_repo.config_writer() as cw:
        cw.set_value("core", "sparseCheckout", True)

    # Write the list of patterns to .git/info/sparse-checkout
    sparse_file = tmpdir / ".git" / "info" / "sparse-checkout"
    sparse_file.write_text("\n".join(files) + "\n", encoding="utf-8")

    # Perform the actual checkout
    remote_repo.git.checkout("HEAD")