continuum-task-server-bundle 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- continuum_task_server_bundle/__init__.py +1 -0
- continuum_task_server_bundle/daytona/__init__.py +4 -0
- continuum_task_server_bundle/daytona/common.py +534 -0
- continuum_task_server_bundle/daytona/create_sandbox.py +166 -0
- continuum_task_server_bundle/daytona/dispatch.py +125 -0
- continuum_task_server_bundle/daytona/execute_sandbox.py +243 -0
- continuum_task_server_bundle/daytona/filesystem.py +330 -0
- continuum_task_server_bundle/daytona/fs_edit.py +127 -0
- continuum_task_server_bundle/daytona/git_ops.py +585 -0
- continuum_task_server_bundle/daytona/github_app.py +207 -0
- continuum_task_server_bundle/daytona/manage_sandbox.py +104 -0
- continuum_task_server_bundle/daytona/plugin.py +18 -0
- continuum_task_server_bundle/daytona/s3_transfer.py +207 -0
- continuum_task_server_bundle/discord/__init__.py +4 -0
- continuum_task_server_bundle/discord/attachment_registry_db.py +127 -0
- continuum_task_server_bundle/discord/attachment_resolve.py +213 -0
- continuum_task_server_bundle/discord/await_cleanup.py +105 -0
- continuum_task_server_bundle/discord/discord_ready.py +39 -0
- continuum_task_server_bundle/discord/message_await_apply.py +143 -0
- continuum_task_server_bundle/discord/message_await_db.py +197 -0
- continuum_task_server_bundle/discord/message_await_inbound.py +54 -0
- continuum_task_server_bundle/discord/message_await_logic.py +133 -0
- continuum_task_server_bundle/discord/message_await_poller.py +122 -0
- continuum_task_server_bundle/discord/message_chunking.py +45 -0
- continuum_task_server_bundle/discord/methods.py +408 -0
- continuum_task_server_bundle/discord/plugin.py +105 -0
- continuum_task_server_bundle/discord/reaction_await_apply.py +238 -0
- continuum_task_server_bundle/discord/reaction_await_db.py +174 -0
- continuum_task_server_bundle/discord/reaction_await_inbound.py +47 -0
- continuum_task_server_bundle/discord/reaction_await_logic.py +118 -0
- continuum_task_server_bundle/discord/reaction_await_poller.py +131 -0
- continuum_task_server_bundle/discord/s3_attachment_config.py +25 -0
- continuum_task_server_bundle/discord/s3_message_attachments.py +139 -0
- continuum_task_server_bundle/discord/sql/001_message_awaits.sql +112 -0
- continuum_task_server_bundle/discord/sql/002_reaction_awaits.sql +61 -0
- continuum_task_server_bundle/github/__init__.py +4 -0
- continuum_task_server_bundle/github/awaits/__init__.py +1 -0
- continuum_task_server_bundle/github/awaits/apply.py +92 -0
- continuum_task_server_bundle/github/awaits/cleanup.py +94 -0
- continuum_task_server_bundle/github/awaits/db.py +220 -0
- continuum_task_server_bundle/github/awaits/evaluators.py +325 -0
- continuum_task_server_bundle/github/awaits/fetch.py +192 -0
- continuum_task_server_bundle/github/awaits/poller.py +82 -0
- continuum_task_server_bundle/github/awaits/register.py +37 -0
- continuum_task_server_bundle/github/ci_status.py +70 -0
- continuum_task_server_bundle/github/comment_normalize.py +76 -0
- continuum_task_server_bundle/github/errors.py +52 -0
- continuum_task_server_bundle/github/github_app.py +291 -0
- continuum_task_server_bundle/github/github_http.py +377 -0
- continuum_task_server_bundle/github/methods/__init__.py +90 -0
- continuum_task_server_bundle/github/methods/_common.py +183 -0
- continuum_task_server_bundle/github/methods/awaits.py +107 -0
- continuum_task_server_bundle/github/methods/ci.py +158 -0
- continuum_task_server_bundle/github/methods/comments.py +214 -0
- continuum_task_server_bundle/github/methods/credentials.py +59 -0
- continuum_task_server_bundle/github/methods/issues.py +131 -0
- continuum_task_server_bundle/github/methods/pulls.py +329 -0
- continuum_task_server_bundle/github/methods/repos.py +424 -0
- continuum_task_server_bundle/github/methods/search.py +109 -0
- continuum_task_server_bundle/github/plugin.py +91 -0
- continuum_task_server_bundle/github/sql/001_github_awaits.sql +65 -0
- continuum_task_server_bundle/github/webhook_server.py +126 -0
- continuum_task_server_bundle/jira/__init__.py +4 -0
- continuum_task_server_bundle/jira/await_poller.py +236 -0
- continuum_task_server_bundle/jira/await_store.py +271 -0
- continuum_task_server_bundle/jira/config.py +68 -0
- continuum_task_server_bundle/jira/jira_client.py +382 -0
- continuum_task_server_bundle/jira/methods.py +246 -0
- continuum_task_server_bundle/jira/plugin.py +86 -0
- continuum_task_server_bundle/jira/sql/001_jira_issue_comment_waits.sql +19 -0
- continuum_task_server_bundle/jira/webhook.py +70 -0
- continuum_task_server_bundle/openrouter/__init__.py +4 -0
- continuum_task_server_bundle/openrouter/chat_service.py +1037 -0
- continuum_task_server_bundle/openrouter/chat_store.py +849 -0
- continuum_task_server_bundle/openrouter/environment.py +29 -0
- continuum_task_server_bundle/openrouter/handler.py +59 -0
- continuum_task_server_bundle/openrouter/image_context.py +207 -0
- continuum_task_server_bundle/openrouter/openrouter_client.py +324 -0
- continuum_task_server_bundle/openrouter/plugin.py +37 -0
- continuum_task_server_bundle/openrouter/prompt_projection.py +417 -0
- continuum_task_server_bundle/openrouter/sql/chat.sql +50 -0
- continuum_task_server_bundle/openrouter/sql/migrate_prune_then_chunk_v1.sql +11 -0
- continuum_task_server_bundle/roblox/__init__.py +4 -0
- continuum_task_server_bundle/roblox/open_cloud.py +335 -0
- continuum_task_server_bundle/roblox/plugin.py +30 -0
- continuum_task_server_bundle/roblox/storage_s3.py +55 -0
- continuum_task_server_bundle-1.0.0.dist-info/METADATA +109 -0
- continuum_task_server_bundle-1.0.0.dist-info/RECORD +90 -0
- continuum_task_server_bundle-1.0.0.dist-info/WHEEL +5 -0
- continuum_task_server_bundle-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"""Jira methods invoked by the ``jira`` Continuum task type."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import re
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from continuum_task_server_bundle.jira.await_store import AwaitStore
|
|
10
|
+
from continuum_task_server_bundle.jira.config import Settings
|
|
11
|
+
from continuum_task_server_bundle.jira.jira_client import JiraApiError, JiraClient
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
_ISSUE_KEY = re.compile(r"^[A-Z][A-Z0-9_]*-\d+$", re.IGNORECASE)
|
|
16
|
+
_PROJECT_KEY = re.compile(r"^[A-Z][A-Z0-9_]{1,29}$", re.IGNORECASE)
|
|
17
|
+
_MAX_AWAIT_SECONDS = 30 * 24 * 60 * 60
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _required_text(data: dict[str, Any], key: str) -> str:
|
|
21
|
+
value = data.get(key)
|
|
22
|
+
if not isinstance(value, str) or not value.strip():
|
|
23
|
+
raise ValueError(f"{key} is required")
|
|
24
|
+
return value.strip()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _issue_key(data: dict[str, Any]) -> str:
|
|
28
|
+
value = _required_text(data, "issue_key")
|
|
29
|
+
if not _ISSUE_KEY.fullmatch(value):
|
|
30
|
+
raise ValueError("issue_key must look like TEST-123")
|
|
31
|
+
return value.upper()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _project_key(data: dict[str, Any]) -> str:
|
|
35
|
+
value = _required_text(data, "project_key")
|
|
36
|
+
if not _PROJECT_KEY.fullmatch(value):
|
|
37
|
+
raise ValueError("project_key must contain only letters, numbers, or underscores")
|
|
38
|
+
return value.upper()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _bounded_int(value: Any, name: str, *, minimum: int, maximum: int, default: int) -> int:
|
|
42
|
+
if value is None:
|
|
43
|
+
return default
|
|
44
|
+
try:
|
|
45
|
+
number = int(value)
|
|
46
|
+
except (TypeError, ValueError) as error:
|
|
47
|
+
raise ValueError(f"{name} must be an integer") from error
|
|
48
|
+
if not minimum <= number <= maximum:
|
|
49
|
+
raise ValueError(f"{name} must be between {minimum} and {maximum}")
|
|
50
|
+
return number
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def dispatch_method(
|
|
54
|
+
method: str,
|
|
55
|
+
input_data: dict[str, Any] | None,
|
|
56
|
+
*,
|
|
57
|
+
jira: JiraClient,
|
|
58
|
+
await_store: AwaitStore,
|
|
59
|
+
settings: Settings,
|
|
60
|
+
queue_item_id: str,
|
|
61
|
+
context: Any = None,
|
|
62
|
+
) -> dict[str, Any] | None:
|
|
63
|
+
data = input_data if isinstance(input_data, dict) else {}
|
|
64
|
+
|
|
65
|
+
if method == "get_issue":
|
|
66
|
+
key = _issue_key(data)
|
|
67
|
+
comment_limit = _bounded_int(data.get("comment_limit"), "comment_limit", minimum=1, maximum=100, default=100)
|
|
68
|
+
result = jira.get_issue(key, include_comments=bool(data.get("include_comments", True)), comment_limit=comment_limit)
|
|
69
|
+
# Preserve the existing Jira task-item contract: space_name is the Jira project key.
|
|
70
|
+
result["space_name"] = str(result.get("project_key") or "")
|
|
71
|
+
result["summary"] = str(result.get("summary") or "")
|
|
72
|
+
result["status"] = str(result.get("status") or "")
|
|
73
|
+
return result
|
|
74
|
+
|
|
75
|
+
if method == "add_comment":
|
|
76
|
+
key = _issue_key(data)
|
|
77
|
+
body = data.get("body")
|
|
78
|
+
body_adf = data.get("body_adf")
|
|
79
|
+
if body is not None and not isinstance(body, str):
|
|
80
|
+
raise ValueError("body must be text")
|
|
81
|
+
if body_adf is not None and not isinstance(body_adf, dict):
|
|
82
|
+
raise ValueError("body_adf must be an object")
|
|
83
|
+
return jira.add_comment(key, body=body, body_adf=body_adf)
|
|
84
|
+
|
|
85
|
+
if method == "search_issues":
|
|
86
|
+
jql = _required_text(data, "jql")
|
|
87
|
+
max_results = _bounded_int(data.get("max_results"), "max_results", minimum=1, maximum=100, default=50)
|
|
88
|
+
token = data.get("next_page_token")
|
|
89
|
+
if token is not None and not isinstance(token, str):
|
|
90
|
+
raise ValueError("next_page_token must be text")
|
|
91
|
+
return jira.search_issues(jql, max_results=max_results, next_page_token=token)
|
|
92
|
+
|
|
93
|
+
if method == "list_projects":
|
|
94
|
+
start_at = _bounded_int(data.get("start_at"), "start_at", minimum=0, maximum=100000, default=0)
|
|
95
|
+
max_results = _bounded_int(data.get("max_results"), "max_results", minimum=1, maximum=100, default=50)
|
|
96
|
+
return jira.list_projects(start_at=start_at, max_results=max_results)
|
|
97
|
+
|
|
98
|
+
if method == "get_project":
|
|
99
|
+
return jira.get_project(_project_key(data))
|
|
100
|
+
|
|
101
|
+
if method == "list_issue_types":
|
|
102
|
+
return jira.list_issue_types(_project_key(data))
|
|
103
|
+
|
|
104
|
+
if method == "create_issue":
|
|
105
|
+
project_key = _project_key(data)
|
|
106
|
+
issue_type_id = _required_text(data, "issue_type_id")
|
|
107
|
+
summary = _required_text(data, "summary")
|
|
108
|
+
description = data.get("description")
|
|
109
|
+
labels = data.get("labels")
|
|
110
|
+
if description is not None and not isinstance(description, str):
|
|
111
|
+
raise ValueError("description must be text")
|
|
112
|
+
if labels is not None and (not isinstance(labels, list) or not all(isinstance(x, str) for x in labels)):
|
|
113
|
+
raise ValueError("labels must be an array of strings")
|
|
114
|
+
return jira.create_issue(
|
|
115
|
+
project_key=project_key,
|
|
116
|
+
issue_type_id=issue_type_id,
|
|
117
|
+
summary=summary,
|
|
118
|
+
description=description,
|
|
119
|
+
labels=labels,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
if method == "update_issue":
|
|
123
|
+
key = _issue_key(data)
|
|
124
|
+
fields = data.get("fields")
|
|
125
|
+
if not isinstance(fields, dict):
|
|
126
|
+
raise ValueError("fields must be an object")
|
|
127
|
+
return jira.update_issue(key, fields)
|
|
128
|
+
|
|
129
|
+
if method == "get_transitions":
|
|
130
|
+
return jira.get_transitions(_issue_key(data))
|
|
131
|
+
|
|
132
|
+
if method == "transition_issue":
|
|
133
|
+
key = _issue_key(data)
|
|
134
|
+
transition_id = _required_text(data, "transition_id")
|
|
135
|
+
return jira.transition_issue(key, transition_id)
|
|
136
|
+
|
|
137
|
+
if method == "await_issue_comments":
|
|
138
|
+
return await_issue_comments(
|
|
139
|
+
data,
|
|
140
|
+
jira=jira,
|
|
141
|
+
await_store=await_store,
|
|
142
|
+
settings=settings,
|
|
143
|
+
queue_item_id=queue_item_id,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
if method == "end_ticket_worker":
|
|
147
|
+
key = _issue_key(data)
|
|
148
|
+
reason = str(data.get("end_reason") or "ended").strip() or "ended"
|
|
149
|
+
affected = await_store.finish_issue_waits(key, end_reason=reason)
|
|
150
|
+
return {"success": True, "issue_key": key, "ended_waits": len(affected), "end_reason": reason}
|
|
151
|
+
|
|
152
|
+
return {"success": False, "error": f"Method not found: {method}"}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def await_issue_comments(
|
|
156
|
+
data: dict[str, Any],
|
|
157
|
+
*,
|
|
158
|
+
jira: JiraClient,
|
|
159
|
+
await_store: AwaitStore,
|
|
160
|
+
settings: Settings,
|
|
161
|
+
queue_item_id: str,
|
|
162
|
+
) -> None:
|
|
163
|
+
"""Persist a Jira comment wait and return; a background poller completes it.
|
|
164
|
+
|
|
165
|
+
The stored wait survives handler return and task-server restarts. Webhooks or
|
|
166
|
+
the poller mark it terminal, and the poller heartbeats/completes the queue item.
|
|
167
|
+
"""
|
|
168
|
+
key = _issue_key(data)
|
|
169
|
+
timeout_seconds = _bounded_int(
|
|
170
|
+
data.get("timeout_seconds"), "timeout_seconds", minimum=1, maximum=_MAX_AWAIT_SECONDS, default=7 * 24 * 60 * 60
|
|
171
|
+
)
|
|
172
|
+
poll_seconds = settings.database_poll_seconds
|
|
173
|
+
if data.get("poll_interval_seconds") is not None:
|
|
174
|
+
try:
|
|
175
|
+
poll_seconds = float(data["poll_interval_seconds"])
|
|
176
|
+
except (TypeError, ValueError) as error:
|
|
177
|
+
raise ValueError("poll_interval_seconds must be a number") from error
|
|
178
|
+
if not 0.25 <= poll_seconds <= 60:
|
|
179
|
+
raise ValueError("poll_interval_seconds must be between 0.25 and 60")
|
|
180
|
+
|
|
181
|
+
ignore_ids = data.get("ignore_comment_ids") or []
|
|
182
|
+
if not isinstance(ignore_ids, list) or len(ignore_ids) > 100 or not all(isinstance(value, (str, int)) for value in ignore_ids):
|
|
183
|
+
raise ValueError("ignore_comment_ids must be an array of at most 100 comment IDs")
|
|
184
|
+
filters: dict[str, Any] = {
|
|
185
|
+
"ignore_comment_ids": [str(value) for value in ignore_ids],
|
|
186
|
+
# Kept with the row so the background poller honors each wait's setting.
|
|
187
|
+
"poll_interval_seconds": poll_seconds,
|
|
188
|
+
}
|
|
189
|
+
for field in ("author_account_id", "exclude_author_account_id", "mention_account_id", "contains_text"):
|
|
190
|
+
value = data.get(field)
|
|
191
|
+
if value is not None:
|
|
192
|
+
if not isinstance(value, str) or not value.strip():
|
|
193
|
+
raise ValueError(f"{field} must be non-empty text")
|
|
194
|
+
filters[field] = value.strip()
|
|
195
|
+
|
|
196
|
+
await_store.register_wait(
|
|
197
|
+
queue_item_id=queue_item_id,
|
|
198
|
+
issue_key=key,
|
|
199
|
+
timeout_seconds=timeout_seconds,
|
|
200
|
+
filters=filters,
|
|
201
|
+
)
|
|
202
|
+
# Returning None leaves the queue item started. The dedicated poller owns its
|
|
203
|
+
# heartbeat and eventual complete_queue_item call, freeing this handler slot.
|
|
204
|
+
return None
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _poll_jira_for_comment(jira: JiraClient, await_store: AwaitStore, issue_key: str) -> None:
|
|
208
|
+
"""Find matching comments or end active waits when the issue is done.
|
|
209
|
+
|
|
210
|
+
Used only when no Jira webhook is configured. Transient failures are logged
|
|
211
|
+
so the background poller can retry on the next scheduled poll.
|
|
212
|
+
"""
|
|
213
|
+
try:
|
|
214
|
+
comments = jira.get_comments(issue_key, max_results=100)
|
|
215
|
+
except JiraApiError as error:
|
|
216
|
+
if _transient_jira_error(error):
|
|
217
|
+
logger.warning("Jira comment poll for %s failed with HTTP %s", issue_key, error.status_code)
|
|
218
|
+
comments = []
|
|
219
|
+
else:
|
|
220
|
+
raise
|
|
221
|
+
else:
|
|
222
|
+
# Newest-first pages are walked oldest-first so the earliest match wins.
|
|
223
|
+
for comment in reversed(comments):
|
|
224
|
+
await_store.complete_for_comment(
|
|
225
|
+
issue_key=issue_key,
|
|
226
|
+
comment={
|
|
227
|
+
"id": comment.get("id"),
|
|
228
|
+
"author": {"accountId": comment.get("author_account_id")},
|
|
229
|
+
"body": comment.get("body_adf"),
|
|
230
|
+
"created": comment.get("created"),
|
|
231
|
+
},
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
try:
|
|
235
|
+
issue = jira.get_issue(issue_key, include_comments=False)
|
|
236
|
+
except JiraApiError as error:
|
|
237
|
+
if _transient_jira_error(error):
|
|
238
|
+
logger.warning("Jira issue poll for %s failed with HTTP %s", issue_key, error.status_code)
|
|
239
|
+
return
|
|
240
|
+
raise
|
|
241
|
+
if issue.get("status_category") == "done":
|
|
242
|
+
await_store.finish_issue_waits(issue_key, end_reason="issue_closed")
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _transient_jira_error(error: JiraApiError) -> bool:
|
|
246
|
+
return error.status_code == 429 or error.status_code >= 500
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""Jira task registration and managed polling/webhook lifecycle."""
|
|
2
|
+
import logging
|
|
3
|
+
import threading
|
|
4
|
+
from waitress.server import create_server
|
|
5
|
+
from waitress.task import ThreadedTaskDispatcher
|
|
6
|
+
|
|
7
|
+
from .await_poller import run_await_poller
|
|
8
|
+
from .await_store import AwaitStore
|
|
9
|
+
from .config import Settings
|
|
10
|
+
from .jira_client import JiraApiError, JiraClient
|
|
11
|
+
from .methods import dispatch_method
|
|
12
|
+
from .webhook import create_webhook_app
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
class _JiraService:
|
|
17
|
+
def __init__(self, server, jira, store, settings):
|
|
18
|
+
self.server, self.jira, self.store, self.settings = server, jira, store, settings
|
|
19
|
+
self.stopping = threading.Event()
|
|
20
|
+
self.web = None
|
|
21
|
+
self.dispatcher = None
|
|
22
|
+
self.threads = []
|
|
23
|
+
|
|
24
|
+
def start(self):
|
|
25
|
+
logging.getLogger("waitress").setLevel(logging.WARNING)
|
|
26
|
+
self.dispatcher = ThreadedTaskDispatcher()
|
|
27
|
+
self.dispatcher.set_thread_count(8)
|
|
28
|
+
self.web = create_server(create_webhook_app(self.settings, self.store), host=self.settings.webhook_host, port=self.settings.webhook_port, threads=8, map={}, asyncore_loop_timeout=1, _dispatcher=self.dispatcher)
|
|
29
|
+
self.threads = [threading.Thread(target=run_await_poller, args=(self.server, self.jira, self.store, self.settings, self.stopping), name="jira-await-poller", daemon=True), threading.Thread(target=self.web.run, name="jira-webhook", daemon=True)]
|
|
30
|
+
for thread in self.threads:
|
|
31
|
+
thread.start()
|
|
32
|
+
|
|
33
|
+
def stop(self):
|
|
34
|
+
self.stopping.set()
|
|
35
|
+
if self.web is not None:
|
|
36
|
+
self.web.close()
|
|
37
|
+
if self.dispatcher is not None:
|
|
38
|
+
self.dispatcher.shutdown()
|
|
39
|
+
for thread in self.threads:
|
|
40
|
+
if thread.ident is not None:
|
|
41
|
+
thread.join(timeout=10)
|
|
42
|
+
if any(thread.is_alive() for thread in self.threads):
|
|
43
|
+
raise RuntimeError("Jira service failed to stop")
|
|
44
|
+
self.jira.close()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def register_jira(host, *, settings=None):
|
|
48
|
+
settings = Settings.from_env() if settings is None else settings
|
|
49
|
+
settings.validate(require_continuum=False)
|
|
50
|
+
store = AwaitStore(settings.database_url)
|
|
51
|
+
jira = JiraClient(settings.jira_base_url, settings.jira_email, settings.jira_api_token, basic_auth=settings.jira_basic_auth or None)
|
|
52
|
+
server = host.server
|
|
53
|
+
service = _JiraService(server, jira, store, settings)
|
|
54
|
+
|
|
55
|
+
def cancel(queue_id):
|
|
56
|
+
try:
|
|
57
|
+
store.cancel_wait(queue_id)
|
|
58
|
+
except Exception as error:
|
|
59
|
+
logger.warning("could not mark Jira wait %s cancelled (%s)", queue_id, type(error).__name__)
|
|
60
|
+
|
|
61
|
+
def handle(item, context):
|
|
62
|
+
method = "unknown"
|
|
63
|
+
context.on_cancelled(lambda: cancel(item.id))
|
|
64
|
+
if context.is_cancelled():
|
|
65
|
+
return
|
|
66
|
+
try:
|
|
67
|
+
version = server.client.task_items.versions.get_by_id(item.task_item_version_id)
|
|
68
|
+
definition = version.item_definition_json or {}
|
|
69
|
+
method = definition.get("method") if isinstance(definition, dict) else None
|
|
70
|
+
if not isinstance(method, str) or not method:
|
|
71
|
+
result = {"success": False, "error": "Task item definition must include a method"}
|
|
72
|
+
else:
|
|
73
|
+
result = dispatch_method(method, item.input_data_json, jira=jira, await_store=store, settings=settings, queue_item_id=str(item.id))
|
|
74
|
+
except (ValueError, JiraApiError) as error:
|
|
75
|
+
logger.warning("Jira task method %s rejected/failed: %s", method, error)
|
|
76
|
+
result = {"success": False, "error": str(error)}
|
|
77
|
+
except Exception as error:
|
|
78
|
+
logger.error("Jira task method %s failed with %s", method, type(error).__name__)
|
|
79
|
+
result = {"success": False, "error": "internal_error"}
|
|
80
|
+
if context.is_cancelled():
|
|
81
|
+
cancel(item.id)
|
|
82
|
+
elif result is not None:
|
|
83
|
+
server.complete_queue_item(item.id, output_data=result)
|
|
84
|
+
|
|
85
|
+
server.register("jira", handle, concurrency=settings.max_workers, auto_complete=False, occupy_slot_until_complete=False)
|
|
86
|
+
host.add_service("jira", service)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
-- Safe, idempotent migration for durable Jira issue-comment waits.
|
|
2
|
+
-- Intentionally contains no DROP/DELETE/TRUNCATE statements.
|
|
3
|
+
CREATE TABLE IF NOT EXISTS jira_issue_comment_waits (
|
|
4
|
+
queue_item_id UUID PRIMARY KEY,
|
|
5
|
+
issue_key TEXT NOT NULL,
|
|
6
|
+
status TEXT NOT NULL DEFAULT 'active'
|
|
7
|
+
CHECK (status IN ('active', 'completed', 'timed_out', 'cancelled')),
|
|
8
|
+
registered_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
9
|
+
deadline_at TIMESTAMPTZ NOT NULL,
|
|
10
|
+
filters JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
11
|
+
result_json JSONB,
|
|
12
|
+
matched_comment_id TEXT,
|
|
13
|
+
completed_at TIMESTAMPTZ,
|
|
14
|
+
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
CREATE INDEX IF NOT EXISTS jira_issue_comment_waits_active_issue_idx
|
|
18
|
+
ON jira_issue_comment_waits (issue_key, registered_at)
|
|
19
|
+
WHERE status = 'active';
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Authenticated Jira webhook receiver for durable issue-comment waits."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import hmac
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from flask import Flask, jsonify, request
|
|
10
|
+
|
|
11
|
+
from continuum_task_server_bundle.jira.await_store import AwaitStore
|
|
12
|
+
from continuum_task_server_bundle.jira.config import Settings
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _valid_secret(secret: str) -> bool:
|
|
16
|
+
if not secret:
|
|
17
|
+
return False
|
|
18
|
+
# Jira webhook configuration can send a custom header or a URL token.
|
|
19
|
+
supplied = request.headers.get("X-Webhook-Secret") or request.args.get("token") or ""
|
|
20
|
+
if supplied and hmac.compare_digest(supplied, secret):
|
|
21
|
+
return True
|
|
22
|
+
signature = request.headers.get("X-Atlassian-Webhook-Signature") or request.headers.get("X-Hub-Signature") or ""
|
|
23
|
+
if signature.startswith("sha256="):
|
|
24
|
+
body = request.get_data(cache=True) or b""
|
|
25
|
+
expected = "sha256=" + hmac.new(secret.encode("utf-8"), body, hashlib.sha256).hexdigest()
|
|
26
|
+
return hmac.compare_digest(signature, expected)
|
|
27
|
+
return False
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _issue_key(issue: dict[str, Any]) -> str | None:
|
|
31
|
+
key = issue.get("key")
|
|
32
|
+
return str(key).upper() if key else None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def create_webhook_app(settings: Settings, await_store: AwaitStore) -> Flask:
|
|
36
|
+
app = Flask(__name__)
|
|
37
|
+
|
|
38
|
+
@app.get("/healthz")
|
|
39
|
+
def healthz():
|
|
40
|
+
return jsonify({"ok": True, "service": "continuum-jira-task-server"})
|
|
41
|
+
|
|
42
|
+
@app.post("/webhooks/jira")
|
|
43
|
+
def jira_webhook():
|
|
44
|
+
if not _valid_secret(settings.webhook_secret):
|
|
45
|
+
return jsonify({"error": "unauthorized"}), 401
|
|
46
|
+
|
|
47
|
+
payload = request.get_json(silent=True) or {}
|
|
48
|
+
event = str(payload.get("webhookEvent") or payload.get("issue_event_type_name") or "").lower()
|
|
49
|
+
issue = payload.get("issue") or {}
|
|
50
|
+
issue_key = _issue_key(issue)
|
|
51
|
+
comment = payload.get("comment")
|
|
52
|
+
|
|
53
|
+
# Only newly-created comments resolve waits; edits/deletions are ignored.
|
|
54
|
+
if issue_key and isinstance(comment, dict) and (
|
|
55
|
+
"comment_created" in event or not event
|
|
56
|
+
):
|
|
57
|
+
completed = await_store.complete_for_comment(issue_key=issue_key, comment=comment)
|
|
58
|
+
return jsonify({"ok": True, "completed_waits": len(completed)})
|
|
59
|
+
|
|
60
|
+
# A closed issue ends pending comment waits deterministically.
|
|
61
|
+
fields = issue.get("fields") or {}
|
|
62
|
+
status = fields.get("status") or {}
|
|
63
|
+
category = (status.get("statusCategory") or {}).get("key")
|
|
64
|
+
if issue_key and category == "done" and ("issue" in event or not event):
|
|
65
|
+
ended = await_store.finish_issue_waits(issue_key, end_reason="issue_closed")
|
|
66
|
+
return jsonify({"ok": True, "ended_waits": len(ended)})
|
|
67
|
+
|
|
68
|
+
return jsonify({"ok": True, "ignored": True})
|
|
69
|
+
|
|
70
|
+
return app
|