File size: 1,558 Bytes
b196357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from __future__ import annotations

from enum import StrEnum

from pydantic import BaseModel, ConfigDict, model_validator


class ActionType(StrEnum):
    FLAG_STYLE = "FLAG_STYLE"
    FLAG_BUG = "FLAG_BUG"
    FLAG_SECURITY = "FLAG_SECURITY"
    FLAG_DEPENDENCY_ISSUE = "FLAG_DEPENDENCY_ISSUE"
    ADD_COMMENT = "ADD_COMMENT"
    REQUEST_CONTEXT = "REQUEST_CONTEXT"
    REQUEST_CHANGES = "REQUEST_CHANGES"
    APPROVE = "APPROVE"
    AMEND_REVIEW = "AMEND_REVIEW"


class ReviewAction(BaseModel):
    """Validated action envelope for grading and persistence."""

    model_config = ConfigDict(strict=True, extra="forbid")

    action_type: ActionType
    target_line: int | None = None
    content: str | None = None
    attributed_to: str | None = None
    context_request: str | None = None

    @model_validator(mode="after")
    def validate_required_fields(self) -> "ReviewAction":
        content_required = {
            ActionType.ADD_COMMENT,
            ActionType.AMEND_REVIEW,
        }
        if self.action_type in content_required and not (self.content and self.content.strip()):
            raise ValueError("content is required for ADD_COMMENT and AMEND_REVIEW")

        if self.action_type == ActionType.REQUEST_CONTEXT and not (
            self.context_request and self.context_request.strip()
        ):
            raise ValueError("context_request is required for REQUEST_CONTEXT")

        if self.target_line is not None and self.target_line <= 0:
            raise ValueError("target_line must be positive")

        return self