Repository Apply

Batch apply: apply every manifest of a public GitHub repository from the web console’s Apply page.

A user gives the Apply page a repository’s url, e.g. https://github.com/smarter-sh/example-manifests. start_repository_apply() records the run, a RepositoryApply, and queues the Celery task smarter.apps.account.tasks.apply_repository(), which runs run_repository_apply(): it clones the repository, records each manifest that it finds, pending, and applies them one by one, recording each one’s result. The page polls the run, and shows each manifest’s state, and for a manifest that failed, why, what applying it printed, and its file on GitHub.

The manifests are found, ordered and applied by manage.py load_from_github’s own code: the RepositoryLoader here is its command, with only what a web user may do. Everything is applied as the user, in their account, through the same brokers and permission checks as the Apply page’s single manifest, and:

  • only a public repository on GitHub, by its https://github.com/<owner>/<repo> url, is cloned, with a time limit, and never with credentials;

  • the repository’s scripts/ never run: they run as the database server’s administrator;

  • a folder that the repository’s owners.yaml gives an owner is skipped: it is applied only by the platform, as its owner;

  • no LLMClient is deployed, and one that exists keeps its deployed value;

  • a manifest that names a resource that doesn’t exist, e.g. a Secret, fails, with the broker’s message.

A user may run one at a time, and REPOSITORY_APPLIES_PER_DAY a day.

smarter.apps.account.repository_apply.MAX_MANIFESTS = 300

The most manifests that a batch apply applies.

A repository with more fails, before applying any.

smarter.apps.account.repository_apply.REPOSITORY_APPLIES_PER_DAY = 20

The most batch applies that a user may start in 24 hours.

class smarter.apps.account.repository_apply.RepositoryApplyListResponse(**data)[source]

Bases: RepositoryApplyApiModel

The user’s recent batch applies, the newest first, without their manifests.

model_config = {'alias_generator': <function to_camel>, 'extra': 'forbid', 'serialize_by_alias': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

runs: list[RepositoryApplyResponse]
runs_per_day: int
class smarter.apps.account.repository_apply.RepositoryApplyManifestResponse(**data)[source]

Bases: RepositoryApplyApiModel

One manifest of a batch apply, and what became of it.

classmethod from_model(row, url, commit)[source]

Return a manifest’s response.

Parameters:
  • row (RepositoryApplyManifest) – The manifest.

  • url (str) – The repository’s url.

  • commit (str) – The commit that was cloned, or an empty string.

Return type:

RepositoryApplyManifestResponse

Returns:

The response.

kind: str
message: str
model_config = {'alias_generator': <function to_camel>, 'extra': 'forbid', 'serialize_by_alias': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: str
output: str
path: str
position: int
source_url: str
status: Literal['pending', 'applied', 'skipped', 'failed']
exception smarter.apps.account.repository_apply.RepositoryApplyRefused[source]

Bases: Exception

The user may not start a batch apply now, or the url isn’t a public GitHub repository’s.

class smarter.apps.account.repository_apply.RepositoryApplyResponse(**data)[source]

Bases: RepositoryApplyApiModel

A batch apply’s progress, as the Apply page reads it.

commit: str
completed_at: str | None
counts: dict[Literal['pending', 'applied', 'skipped', 'failed'], int]
created_at: str
error: str
classmethod from_model(run, with_manifests=True)[source]

Return a run’s progress.

Parameters:
  • run (RepositoryApply) – The run.

  • with_manifests (bool) – Include each manifest, or only the counts.

Return type:

RepositoryApplyResponse

Returns:

The response.

id: int
manifests: list[RepositoryApplyManifestResponse]
model_config = {'alias_generator': <function to_camel>, 'extra': 'forbid', 'serialize_by_alias': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

status: Literal['queued', 'cloning', 'applying', 'finished', 'failed']
url: str
class smarter.apps.account.repository_apply.RepositoryLoader(url, user_profile)[source]

Bases: Command

manage.py load_from_github, as a web user may run it: see the module’s docstring.

It finds, orders and applies the manifests with the command’s own methods, and records each one’s result in a RepositoryApplyManifest, rather than writing it to the console.

__init__(url, user_profile)[source]
applied: bool = False

apply() sets it.

Type:

Whether the last manifest was applied

apply(filespec, user_profile, manifest=None)[source]

Apply one manifest, with the command’s apply(), and remember whether it was.

Parameters:
  • filespec (str) – The manifest’s path.

  • user_profile (UserProfile) – The owner of the resource.

  • manifest (str | None) – The manifest to apply instead of the file’s.

Returns:

The error, or None if the manifest was applied.

Return type:

str | None

apply_one(item)[source]

Apply one manifest as the user, with the command’s apply_manifest, and say what became of it.

Parameters:

item (RepositoryManifest) – The manifest.

Returns:

Its status, why it failed or was skipped, and what applying it printed.

Return type:

tuple[str, str, str]

capture()[source]

Send what the command writes to a new buffer, and return the buffer.

Returns:

The buffer.

Return type:

StringIO

clone_repo(local_path)[source]

Clone the repository’s default branch, without its history, within CLONE_TIMEOUT_SECONDS.

git never asks for credentials: a private or missing repository fails at once.

Parameters:

local_path (str) – The folder to clone into.

Raises:

SmarterValueError – If git fails, or takes too long.

Return type:

None

skipped_for_owner(item)[source]

Return why a manifest of a folder that owners.yaml gives an owner is skipped, or None.

Parameters:

item (RepositoryManifest) – The manifest.

Returns:

The reason, or None if it isn’t in such a folder.

Return type:

str | None

smarter.apps.account.repository_apply.normalize_repository_url(url)[source]

Return a GitHub repository’s url, as it is cloned and recorded.

Parameters:

url (str) – The url that the user gave, e.g. https://github.com/smarter-sh/stackademy.git.

Returns:

e.g. https://github.com/smarter-sh/stackademy.

Return type:

str

Raises:

RepositoryApplyRefused – If it isn’t the url of a repository on GitHub.

smarter.apps.account.repository_apply.recent_runs(user_profile, limit=10)[source]

Return the user’s recent batch applies.

Parameters:
  • user_profile (UserProfile) – The user.

  • limit (int) – How many, at most.

Return type:

RepositoryApplyListResponse

Returns:

The runs, the newest first, without their manifests.

smarter.apps.account.repository_apply.run_repository_apply(run_id)[source]

Run a batch apply: clone, discover, then apply each manifest, recording each step in the rows.

A failure of the run as a whole, e.g. a repository that can’t be cloned, is recorded in the run, with what went wrong, rather than raised, so that the Celery task isn’t retried: the user can start another.

Parameters:

run_id (int) – The run’s id.

Return type:

RepositoryApply

Returns:

The run.

Raises:

RepositoryApply.DoesNotExist – If there is no such run.

smarter.apps.account.repository_apply.start_repository_apply(user_profile, url)[source]

Start a batch apply of a repository, unless one of the user’s is in progress.

The Celery task is queued once the run’s row is committed, so that the worker finds it.

Parameters:
  • user_profile (UserProfile) – The user, as whom the manifests are applied.

  • url (str) – The repository’s url.

Return type:

tuple[RepositoryApply, bool]

Returns:

The run, and True if it was started now, or False if one was already in progress.

Raises:

RepositoryApplyRefused – If the url isn’t a public GitHub repository’s, or the user started REPOSITORY_APPLIES_PER_DAY runs in the last 24 hours.