FakeGitHub#

class graftkit.testing.FakeGitHub[source]#

Bases:

An in-memory Fetcher: publish releases, then let the skills commands read them.

Attributes:
tags

Each repository’s tags and the commits they point to.

commits

Each published commit’s files by path.

latest

Each repository’s latest release tag.

organisations

Each organisation’s repositories, as add_repository added them.

Methods

publish(repository, tag, files)

Publish a release (or move a tag) and return its fake commit.

add_repository(organisation, name, *[, ...])

Add a repository to an organisation; its head is the commit last published to it.

latest_release(repository)

Return the last tag published for a repository.

resolve(repository, tag)

Return the commit a tag points to.

files(repository, commit, folders)

Return the files under some folders at a commit.

organisation_repositories(organisation, *, files)

Return an organisation's repositories, by name, at the commit last published.

search_code(organisation, query, repository, ...)

Return the files of an organisation's repositories that contain a text.

publish(repository: str, tag: str, files: Mapping[str, bytes]) → str[source]#

Publish a release (or move a tag) and return its fake commit.

Parameters:
repositorystr

owner/name.

tagstr

The tag; publishing the same tag again moves it.

filesMapping[str, bytes]

The release’s files by path.

Returns:
str

The commit.

add_repository(organisation: str, name: str, *, description: str = '', topics: tuple[str, ...] = (), permission: Literal['ADMIN', 'MAINTAIN', 'WRITE', 'TRIAGE', 'READ'] | None = 'READ', archived: bool = False, branch: str = 'main') → None[source]#

Add a repository to an organisation; its head is the commit last published to it.

Its README and generated skill, as organisation_repositories returns them, are the README.md and .agents/skills/graftkit/SKILL.md files of that commit. Until something is published to it, it is empty: no default branch and no head.

Parameters:
organisationstr

The organisation’s login.

namestr

The repository’s name, without the organisation.

descriptionstr

GitHub’s description of it. Default: none.

topicstuple[str, …]

Its GitHub topics. Default: none.

permissionLiteral[‘ADMIN’, ‘MAINTAIN’, ‘WRITE’, ‘TRIAGE’, ‘READ’] | None

The caller’s access to it, None when GitHub does not say. Default: READ.

archivedbool

Whether it is archived. Default: no.

branchstr

Its default branch, once something is published. Default: main.

latest_release(repository: str) → str[source]#

Return the last tag published for a repository.

Parameters:
repositorystr

owner/name.

Returns:
str

The tag.

Raises:
CliError

If nothing was published for the repository.

resolve(repository: str, tag: str) → str[source]#

Return the commit a tag points to.

Parameters:
repositorystr

owner/name.

tagstr

The tag.

Returns:
str

The commit.

Raises:
CliError

If the tag was never published, as gh reports an unknown tag.

files(repository: str, commit: str, folders: Iterable[str]) → dict[str, bytes][source]#

Return the files under some folders at a commit.

Parameters:
repositorystr

owner/name.

commitstr

A commit publish returned.

foldersIterable[str]

The folders to return; "" means the root files only.

Returns:
dict[str, bytes]

The files by path from the repository root.

Raises:
CliError

If the commit was never published, as gh reports an unknown commit.

organisation_repositories(organisation: str, *, files: bool) → tuple[RepositorySnapshot, ...][source]#

Return an organisation’s repositories, by name, at the commit last published.

Parameters:
organisationstr

The organisation’s login.

filesbool

Whether to read each one’s README and generated skill; None for both otherwise.

Returns:
tuple[RepositorySnapshot, …]

Its repositories, sorted by name.

Raises:
CliError

If no repository was added to it, as gh reports an unknown organisation.

search_code(organisation: str, query: str, repository: str | None, limit: int) → tuple[CodeMatch, ...][source]#

Return the files of an organisation’s repositories that contain a text.

Searches the files last published to each repository added with add_repository, ignoring case; each line holding the text is a fragment.

Parameters:
organisationstr

The organisation’s login.

querystr

The text to find.

repositorystr | None

Only this repository, by name; None for all of them.

limitint

The most files to return.

Returns:
tuple[CodeMatch, …]

The matching files, by repository and path.