Skip to content

tickets

InMemoryBackend

In-memory ticket backend for testing and prototyping.

Methods implement the TicketBackend protocol.

Example

Configure once at startup:

Ticket.register_backend("mem", InMemoryBackend(), make_default=True)

Your app code stays vendor-agnostic:

tid = Ticket.create(title="Login bug", description="Fails on Safari") Ticket.comment(tid, text="Reproduced on 17.0.3", author="qa-bot") t = Ticket.read(tid) t.change_status(Status.IN_PROGRESS)

Switch the whole app to Salesforce later without touching call sites:

Ticket.use_backend("sf")

Attributes:

Name Type Description
* _db (Dict[str, TicketData]

In-memory storage of tickets.

* _comments (Dict[str, List[Comment]]

In-memory storage of ticket comments.

* _seq (int

Sequence counter for generating ticket IDs.

Source code in wintermute/tickets.py
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
class InMemoryBackend:
    """In-memory ticket backend for testing and prototyping.

    Methods implement the TicketBackend protocol.

    Example:
        >>> # Configure once at startup:
        >>> Ticket.register_backend("mem", InMemoryBackend(), make_default=True)
        >>> # Your app code stays vendor-agnostic:
        >>> tid = Ticket.create(title="Login bug", description="Fails on Safari")
        >>> Ticket.comment(tid, text="Reproduced on 17.0.3", author="qa-bot")
        >>> t = Ticket.read(tid)
        >>> t.change_status(Status.IN_PROGRESS)
        >>> # Switch the whole app to Salesforce later without touching call sites:
        >>> Ticket.use_backend("sf")

    Attributes:
        * _db (Dict[str, TicketData]): In-memory storage of tickets.
        * _comments (Dict[str, List[Comment]]): In-memory storage of ticket comments.
        * _seq (int): Sequence counter for generating ticket IDs.
    """

    def __init__(self) -> None:
        self._db: Dict[str, TicketData] = {}
        self._comments: Dict[str, List[Comment]] = {}
        self._seq = 0

    def _next_id(self) -> str:
        self._seq += 1
        return f"T{self._seq:06d}"

    def create(self, data: TicketData) -> str:
        tid = self._next_id()
        self._db[tid] = data
        self._comments[tid] = list(data.communication)
        return tid

    def read(self, ticket_id: str) -> Tuple[TicketData, List[Comment]]:
        return (self._db[ticket_id], list(self._comments.get(ticket_id, [])))

    def update(self, ticket_id: str, fields: MutableMapping[str, Any]) -> None:
        d = self._db[ticket_id]
        for k, v in fields.items():
            if k == "status" and isinstance(v, str):
                v = Status(v)
            setattr(d, k, v)

    def add_comment(self, ticket_id: str, comment: Comment) -> None:
        self._comments.setdefault(ticket_id, []).append(comment)

Ticket dataclass

Bases: BaseModel

Ticket model with backend-agnostic CRUD operations.

This class provides a unified interface for creating, reading, updating, and commenting on tickets, regardless of the underlying backend system.

Example

Configure once at startup:

Ticket.register_backend("mem", InMemoryBackend(), make_default=True) Ticket.register_backend( ... "bugzilla", ... BugzillaBackend(base_url="https://bz.example", api_key="..."), ... ) Ticket.register_backend( ... "sf", SalesforceBackend(instance_url="...", access_token="...") ... )

App code stays vendor-agnostic:

tid = Ticket.create(title="Login bug", description="Fails on Safari") Ticket.comment(tid, text="Reproduced on 17.0.3", author="qa-bot") t = Ticket.read(tid) t.change_status(Status.IN_PROGRESS)

Switch the whole app to Salesforce later without touching call sites:

Ticket.use_backend("sf")

Attributes:

Name Type Description
* ticket_id (str

Unique identifier of the ticket.

* data (TicketData

Core data of the ticket.

* comments (List[Comment]

List of comments associated with the ticket.

Source code in wintermute/tickets.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
@dataclass
class Ticket(BaseModel, metaclass=TicketMeta):
    """Ticket model with backend-agnostic CRUD operations.

    This class provides a unified interface for creating, reading, updating,
    and commenting on tickets, regardless of the underlying backend system.

    Example:
        >>> # Configure once at startup:
        >>> Ticket.register_backend("mem", InMemoryBackend(), make_default=True)
        >>> Ticket.register_backend(
        ...     "bugzilla",
        ...     BugzillaBackend(base_url="https://bz.example", api_key="..."),
        ... )
        >>> Ticket.register_backend(
        ...     "sf", SalesforceBackend(instance_url="...", access_token="...")
        ... )
        >>> # App code stays vendor-agnostic:
        >>> tid = Ticket.create(title="Login bug", description="Fails on Safari")
        >>> Ticket.comment(tid, text="Reproduced on 17.0.3", author="qa-bot")
        >>> t = Ticket.read(tid)
        >>> t.change_status(Status.IN_PROGRESS)
        >>> # Switch the whole app to Salesforce later without touching call sites:
        >>> Ticket.use_backend("sf")


    Attributes:
        * ticket_id (str): Unique identifier of the ticket.
        * data (TicketData): Core data of the ticket.
        * comments (List[Comment]): List of comments associated with the ticket."""

    ticket_id: str
    data: TicketData
    comments: List[Comment] = field(default_factory=list)

    _backend: ClassVar[Optional[TicketBackend]] = None
    _backends: ClassVar[Dict[str, TicketBackend]] = {}

    @classmethod
    def set_backend(cls, backend: TicketBackend) -> None:
        raise NotImplementedError

    @classmethod
    def register_backend(
        cls, name: str, backend: TicketBackend, *, make_default: bool = False
    ) -> None:
        raise NotImplementedError

    @classmethod
    def use_backend(cls, name: str) -> None:
        raise NotImplementedError

    # NOTE: concrete types (no Type[T_Ticket]/T_Ticket)
    @classmethod
    def create(
        cls,
        *,
        title: str,
        description: str,
        assignee: Optional[str] = None,
        requester: Optional[str] = None,
        status: Status = Status.OPEN,
        custom_fields: Optional[Dict[str, Any]] = None,
    ) -> str:
        raise NotImplementedError

    @classmethod
    def read(cls, ticket_id: str) -> "Ticket":
        raise NotImplementedError

    @classmethod
    def update(cls, ticket_id: str, **fields: Any) -> None:
        raise NotImplementedError

    @classmethod
    def comment(cls, ticket_id: str, text: str, author: str) -> None:
        raise NotImplementedError

    __schema__ = {"data": TicketData, "comments": Comment}
    __enums__ = {}

    def to_dict(self) -> Dict[str, Any]:
        d = super().to_dict()
        d["data"]["status"] = self.data.status.value
        return d