Skip to content

github

napt.github

GitHub API access shared by discovery and the tool downloads.

Three parts of NAPT read GitHub: the api_github discovery strategy, the PSADT toolkit download in napt build, and the IntuneWinAppUtil download in napt package. All of them go through this module, so a request carries the same headers, a failure reads the same, and a tag is read the same way wherever it came from.

Tokens

Discovery sends the token the recipe names, which defaults/org.yaml binds to api.github.com (see napt.secrets). The tool downloads send GITHUB_TOKEN from the environment when it is set, since every unauthenticated call counts against one shared limit of 60 per hour. A token is only ever sent to api.github.com: with a token present a redirect to any other host is refused.

env_token

env_token() -> str | None

Returns GITHUB_TOKEN from the environment, or None when unset.

Source code in napt/github.py
def env_token() -> str | None:
    """Returns ``GITHUB_TOKEN`` from the environment, or None when unset."""
    return os.environ.get("GITHUB_TOKEN") or None

api_headers

api_headers(token: str | None) -> dict[str, str]

Builds the headers for a GitHub API call.

Parameters:

Name Type Description Default
token str | None

A personal access or workflow token, or None for an unauthenticated call.

required

Returns:

Type Description
dict[str, str]

The Accept and API-version headers, plus Authorization when a

dict[str, str]

token is given.

Source code in napt/github.py
def api_headers(token: str | None) -> dict[str, str]:
    """Builds the headers for a GitHub API call.

    Args:
        token: A personal access or workflow token, or None for an
            unauthenticated call.

    Returns:
        The Accept and API-version headers, plus Authorization when a
        token is given.
    """
    headers = {
        "Accept": "application/vnd.github+json",
        "X-GitHub-Api-Version": "2022-11-28",
    }
    if token:
        headers["Authorization"] = f"Bearer {token}"
    return headers

get_json

get_json(
    session: Session,
    url: str,
    *,
    what: str,
    token: str | None = None,
    hosts: set[str] | None = None
) -> dict[str, Any] | None

Fetches a GitHub API resource and reads it as a JSON object.

Every failure is a NetworkError that names what was asked for. An exhausted rate limit is named as such, from the response's own rate-limit header rather than the 403 status alone, since GitHub also answers 403 for a refused token.

Parameters:

Name Type Description Default
session Session

The session to send with.

required
url str

The API URL.

required
what str

What is being fetched, for messages ("the latest PSADT release").

required
token str | None

The token to send, or None.

None
hosts set[str] | None

The hosts a redirect may go to, from bound_hosts; with a token and no hosts, only api.github.com.

None

Returns:

Type Description
dict[str, Any] | None

The parsed object, or None when the resource does not exist (404).

Raises:

Type Description
NetworkError

On transport failure, a refused or failed request, or a body that is not a JSON object.

Source code in napt/github.py
def get_json(
    session: requests.Session,
    url: str,
    *,
    what: str,
    token: str | None = None,
    hosts: set[str] | None = None,
) -> dict[str, Any] | None:
    """Fetches a GitHub API resource and reads it as a JSON object.

    Every failure is a NetworkError that names what was asked for. An
    exhausted rate limit is named as such, from the response's own
    rate-limit header rather than the 403 status alone, since GitHub also
    answers 403 for a refused token.

    Args:
        session: The session to send with.
        url: The API URL.
        what: What is being fetched, for messages (``"the latest PSADT
            release"``).
        token: The token to send, or None.
        hosts: The hosts a redirect may go to, from
            [bound_hosts][napt.secrets.bound_hosts]; with a token and no
            hosts, only ``api.github.com``.

    Returns:
        The parsed object, or None when the resource does not exist (404).

    Raises:
        NetworkError: On transport failure, a refused or failed request,
            or a body that is not a JSON object.
    """
    if token and hosts is None:
        hosts = {API_HOST}
    try:
        response = guarded_get(
            session, url, api_headers(token), hosts=hosts, timeout=_API_TIMEOUT
        )
    except requests.RequestException as err:
        raise NetworkError(f"Failed to fetch {what}: {err}") from err

    if response.status_code == 404:
        return None
    if response.status_code == 403:
        if response.headers.get("X-RateLimit-Remaining") == "0":
            raise NetworkError(
                f"GitHub API rate limit exceeded while fetching {what}. "
                "Authenticate with a token to raise the limit."
            )
        raise NetworkError(
            f"GitHub refused the request for {what}: 403 {response.reason}"
        )
    if not response.ok:
        raise NetworkError(
            f"GitHub API request for {what} failed: {response.status_code} "
            f"{response.reason}"
        )

    try:
        data = response.json()
    except ValueError as err:
        raise NetworkError(
            f"Invalid JSON response from the GitHub API for {what}. Response: "
            f"{response.text[:200]}"
        ) from err
    if not isinstance(data, dict):
        raise NetworkError(
            f"Unexpected GitHub API response for {what}: {response.text[:200]}"
        )
    return data

