diff --git a/requirements.txt b/requirements.txt index 3df0cff..77f32a3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ ghapi jinja2 kombu==5.4.0 openstacksdk>=3,<4 -pydantic < 2 +pydantic==2.8.2 diff --git a/src-docs/metrics.events.md b/src-docs/metrics.events.md index 055705c..9c8cd12 100644 --- a/src-docs/metrics.events.md +++ b/src-docs/metrics.events.md @@ -65,6 +65,28 @@ Initialize the event. - `kwargs`: The keyword arguments to pass to the base class. These are used to set the specific fields. E.g. timestamp=12345 will set the timestamp field to 12345. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -100,6 +122,28 @@ Initialize the event. - `kwargs`: The keyword arguments to pass to the base class. These are used to set the specific fields. E.g. timestamp=12345 will set the timestamp field to 12345. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -139,6 +183,28 @@ Initialize the event. - `kwargs`: The keyword arguments to pass to the base class. These are used to set the specific fields. E.g. timestamp=12345 will set the timestamp field to 12345. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -158,6 +224,28 @@ This could e.g. be an exit code or a http status code. - `code`: The status code. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -199,6 +287,28 @@ Initialize the event. - `kwargs`: The keyword arguments to pass to the base class. These are used to set the specific fields. E.g. timestamp=12345 will set the timestamp field to 12345. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -236,6 +346,28 @@ Initialize the event. - `kwargs`: The keyword arguments to pass to the base class. These are used to set the specific fields. E.g. timestamp=12345 will set the timestamp field to 12345. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + diff --git a/src-docs/metrics.runner.md b/src-docs/metrics.runner.md index 0b8b5af..d4af292 100644 --- a/src-docs/metrics.runner.md +++ b/src-docs/metrics.runner.md @@ -96,6 +96,28 @@ Metrics for the pre-job phase of a runner. - `event`: The github event. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -132,6 +154,28 @@ Information about a status code. - `code`: The status code. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -151,6 +195,28 @@ Metrics for the post-job phase of a runner. - `status_info`: More information about the status. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -171,6 +237,28 @@ Metrics for a runner. - `runner_name`: The name of the runner. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + diff --git a/src-docs/reactive.consumer.md b/src-docs/reactive.consumer.md index 99ce9ed..34b37f9 100644 --- a/src-docs/reactive.consumer.md +++ b/src-docs/reactive.consumer.md @@ -70,6 +70,28 @@ A class to translate the payload. - `run_url`: The URL of the job. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + diff --git a/src-docs/types_.github.md b/src-docs/types_.github.md index d410455..4abe6d9 100644 --- a/src-docs/types_.github.md +++ b/src-docs/types_.github.md @@ -214,6 +214,28 @@ Stats for a job on GitHub. - `conclusion`: The end result of a job. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + diff --git a/src-docs/types_.md b/src-docs/types_.md index b423cd2..a183143 100644 --- a/src-docs/types_.md +++ b/src-docs/types_.md @@ -21,6 +21,28 @@ Represents the configuration for reactive scheduling. - `mq_uri`: The URI of the MQ to use to spawn runners reactively. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + @@ -48,29 +70,44 @@ Proxy configuration. Return the aproxy address. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + --- -### classmethod `check_use_aproxy` +### method `check_use_aproxy` ```python -check_use_aproxy(use_aproxy: bool, values: dict) → bool +check_use_aproxy(self: 'ProxyConfig') → ProxyConfig ``` Validate the proxy configuration. -**Args:** - - - `use_aproxy`: Value of use_aproxy variable. - - `values`: Values in the pydantic model. - - - **Raises:** - `ValueError`: if use_aproxy was set but no http/https was passed. @@ -78,12 +115,12 @@ Validate the proxy configuration. **Returns:** - Validated use_aproxy value. + Validated ProxyConfig instance. --- - + ## class `SSHDebugConnection` SSH connection information for debug workflow. @@ -98,12 +135,34 @@ SSH connection information for debug workflow. - `ed25519_fingerprint`: The host SSH server public ed25519 key fingerprint. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + --- - + ## class `RepoPolicyComplianceConfig` Configuration for the repo policy compliance service. @@ -116,6 +175,28 @@ Configuration for the repo policy compliance service. - `url`: URL of the repo policy compliance service. +--- + +#### property model_extra + +Get extra fields set during validation. + + + +**Returns:** + A dictionary of extra fields, or `None` if `config.extra` is not set to `"allow"`. + +--- + +#### property model_fields_set + +Returns the set of fields that have been explicitly set on this model instance. + + + +**Returns:** + A set of strings representing the fields that have been set, i.e. that were not filled from defaults. + diff --git a/src/github_runner_manager/metrics/events.py b/src/github_runner_manager/metrics/events.py index b73815e..916e700 100644 --- a/src/github_runner_manager/metrics/events.py +++ b/src/github_runner_manager/metrics/events.py @@ -92,7 +92,7 @@ class RunnerStart(Event): repo: str github_event: str idle: NonNegativeFloat - queue_duration: Optional[NonNegativeFloat] + queue_duration: Optional[NonNegativeFloat] = None class CodeInformation(BaseModel): @@ -127,9 +127,9 @@ class RunnerStop(Event): repo: str github_event: str status: str - status_info: Optional[CodeInformation] + status_info: Optional[CodeInformation] = None job_duration: NonNegativeFloat - job_conclusion: Optional[str] + job_conclusion: Optional[str] = None class Reconciliation(Event): diff --git a/src/github_runner_manager/metrics/runner.py b/src/github_runner_manager/metrics/runner.py index 029cd6c..d43ca66 100644 --- a/src/github_runner_manager/metrics/runner.py +++ b/src/github_runner_manager/metrics/runner.py @@ -46,7 +46,7 @@ class PreJobMetrics(BaseModel): timestamp: NonNegativeFloat workflow: str workflow_run_id: str - repository: str = Field(None, regex=r"^.+/.+$") + repository: str = Field(None, pattern=r"^.+/.+$") event: str @@ -85,7 +85,7 @@ class PostJobMetrics(BaseModel): timestamp: NonNegativeFloat status: PostJobStatus - status_info: Optional[CodeInformation] + status_info: Optional[CodeInformation] = None class RunnerMetrics(BaseModel): @@ -100,7 +100,7 @@ class RunnerMetrics(BaseModel): installed_timestamp: NonNegativeFloat pre_job: PreJobMetrics - post_job: Optional[PostJobMetrics] + post_job: Optional[PostJobMetrics] = None runner_name: str diff --git a/src/github_runner_manager/types_/__init__.py b/src/github_runner_manager/types_/__init__.py index cd712da..7ab4615 100644 --- a/src/github_runner_manager/types_/__init__.py +++ b/src/github_runner_manager/types_/__init__.py @@ -4,7 +4,7 @@ """Package containing modules with type definitions.""" from typing import Optional -from pydantic import AnyHttpUrl, BaseModel, Field, IPvAnyAddress, MongoDsn, validator +from pydantic import AnyHttpUrl, BaseModel, Field, IPvAnyAddress, MongoDsn, model_validator class ReactiveConfig(BaseModel): @@ -28,9 +28,9 @@ class ProxyConfig(BaseModel): use_aproxy: Whether aproxy should be used for the runners. """ - http: Optional[AnyHttpUrl] - https: Optional[AnyHttpUrl] - no_proxy: Optional[str] + http: Optional[AnyHttpUrl] = None + https: Optional[AnyHttpUrl] = None + no_proxy: Optional[str] = None use_aproxy: bool = False @property @@ -51,25 +51,20 @@ def aproxy_address(self) -> Optional[str]: aproxy_address = None return aproxy_address - @validator("use_aproxy") - @classmethod - def check_use_aproxy(cls, use_aproxy: bool, values: dict) -> bool: + @model_validator(mode="after") + def check_use_aproxy(self: "ProxyConfig") -> "ProxyConfig": """Validate the proxy configuration. - Args: - use_aproxy: Value of use_aproxy variable. - values: Values in the pydantic model. - Raises: ValueError: if use_aproxy was set but no http/https was passed. Returns: - Validated use_aproxy value. + Validated ProxyConfig instance. """ - if use_aproxy and not (values.get("http") or values.get("https")): + if self.use_aproxy and not (self.http or self.https): raise ValueError("aproxy requires http or https to be set") - return use_aproxy + return self def __bool__(self) -> bool: """Return whether the proxy config is set. diff --git a/src/github_runner_manager/types_/github.py b/src/github_runner_manager/types_/github.py index 89196a7..760598f 100644 --- a/src/github_runner_manager/types_/github.py +++ b/src/github_runner_manager/types_/github.py @@ -162,7 +162,7 @@ class JobStats(BaseModel): job_id: int created_at: datetime started_at: datetime - conclusion: Optional[JobConclusion] + conclusion: Optional[JobConclusion] = None @dataclasses.dataclass diff --git a/tests/unit/metrics/test_github.py b/tests/unit/metrics/test_github.py index 7d13495..d09d875 100644 --- a/tests/unit/metrics/test_github.py +++ b/tests/unit/metrics/test_github.py @@ -19,7 +19,7 @@ def pre_job_metrics_fixture() -> PreJobMetrics: """Create a PreJobMetrics object.""" return PreJobMetrics( repository="owner/repo", - workflow_run_id=1, + workflow_run_id="1", workflow="workflow", job_name="job", job_started_at=datetime(2021, 10, 1, 1, 0, 0, tzinfo=timezone.utc), diff --git a/tests/unit/reactive/test_consumer.py b/tests/unit/reactive/test_consumer.py index c036b92..5d0737c 100644 --- a/tests/unit/reactive/test_consumer.py +++ b/tests/unit/reactive/test_consumer.py @@ -30,7 +30,7 @@ def test_consume(caplog: pytest.LogCaptureFixture): # we use construct to avoid pydantic validation as IN_MEMORY_URI is not a valid URL consumer.consume(IN_MEMORY_URI, queue_name) assert str(job_details.labels) in caplog.text - assert job_details.run_url in caplog.text + assert str(job_details.run_url) in caplog.text @pytest.mark.parametrize( diff --git a/tests/unit/test_types_.py b/tests/unit/test_types_.py index 8784ba9..89e9f9a 100644 --- a/tests/unit/test_types_.py +++ b/tests/unit/test_types_.py @@ -2,6 +2,7 @@ # See LICENSE file for licensing details. """Module for testing the general types.""" import pytest +from pydantic import ValidationError from github_runner_manager.types_ import ProxyConfig @@ -13,10 +14,7 @@ def test_check_use_aproxy(): act: Call the check_use_aproxy method with the provided values. assert: Verify that the method raises a ValueError with the expected message. """ - values = {"http": None, "https": None} - use_aproxy = True + with pytest.raises(ValidationError) as exc_info: + ProxyConfig(use_aproxy=True) - with pytest.raises(ValueError) as exc_info: - ProxyConfig.check_use_aproxy(use_aproxy, values) - - assert str(exc_info.value) == "aproxy requires http or https to be set" + assert "aproxy requires http or https to be set" in str(exc_info.value)