split challenge.py into generic properties#214
Closed
vellvoid wants to merge 0 commit into
Closed
Conversation
Collaborator
Author
|
this builds on #213 to avoid conflicts |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors ctfcli.core.challenge by introducing a property registry (ctfcli.core.properties) where each challenge.yml attribute is managed by a dedicated Property class responsible for create/sync/pull/compare/mirror/verify behavior. This reduces the growth pressure on core/challenge.py and centralizes lifecycle logic per attribute.
Changes:
- Added a property framework (
Property,PropertyContext, registry, and multiple concrete properties) and updatedChallengeto orchestrate behavior via the registry. - Moved/rewired challenge linting into
ctfcli.core.lint.lint_challengeand updated tests to patch the new module locations. - Improved file upload handling in CLI media upload to ensure file handles are closed via context managers.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/core/test_challenge.py | Updates tests to use property APIs (get_property, PropertyContext) and patch new module locations. |
| tests/core/deployment/test_ssh_deployment.py | Updates mocks to patch Image in the new properties.image module. |
| tests/core/deployment/test_registry_deployment.py | Updates mocks to patch Image in the new properties.image module. |
| tests/core/deployment/test_cloud_deployment.py | Updates mocks to patch Image in the new properties.image module. |
| Makefile | Adjusts phony targets and makes lint errors fail the build (no longer ignored). |
| ctfcli/core/properties/base.py | Introduces Property/PropertyContext base abstractions and sentinel NOT_PULLED. |
| ctfcli/core/properties/init.py | Adds the ordered property registry and helpers (get_property, operation_order). |
| ctfcli/core/properties/scalars.py | Implements scalar-style properties (name/text/value/type/state/etc.), including scheduled_at parsing/normalization. |
| ctfcli/core/properties/collections.py | Implements collection-backed properties (flags/topics/tags/hints) with create/sync/pull behavior. |
| ctfcli/core/properties/files.py | Implements challenge file sync/mirror/verify logic as a property. |
| ctfcli/core/properties/image.py | Implements image resolution logic as a local-only property used by deployments. |
| ctfcli/core/properties/references.py | Implements reference properties (requirements/next/module) with normalization and matching. |
| ctfcli/core/properties/solution.py | Implements solution upsert logic including markdown image uploading and snippet include expansion. |
| ctfcli/core/lint.py | Adds a dedicated lint module and delegates Challenge.lint() to it. |
| ctfcli/core/challenge.py | Refactors Challenge to build payloads and run operations via the property registry. |
| ctfcli/core/api.py | Modernizes imports and super() usage; keeps multipart handling centralized. |
| ctfcli/cli/media.py | Fixes file upload to use a context manager for safe file-handle cleanup. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+52
to
+60
| required_challenge_ids = list(set(required_challenges)) | ||
|
|
||
| if ctx.challenge_id in required_challenge_ids: | ||
| click.secho( | ||
| "Challenge cannot require itself. Skipping invalid requirement.", | ||
| fg="yellow", | ||
| ) | ||
| required_challenges.remove(ctx.challenge_id) | ||
| required_challenges.sort() |
Comment on lines
+201
to
+215
| def create_all_files(self, ctx: PropertyContext) -> None: | ||
| new_files = [] | ||
| for challenge_file in ctx.challenge["files"]: | ||
| file_path = ctx.challenge_directory / challenge_file | ||
| new_files.append(("file", (file_path.name, file_path.open("rb")))) | ||
|
|
||
| files_payload = {"challenge_id": ctx.challenge_id, "type": "challenge"} | ||
|
|
||
| # Specifically use data= here to send multipart/form-data | ||
| r = ctx.api.post("/api/v1/files", files=new_files, data=files_payload) | ||
| r.raise_for_status() | ||
|
|
||
| # Close the file handles | ||
| for file_payload in new_files: | ||
| file_payload[1][1].close() |
Comment on lines
+123
to
+137
| for mdx, alt, path in markdown_images: | ||
| local_path = solution_path.parent / path | ||
| file_payload = { | ||
| "type": "solution", | ||
| "solution_id": solution_id, | ||
| } | ||
|
|
||
| with local_path.open(mode="rb") as file_handle: | ||
| # Specifically use data= here to send multipart/form-data | ||
| r = ctx.api.post("/api/v1/files", files={"file": (local_path.name, file_handle)}, data=file_payload) | ||
| r.raise_for_status() | ||
| resp = r.json() | ||
| server_location = resp["data"][0]["location"] | ||
|
|
||
| content = content.replace(mdx, f"") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
core/challenge.pyhas become the designated place for most of ctfcli's most-important functionality (managing challenges). Adding a new property to the challenge requires touching multiple places and further stuffing of that file - the architecture is just not scalable.This PR introduces a concept of properties - each challenge property like flags, topics, files, name etc. is handled within its own class which knows how to install, sync, compare, pull that specific property. The challenge class just calls hooks for all known properties.
Adding a new property boils down to creating a new class (or using a generic one for simple properties like just a string), and registering it with the properties registry.