latest_release

latest_release(
    session: Session,
    repo: str,
    *,
    token: str | None = None,
    hosts: set[str] | None = None
) -> dict[str, Any]

Fetches a repository's latest release.

GitHub's latest release is the most recent one that is neither a draft nor a pre-release.

Parameters:

Name Type Description Default
session Session

The session to send with.

required
repo str

The repository as owner/name.

required
token str | None

The token to send, or None.

None
hosts set[str] | None

The hosts a redirect may go to, or None.

None

Returns:

Type Description
dict[str, Any]

The release object, with its tag_name and assets.

Raises:

Type Description
NetworkError

If the repository does not exist or has no releases, or on any failure get_json reports.

Source code in napt/github.py
def latest_release(
    session: requests.Session,
    repo: str,
    *,
    token: str | None = None,
    hosts: set[str] | None = None,
) -> dict[str, Any]:
    """Fetches a repository's latest release.

    GitHub's latest release is the most recent one that is neither a
    draft nor a pre-release.

    Args:
        session: The session to send with.
        repo: The repository as ``owner/name``.
        token: The token to send, or None.
        hosts: The hosts a redirect may go to, or None.

    Returns:
        The release object, with its ``tag_name`` and ``assets``.

    Raises:
        NetworkError: If the repository does not exist or has no releases,
            or on any failure [get_json][napt.github.get_json] reports.
    """
    logger = get_global_logger()
    url = f"{API_BASE}/repos/{repo}/releases/latest"
    logger.verbose("HTTP", f"Fetching release from: {url}")
    release = get_json(
        session, url, what=f"the latest release of {repo}", token=token, hosts=hosts
    )
    if release is None:
        raise NetworkError(f"Repository {repo!r} not found or has no releases")
    return release

release_by_version

release_by_version(
    session: Session, repo: str, version: str, *, token: str | None = None
) -> tuple[str, dict[str, Any]]

Fetches the release of a version, however the repository tags it.

Repositories differ on whether tags carry a v prefix, and some switched part-way, so both spellings are tried.

Parameters:

Name Type Description Default
session Session

The session to send with.

required
repo str

The repository as owner/name.

required
version str

The version without a prefix ("4.1.7").

required
token str | None

The token to send, or None.

None

Returns:

Type Description
tuple[str, dict[str, Any]]

The tag as the repository spells it, and the release object.

Raises:

Type Description
NetworkError

If no release is tagged with either spelling, or on any failure get_json reports.

Source code in napt/github.py
def release_by_version(
    session: requests.Session,
    repo: str,
    version: str,
    *,
    token: str | None = None,
) -> tuple[str, dict[str, Any]]:
    """Fetches the release of a version, however the repository tags it.

    Repositories differ on whether tags carry a ``v`` prefix, and some
    switched part-way, so both spellings are tried.

    Args:
        session: The session to send with.
        repo: The repository as ``owner/name``.
        version: The version without a prefix (``"4.1.7"``).
        token: The token to send, or None.

    Returns:
        The tag as the repository spells it, and the release object.

    Raises:
        NetworkError: If no release is tagged with either spelling, or on
            any failure [get_json][napt.github.get_json] reports.
    """
    logger = get_global_logger()
    for tag in (version, f"v{version}"):
        url = f"{API_BASE}/repos/{repo}/releases/tags/{quote(tag)}"
        logger.verbose("HTTP", f"Fetching release from: {url}")
        release = get_json(session, url, what=f"release {tag} of {repo}", token=token)
        if release is not None:
            return tag, release
    raise NetworkError(f"No release of {repo} is tagged {version} or v{version}")

repo_file

