Skip to content

feu.github

The metadata of GitHub repositories, from the GitHub REST API. The functions that call the API need the requests extra; the GITHUB_TOKEN environment variable, if set, authenticates them. See the usage guide.

feu.github

Contain GitHub utility functions.

The functions read the metadata of repositories from the GitHub REST API (they need the feu[requests] extra), sort it and summarize it. The GITHUB_TOKEN environment variable, if set, authenticates the requests.

feu.github.build_github_headers

build_github_headers() -> dict[str, str]

Build the HTTP headers to call the GitHub REST API.

The GITHUB_TOKEN environment variable is used to authenticate the request if it is set.

Returns:

Type Description
dict[str, str]

The HTTP headers to use to call the GitHub API.

Example
>>> from feu.github import build_github_headers
>>> headers = build_github_headers()
>>> headers["Accept"]
'application/vnd.github+json'

feu.github.display_repos_summary

display_repos_summary(
    repos: Sequence[Mapping[str, Any]],
) -> None

Display repository information in a readable format.

The summary is logged at the INFO level with the logger of this module, so it is not visible unless logging is configured.

Parameters:

Name Type Description Default
repos Sequence[Mapping[str, Any]]

List of repository dictionaries from GitHub API, as returned by fetch_github_repos.

required
Example
>>> from feu.github import fetch_github_repos, display_repos_summary
>>> repos = fetch_github_repos(owner="durandtibo")  # doctest: +SKIP
>>> display_repos_summary(repos)  # doctest: +SKIP

feu.github.fetch_github_metadata cached

fetch_github_metadata(
    owner: str, repo: str
) -> Mapping[str, Any]

Get the GitHub repo metadata.

The metadata is read from the GitHub API. The request is authenticated if the GITHUB_TOKEN environment variable is set (see build_github_headers). The result is cached and read-only, so a caller cannot corrupt the cache.

Parameters:

Name Type Description Default
owner str

The owner of the repo.

required
repo str

The repo name.

required

Returns:

Type Description
Mapping[str, Any]

The repo metadata, as returned by the GitHub API, as a read-only mapping.

Raises:

Type Description
RuntimeError

If the request fails.

MissingPackageError

If requests is not installed.

Example
>>> from feu.github import fetch_github_metadata
>>> metadata = fetch_github_metadata(owner="durandtibo", repo="feu")  # doctest: +SKIP

feu.github.fetch_github_repos cached

fetch_github_repos(
    owner: str,
) -> tuple[Mapping[str, Any], ...]

Get the metadata of all the repositories of a GitHub user or organization.

The metadata is read from the GitHub API, following the pagination. The request is authenticated if the GITHUB_TOKEN environment variable is set (see build_github_headers). The result is cached and read-only: a tuple of read-only mappings.

Parameters:

Name Type Description Default
owner str

The owner of the repos i.e. the user or organization name.

required

Returns:

Type Description
tuple[Mapping[str, Any], ...]

The metadata of each repository, as returned by the GitHub API, as read-only mappings.

Raises:

Type Description
RuntimeError

If a request fails.

MissingPackageError

If requests is not installed.

Example
>>> from feu.github import fetch_github_repos
>>> repos = fetch_github_repos(owner="durandtibo")  # doctest: +SKIP

feu.github.sort_repos_by_key

sort_repos_by_key(
    repos: Sequence[Mapping[str, Any]],
    *,
    key: str,
    reverse: bool = False,
) -> list[Mapping[str, Any]]

Sort repositories by a given key.

Repositories without the key, or with a None value for the key, are placed at the end in their original order.

Parameters:

Name Type Description Default
repos Sequence[Mapping[str, Any]]

List of repository mappings from GitHub API.

required
key str

Key to sort.

required
reverse bool

If True, sort in descending order. Defaults to False.

False

Returns:

Type Description
list[Mapping[str, Any]]

A new list of repository mappings sorted in ascending order (or descending if reverse=True). Repositories without the key, or with a None value for the key, appear at the end in their original order, whatever the value of reverse.

Raises:

Type Description
TypeError

If the values of the key cannot be compared.

Example
>>> from feu.github import sort_repos_by_key
>>> repos = [{"name": "zoo"}, {"name": "alpha"}, {"id": 1}]
>>> sort_repos_by_key(repos, key="name")
[{'name': 'alpha'}, {'name': 'zoo'}, {'id': 1}]
>>> sort_repos_by_key(repos, key="name", reverse=True)
[{'name': 'zoo'}, {'name': 'alpha'}, {'id': 1}]

