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
api_headers
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
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 ( |
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 |
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
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 |
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 |
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
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 |
required |
version
|
str
|
The version without a prefix ( |
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
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 |
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
release_tag
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
version_from_tag
Reads the version a tag names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
str
|
The tag ( |
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 |
Source code in napt/github.py
normalize_release_spec
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 |
required |
setting
|
str
|
The setting's path, for the message ( |
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
find_asset
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
asset_names
asset_url
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
download
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
verify_size
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. |