repo_file(
    session: Session,
    repo: str,
    path: str,
    ref: str,
    *,
    token: str | None = None
) -> dict[str, Any] | None

Looks a file up through the contents API at a tag or branch.

The entry reports the file's size, its git blob hash, and a download URL, which is how a file that is committed to the repository rather than attached to a release is fetched and verified.

Parameters:

Name Type Description Default
session Session

The session to send with.

required
repo str

The repository as owner/name.

required
path str

The file's path in the repository.

required
ref str

The tag or branch to read it at.

required
token str | None

The token to send, or None.

None

Returns:

Type Description
dict[str, Any] | None

The contents entry, or None when the file or the ref does not exist.

Raises:

Type Description
NetworkError

On any failure get_json reports.

Source code in napt/github.py
def repo_file(
    session: requests.Session,
    repo: str,
    path: str,
    ref: str,
    *,
    token: str | None = None,
) -> dict[str, Any] | None:
    """Looks a file up through the contents API at a tag or branch.

    The entry reports the file's size, its git blob hash, and a download
    URL, which is how a file that is committed to the repository rather
    than attached to a release is fetched and verified.

    Args:
        session: The session to send with.
        repo: The repository as ``owner/name``.
        path: The file's path in the repository.
        ref: The tag or branch to read it at.
        token: The token to send, or None.

    Returns:
        The contents entry, or None when the file or the ref does not exist.

    Raises:
        NetworkError: On any failure [get_json][napt.github.get_json]
            reports.
    """
    url = f"{API_BASE}/repos/{repo}/contents/{path}?{urlencode({'ref': ref})}"
    return get_json(session, url, what=f"{path} at {ref} in {repo}", token=token)

release_tag

release_tag(release: dict[str, Any]) -> str

Reads a release's tag.

Parameters:

Name Type Description Default
release dict[str, Any]

The release object.

required

Returns:

Type Description
str

The tag name.

Raises:

Type Description
NetworkError

If the release carries no tag.

Source code in napt/github.py
def release_tag(release: dict[str, Any]) -> str:
    """Reads a release's tag.

    Args:
        release: The release object.

    Returns:
        The tag name.

    Raises:
        NetworkError: If the release carries no tag.
    """
    tag = release.get("tag_name")
    if not isinstance(tag, str) or not tag:
        raise NetworkError("GitHub release response is missing 'tag_name'")
    return tag

version_from_tag

version_from_tag(tag: str) -> str

Reads the version a tag names.

Parameters:

Name Type Description Default
tag str

The tag ("4.1.7" or "v4.1.7").

required

Returns:

Type Description
str

The version without the prefix.

Raises:

Type Description
NetworkError

If the whole tag is not a version, so a suffix such as -rc1 is never silently dropped.

Source code in napt/github.py
def version_from_tag(tag: str) -> str:
    """Reads the version a tag names.

    Args:
        tag: The tag (``"4.1.7"`` or ``"v4.1.7"``).

    Returns:
        The version without the prefix.

    Raises:
        NetworkError: If the whole tag is not a version, so a suffix such
            as ``-rc1`` is never silently dropped.
    """
    match = _VERSION_TAG.fullmatch(tag)
    if not match:
        raise NetworkError(
            f"Could not extract version from tag {tag!r}: expected a version "
            "such as 1.2.3, with or without a v prefix"
        )
    return match.group(1)

normalize_release_spec

normalize_release_spec(spec: str, setting: str, example: str) -> str

Turns a pinned release setting into the version it names.

Parameters:

Name Type Description Default
spec str

The configured value, a version with or without a v prefix.

required
setting str

The setting's path, for the message ("psadt.release").

required
example str

A valid value, for the message.

required

Returns:

Type Description
str

The version, usable as a cache folder name.

Raises:

Type Description
ConfigError

If the value is not a plain version.

Source code in napt/github.py
def normalize_release_spec(spec: str, setting: str, example: str) -> str:
    """Turns a pinned release setting into the version it names.

    Args:
        spec: The configured value, a version with or without a ``v``
            prefix.
        setting: The setting's path, for the message (``"psadt.release"``).
        example: A valid value, for the message.

    Returns:
        The version, usable as a cache folder name.

    Raises:
        ConfigError: If the value is not a plain version.
    """
    version = spec.removeprefix("v")
    if not version or not is_safe_path_component(version):
        raise ConfigError(
            f"Invalid {setting} {spec!a}: use 'latest' or a version such as "
            f"{example!r}"
        )
    return version