feu.github.auth

Contain utilities to authenticate against the GitHub API.

feu.github.auth.build_github_headers

build_github_headers() -> dict[str, str]

Build the HTTP headers to call the GitHub REST API.

The GITHUB_TOKEN environment variable is used to authenticate the request if it is set.

Returns:

Type Description
dict[str, str]

The HTTP headers to use to call the GitHub API.

Example
>>> from feu.github import build_github_headers
>>> headers = build_github_headers()
>>> headers["Accept"]
'application/vnd.github+json'

feu.github.metadata

Contain GitHub utility functions to get information about a single repository.

feu.github.metadata.fetch_github_metadata cached

fetch_github_metadata(
    owner: str, repo: str
) -> Mapping[str, Any]

Get the GitHub repo metadata.

The metadata is read from the GitHub API. The request is authenticated if the GITHUB_TOKEN environment variable is set (see build_github_headers). The result is cached and read-only, so a caller cannot corrupt the cache.

Parameters:

Name Type Description Default
owner str

The owner of the repo.

required
repo str

The repo name.

required

Returns:

Type Description
Mapping[str, Any]

The repo metadata, as returned by the GitHub API, as a read-only mapping.

Raises:

Type Description
RuntimeError

If the request fails.

MissingPackageError

If requests is not installed.

Example
>>> from feu.github import fetch_github_metadata
>>> metadata = fetch_github_metadata(owner="durandtibo", repo="feu")  # doctest: +SKIP

feu.github.repos

Contain GitHub utility functions to get information about repositories.

feu.github.repos.display_repos_summary

display_repos_summary(
    repos: Sequence[Mapping[str, Any]],
) -> None

Display repository information in a readable format.

The summary is logged at the INFO level with the logger of this module, so it is not visible unless logging is configured.

Parameters:

Name Type Description Default
repos Sequence[Mapping[str, Any]]

List of repository dictionaries from GitHub API, as returned by fetch_github_repos.

required
Example
>>> from feu.github import fetch_github_repos, display_repos_summary
>>> repos = fetch_github_repos(owner="durandtibo")  # doctest: +SKIP
>>> display_repos_summary(repos)  # doctest: +SKIP

feu.github.repos.fetch_github_repos cached

fetch_github_repos(
    owner: str,
) -> tuple[Mapping[str, Any], ...]

Get the metadata of all the repositories of a GitHub user or organization.

The metadata is read from the GitHub API, following the pagination. The request is authenticated if the GITHUB_TOKEN environment variable is set (see build_github_headers). The result is cached and read-only: a tuple of read-only mappings.

Parameters:

Name Type Description Default
owner str

The owner of the repos i.e. the user or organization name.

required

Returns:

Type Description
tuple[Mapping[str, Any], ...]

The metadata of each repository, as returned by the GitHub API, as read-only mappings.

Raises:

Type Description
RuntimeError

If a request fails.

MissingPackageError

If requests is not installed.

Example
>>> from feu.github import fetch_github_repos
>>> repos = fetch_github_repos(owner="durandtibo")  # doctest: +SKIP

feu.github.sorting

Contain GitHub utility functions to sort repositories.

feu.github.sorting.sort_repos_by_key

sort_repos_by_key(
    repos: Sequence[Mapping[str, Any]],
    *,
    key: str,
    reverse: bool = False,
) -> list[Mapping[str, Any]]

Sort repositories by a given key.

Repositories without the key, or with a None value for the key, are placed at the end in their original order.

Parameters:

Name Type Description Default
repos Sequence[Mapping[str, Any]]

List of repository mappings from GitHub API.

required
key str

Key to sort.

required
reverse bool

If True, sort in descending order. Defaults to False.

False

Returns:

Type Description
list[Mapping[str, Any]]

A new list of repository mappings sorted in ascending order (or descending if reverse=True). Repositories without the key, or with a None value for the key, appear at the end in their original order, whatever the value of reverse.

Raises:

Type Description
TypeError

If the values of the key cannot be compared.

Example
>>> from feu.github import sort_repos_by_key
>>> repos = [{"name": "zoo"}, {"name": "alpha"}, {"id": 1}]
>>> sort_repos_by_key(repos, key="name")
[{'name': 'alpha'}, {'name': 'zoo'}, {'id': 1}]
>>> sort_repos_by_key(repos, key="name", reverse=True)
[{'name': 'zoo'}, {'name': 'alpha'}, {'id': 1}]