Skip to content

GitHub

feu.github

Contain GitHub utility functions.

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()

feu.github.display_repos_summary

display_repos_summary(
    repos: Sequence[dict[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[dict[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
) -> dict[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.

Parameters:

Name Type Description Default
owner str

The owner of the repo.

required
repo str

The repo name.

required

Returns:

Type Description
dict[str, Any]

The repo metadata, as returned by the GitHub API.

Raises:

Type Description
RuntimeError

If the request fails or 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[dict[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.

Parameters:

Name Type Description Default
owner str

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

required

Returns:

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

The metadata of each repository, as returned by the GitHub API.

Raises:

Type Description
RuntimeError

If a request fails or 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[dict[str, Any]],
    *,
    key: str,
    reverse: bool = False,
) -> list[dict[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[dict[str, Any]]

List of repository dictionaries 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[dict[str, Any]]

A new list of repository dictionaries 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}]