-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcommon.py
More file actions
97 lines (68 loc) · 2.24 KB
/
common.py
File metadata and controls
97 lines (68 loc) · 2.24 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from abc import ABCMeta
from enum import Enum
from pathlib import Path
from typing import Optional
from pydantic import BaseModel, ConfigDict, model_validator
from codemodder.logging import logger
class CaseInsensitiveEnum(str, Enum):
@classmethod
def _missing_(cls, value: object):
if not isinstance(value, str):
return super()._missing_(value)
return cls.__members__.get(value.upper())
class CodeTFWriter(BaseModel, metaclass=ABCMeta):
def write_report(self, outfile: Path | str) -> int:
try:
Path(outfile).write_text(self.model_dump_json(exclude_none=True))
except Exception:
logger.exception("failed to write report file.")
# Any issues with writing the output file should exit status 2.
return 2
logger.debug("wrote report to %s", outfile)
return 0
class Rule(BaseModel):
id: str
name: str
url: Optional[str] = None
model_config = ConfigDict(frozen=True)
class Finding(BaseModel):
id: str
rule: Rule
model_config = ConfigDict(frozen=True)
class Action(CaseInsensitiveEnum):
ADD = "add"
REMOVE = "remove"
class PackageResult(CaseInsensitiveEnum):
COMPLETED = "completed"
FAILED = "failed"
SKIPPED = "skipped"
class DiffSide(CaseInsensitiveEnum):
LEFT = "left"
RIGHT = "right"
class PackageAction(BaseModel):
action: Action
result: PackageResult
package: str
class Change(BaseModel):
lineNumber: int
description: Optional[str]
diffSide: DiffSide = DiffSide.RIGHT
properties: Optional[dict] = None
packageActions: Optional[list[PackageAction]] = None
@model_validator(mode="after")
def validate_lineNumber(self):
if self.lineNumber < 1:
raise ValueError("lineNumber must be greater than 0")
return self
@model_validator(mode="after")
def validate_description(self):
if self.description is not None and not self.description:
raise ValueError("description must not be empty")
return self
class Rating(BaseModel):
score: int
description: Optional[str] = None
class FixQuality(BaseModel):
safetyRating: Rating
effectivenessRating: Rating
cleanlinessRating: Rating