Repository navigation
Expand file tree
/
Copy pathevents.py
More file actions
75 lines (54 loc) · 2.66 KB
/
Copy pathevents.py
File metadata and controls
75 lines (54 loc) · 2.66 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
"""Normalized Jira webhook events consumed by the agent runtime."""
from __future__ import annotations
from datetime import datetime
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
EventKind = Literal["issue_created", "issue_updated", "comment_created"]
"""Subset of Jira webhook event types the agent acts on."""
class WebhookActor(BaseModel):
"""Identity of the actor that produced the underlying Jira event."""
model_config = ConfigDict(extra="forbid", frozen=True)
account_id: str | None = Field(default=None)
email: str | None = Field(default=None)
display_name: str | None = Field(default=None)
class ChangedField(BaseModel):
"""Single field change captured from a Jira changelog entry."""
model_config = ConfigDict(extra="forbid", frozen=True)
field: str
from_value: str | None = Field(default=None)
to_value: str | None = Field(default=None)
class NormalizedJiraEvent(BaseModel):
"""Canonical representation of a Jira webhook event after normalization."""
model_config = ConfigDict(extra="forbid", frozen=True)
event_kind: EventKind
issue_key: str = Field(min_length=1)
webhook_event_id: str = Field(min_length=1)
occurred_at: datetime
actor: WebhookActor
comment_id: str | None = Field(default=None)
changelog_id: str | None = Field(default=None)
issue_created_at: str | None = Field(default=None)
changed_fields: tuple[ChangedField, ...] = Field(default_factory=tuple)
tenant: str | None = Field(default=None)
@property
def is_comment(self) -> bool:
return self.event_kind == "comment_created"
@property
def change_id(self) -> str | None:
"""Per-event-kind change identifier used by the idempotency triple.
Per PLAN.md §5.3, the idempotency key includes a third component
beyond ``(issue_key, webhook_event_id)`` so back-to-back Atlassian
replays of the same ``webhook_event_id`` against different content
are not over-deduplicated.
* ``comment_created`` -> ``comment.id`` (the comment that triggered the event).
* ``issue_updated`` -> ``changelog.id`` (the Jira changelog batch id).
* ``issue_created`` -> ``issue.fields.created`` (the issue's create timestamp,
serialised as a string by Jira). When absent (older payloads, fixtures),
the property returns ``None`` and the dedupe key falls back to the
``(issue_key, webhook_event_id)`` pair.
"""
if self.event_kind == "comment_created":
return self.comment_id
if self.event_kind == "issue_updated":
return self.changelog_id
return self.issue_created_at