find_asset

find_asset(
    release: dict[str, Any], predicate: Callable[[str], bool]
) -> dict[str, Any] | None

Finds the first release asset whose name satisfies a predicate.

Parameters:

Name Type Description Default
release dict[str, Any]

The release object.

required
predicate Callable[[str], bool]

Called with each asset's name.

required

Returns:

Type Description
dict[str, Any] | None

The asset entry, or None when none matches.

Source code in napt/github.py
def find_asset(
    release: dict[str, Any], predicate: Callable[[str], bool]
) -> dict[str, Any] | None:
    """Finds the first release asset whose name satisfies a predicate.

    Args:
        release: The release object.
        predicate: Called with each asset's name.

    Returns:
        The asset entry, or None when none matches.
    """
    for asset in release.get("assets") or []:
        if isinstance(asset, dict) and predicate(str(asset.get("name", ""))):
            return asset
    return None

asset_names

asset_names(release: dict[str, Any]) -> list[str]

Lists a release's asset names, for messages.

Source code in napt/github.py
def asset_names(release: dict[str, Any]) -> list[str]:
    """Lists a release's asset names, for messages."""
    return [str(a.get("name", "(unnamed)")) for a in release.get("assets") or []]

asset_url

asset_url(asset: dict[str, Any]) -> str

Reads an asset's download URL.

Parameters:

Name Type Description Default
asset dict[str, Any]

The asset entry.

required

Returns:

Type Description
str

The browser download URL.

Raises:

Type Description
NetworkError

If the entry carries none.

Source code in napt/github.py
def asset_url(asset: dict[str, Any]) -> str:
    """Reads an asset's download URL.

    Args:
        asset: The asset entry.

    Returns:
        The browser download URL.

    Raises:
        NetworkError: If the entry carries none.
    """
    url = asset.get("browser_download_url")
    if not isinstance(url, str) or not url:
        raise NetworkError(f"Asset {asset.get('name')} has no download URL")
    return url

download

download(
    session: Session, url: str, *, what: str, timeout: int = ASSET_TIMEOUT
) -> bytes

Downloads a file and returns its bytes.

No token is sent: the download hosts are not the API, and the bytes are checked by the caller against what the API reported for them.

Parameters:

Name Type Description Default
session Session

The session to send with.

required
url str

The download URL.

required
what str

What is being downloaded, for messages.

required
timeout int

Seconds allowed for the download.

ASSET_TIMEOUT

Returns:

Type Description
bytes

The downloaded bytes.

Raises:

Type Description
NetworkError

On transport failure or a failed request.

Source code in napt/github.py
def download(
    session: requests.Session,
    url: str,
    *,
    what: str,
    timeout: int = ASSET_TIMEOUT,
) -> bytes:
    """Downloads a file and returns its bytes.

    No token is sent: the download hosts are not the API, and the bytes
    are checked by the caller against what the API reported for them.

    Args:
        session: The session to send with.
        url: The download URL.
        what: What is being downloaded, for messages.
        timeout: Seconds allowed for the download.

    Returns:
        The downloaded bytes.

    Raises:
        NetworkError: On transport failure or a failed request.
    """
    try:
        response = session.get(url, timeout=timeout)
        response.raise_for_status()
    except requests.RequestException as err:
        raise NetworkError(f"Failed to download {what}: {err}") from err
    return response.content

verify_size

verify_size(what: str, content: bytes, size: object) -> None

Checks downloaded bytes against the size the API reported.

Parameters:

Name Type Description Default
what str

What was downloaded, for the message.

required
content bytes

The downloaded bytes.

required
size object

The reported size; anything but an int is ignored.

required

Raises:

Type Description
NetworkError

If the sizes differ.

Source code in napt/github.py
def verify_size(what: str, content: bytes, size: object) -> None:
    """Checks downloaded bytes against the size the API reported.

    Args:
        what: What was downloaded, for the message.
        content: The downloaded bytes.
        size: The reported size; anything but an int is ignored.

    Raises:
        NetworkError: If the sizes differ.
    """
    if isinstance(size, int) and len(content) != size:
        raise NetworkError(
            f"Downloaded {what} is {len(content)} bytes; GitHub reports {size} "
            "bytes. The download was cut short; try again."
        )