codapact-jira 0.1.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.
- codapact_jira/__init__.py +6 -0
- codapact_jira/__main__.py +6 -0
- codapact_jira/_docformat.py +233 -0
- codapact_jira/_model.json +120757 -0
- codapact_jira/cli.py +7676 -0
- codapact_jira/client.py +10224 -0
- codapact_jira/py.typed +0 -0
- codapact_jira/skill/SKILL.md +39 -0
- codapact_jira/types.py +11043 -0
- codapact_jira-0.1.0.dist-info/METADATA +129 -0
- codapact_jira-0.1.0.dist-info/RECORD +14 -0
- codapact_jira-0.1.0.dist-info/WHEEL +4 -0
- codapact_jira-0.1.0.dist-info/entry_points.txt +3 -0
- codapact_jira-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"""Markdown in, Markdown out: Jira rich-text fields as Markdown instead of ADF JSON.
|
|
2
|
+
|
|
3
|
+
Jira stores descriptions, comments and other rich text as Atlassian Document Format
|
|
4
|
+
(ADF). With ``--doc-format markdown`` (the default; ``JIRA_DOC_FORMAT`` sets it too):
|
|
5
|
+
|
|
6
|
+
* a string in an issue's rich-text fields (``fields.description``, ``fields.environment``,
|
|
7
|
+
their ``update`` ``set`` operations, a comment ``add`` body), a comment ``body`` or a
|
|
8
|
+
worklog ``comment`` is converted from Markdown to ADF before it is sent; other strings
|
|
9
|
+
(``historyMetadata.description``, property values, summaries) and ADF documents are not;
|
|
10
|
+
* every ADF document in a response is converted to Markdown.
|
|
11
|
+
|
|
12
|
+
``--doc-format adf`` sends and returns ADF unchanged. Conversion uses adflux with its Jira
|
|
13
|
+
profile (Jira emoji, panels as ``> [!NOTE]`` alerts, ``:status[...]``...); run
|
|
14
|
+
``uvx adflux syntax`` for the dialect.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import argparse
|
|
20
|
+
import os
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from adflux import AdfluxError, to_markdown
|
|
24
|
+
from adflux import to_adf as _to_adf
|
|
25
|
+
from codapact.runtime.errors import ValidationError
|
|
26
|
+
|
|
27
|
+
PROFILE = "jira"
|
|
28
|
+
CHOICES = ("markdown", "adf")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def to_adf(markdown: str, profile: str) -> dict[str, Any]:
|
|
32
|
+
"""Markdown to ADF; Markdown adflux cannot read is a usage error, not a crash."""
|
|
33
|
+
try:
|
|
34
|
+
return _to_adf(markdown, profile)
|
|
35
|
+
except AdfluxError as error:
|
|
36
|
+
raise usage(f"cannot convert the Markdown: {error}") from None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def is_adf(value: Any) -> bool:
|
|
40
|
+
"""Whether ``value`` is an ADF document."""
|
|
41
|
+
return (
|
|
42
|
+
isinstance(value, dict)
|
|
43
|
+
and value.get("type") == "doc"
|
|
44
|
+
and isinstance(value.get("content"), list)
|
|
45
|
+
and "version" in value
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def to_wire(value: Any, path: tuple[str, ...]) -> Any:
|
|
50
|
+
"""Convert the Markdown string at ``path`` (``*``: each list item) to ADF.
|
|
51
|
+
|
|
52
|
+
Only that exact place changes; a value already ADF, or not a string, is left alone.
|
|
53
|
+
"""
|
|
54
|
+
if not path:
|
|
55
|
+
return to_adf(value, PROFILE) if isinstance(value, str) else value
|
|
56
|
+
key, rest = path[0], path[1:]
|
|
57
|
+
if key == "*":
|
|
58
|
+
return [to_wire(v, rest) for v in value] if isinstance(value, list) else value
|
|
59
|
+
if isinstance(value, dict) and key in value:
|
|
60
|
+
return {**value, key: to_wire(value[key], rest)}
|
|
61
|
+
return value
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def from_wire(value: Any) -> Any:
|
|
65
|
+
"""Convert every ADF document in ``value`` to Markdown, recursively."""
|
|
66
|
+
if is_adf(value):
|
|
67
|
+
try:
|
|
68
|
+
return to_markdown(value, PROFILE)
|
|
69
|
+
except AdfluxError:
|
|
70
|
+
return value # doc-shaped JSON that is not ADF (an entity property, say)
|
|
71
|
+
if isinstance(value, dict):
|
|
72
|
+
return {k: from_wire(v) for k, v in value.items()}
|
|
73
|
+
if isinstance(value, list):
|
|
74
|
+
return [from_wire(v) for v in value]
|
|
75
|
+
return value
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# Operations whose request bodies carry ADF. Elsewhere ``description`` and friends are
|
|
79
|
+
# plain strings (project, field and scheme descriptions), so they are left alone.
|
|
80
|
+
SLOTS: dict[str, tuple[str, ...]] = {
|
|
81
|
+
"create_issue": ("fields", "description"),
|
|
82
|
+
"edit_issue": ("fields", "description"),
|
|
83
|
+
"add_comment": ("body",),
|
|
84
|
+
"update_comment": ("body",),
|
|
85
|
+
"add_worklog": ("comment",),
|
|
86
|
+
"update_worklog": ("comment",),
|
|
87
|
+
}
|
|
88
|
+
# Where each operation's request body carries ADF (paths below ``body``; ``*`` is each
|
|
89
|
+
# item of a list). ``fields`` and ``update`` are free-form maps keyed by Jira field id.
|
|
90
|
+
ISSUE_PATHS: tuple[tuple[str, ...], ...] = (
|
|
91
|
+
("fields", "description"),
|
|
92
|
+
("fields", "environment"),
|
|
93
|
+
("update", "description", "*", "set"),
|
|
94
|
+
("update", "environment", "*", "set"),
|
|
95
|
+
("update", "comment", "*", "add", "body"),
|
|
96
|
+
("update", "worklog", "*", "add", "comment"),
|
|
97
|
+
)
|
|
98
|
+
RICH_TEXT: dict[str, tuple[tuple[str, ...], ...]] = {
|
|
99
|
+
"create_issue": ISSUE_PATHS,
|
|
100
|
+
"edit_issue": ISSUE_PATHS,
|
|
101
|
+
"do_transition": ISSUE_PATHS,
|
|
102
|
+
# the Python field name or Jira's own: the hook runs before the body is encoded
|
|
103
|
+
"create_issues": tuple(
|
|
104
|
+
(name, "*", *path) for name in ("issue_updates", "issueUpdates") for path in ISSUE_PATHS
|
|
105
|
+
),
|
|
106
|
+
"add_comment": (("body",),),
|
|
107
|
+
"update_comment": (("body",),),
|
|
108
|
+
"add_worklog": (("comment",),),
|
|
109
|
+
"update_worklog": (("comment",),),
|
|
110
|
+
"link_issues": (("comment", "body"),),
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def usage(message: str) -> ValidationError:
|
|
115
|
+
"""An input error the CLI reports as a usage error (exit 2, structured with JSON)."""
|
|
116
|
+
return ValidationError("--markdown", message)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _slot(operation: Any) -> tuple[str, ...]:
|
|
120
|
+
"""Where ``--markdown`` goes in this operation's request body."""
|
|
121
|
+
try:
|
|
122
|
+
return SLOTS[str(getattr(operation, "name", ""))]
|
|
123
|
+
except KeyError:
|
|
124
|
+
raise usage("is for creating or editing issues, comments and worklogs") from None
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class MarkdownHook:
|
|
128
|
+
"""Call hook that converts rich text between Markdown and ADF."""
|
|
129
|
+
|
|
130
|
+
def __init__(self, markdown: str | None = None) -> None:
|
|
131
|
+
self.markdown = markdown
|
|
132
|
+
|
|
133
|
+
def before(self, operation: Any, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
134
|
+
if self.markdown is not None:
|
|
135
|
+
path = _slot(operation)
|
|
136
|
+
body = arguments.get("body")
|
|
137
|
+
if body is not None and not isinstance(body, dict):
|
|
138
|
+
raise usage("needs a JSON object body")
|
|
139
|
+
request = dict(body or {})
|
|
140
|
+
target = request
|
|
141
|
+
for key in path[:-1]:
|
|
142
|
+
inner = target.get(key)
|
|
143
|
+
if inner is not None and not isinstance(inner, dict):
|
|
144
|
+
raise usage(f"needs --{key.replace('_', '-')} to be a JSON object")
|
|
145
|
+
target[key] = dict(inner or {})
|
|
146
|
+
target = target[key]
|
|
147
|
+
target[path[-1]] = self.markdown
|
|
148
|
+
arguments = {**arguments, "body": request}
|
|
149
|
+
paths = RICH_TEXT.get(str(getattr(operation, "name", "")), ())
|
|
150
|
+
body = arguments.get("body")
|
|
151
|
+
if not paths or not isinstance(body, dict):
|
|
152
|
+
return arguments
|
|
153
|
+
for path in paths:
|
|
154
|
+
body = to_wire(body, path)
|
|
155
|
+
return {**arguments, "body": body}
|
|
156
|
+
|
|
157
|
+
def after(self, operation: Any, result: Any) -> Any:
|
|
158
|
+
del operation
|
|
159
|
+
return from_wire(result)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class Extension:
|
|
163
|
+
"""``cli.extensions`` entry: the ``--doc-format`` option."""
|
|
164
|
+
|
|
165
|
+
def __init__(self, runtime: Any) -> None:
|
|
166
|
+
self.prefix = getattr(runtime, "env_prefix", "JIRA")
|
|
167
|
+
|
|
168
|
+
def add_options(self, group: argparse._ArgumentGroup, hidden: bool) -> None:
|
|
169
|
+
group.add_argument(
|
|
170
|
+
"--doc-format",
|
|
171
|
+
choices=CHOICES,
|
|
172
|
+
default=argparse.SUPPRESS,
|
|
173
|
+
help=argparse.SUPPRESS
|
|
174
|
+
if hidden
|
|
175
|
+
else f"rich text as markdown (default) or raw adf JSON (env {self.prefix}_DOC_FORMAT)",
|
|
176
|
+
)
|
|
177
|
+
group.add_argument(
|
|
178
|
+
"--markdown",
|
|
179
|
+
metavar="TEXT|@FILE|@-",
|
|
180
|
+
default=argparse.SUPPRESS,
|
|
181
|
+
help=argparse.SUPPRESS
|
|
182
|
+
if hidden
|
|
183
|
+
else "issue description, comment or worklog text as Markdown (text, @file, @- for stdin)",
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
def format(self, args: argparse.Namespace) -> str:
|
|
187
|
+
chosen = getattr(args, "doc_format", None) or os.environ.get(f"{self.prefix}_DOC_FORMAT") or "markdown"
|
|
188
|
+
if chosen not in CHOICES:
|
|
189
|
+
raise ValidationError("--doc-format", f"{self.prefix}_DOC_FORMAT must be markdown or adf")
|
|
190
|
+
return chosen
|
|
191
|
+
|
|
192
|
+
def hooks(self, args: argparse.Namespace) -> list[Any]:
|
|
193
|
+
markdown = _read(getattr(args, "markdown", None))
|
|
194
|
+
if self.format(args) != "markdown":
|
|
195
|
+
if markdown is not None:
|
|
196
|
+
raise usage("needs --doc-format markdown")
|
|
197
|
+
return []
|
|
198
|
+
return [MarkdownHook(markdown)]
|
|
199
|
+
|
|
200
|
+
def notes(self) -> list[str]:
|
|
201
|
+
return [
|
|
202
|
+
(
|
|
203
|
+
"Rich text (issue description and environment, comment body, worklog comment) "
|
|
204
|
+
"is Markdown in and out; --markdown TEXT|@file|@- sets an issue description, "
|
|
205
|
+
"comment or worklog; --doc-format adf sends and returns ADF JSON. "
|
|
206
|
+
"Dialect: uvx adflux syntax."
|
|
207
|
+
),
|
|
208
|
+
]
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _read(value: str | None) -> str | None:
|
|
212
|
+
if value is None:
|
|
213
|
+
return None
|
|
214
|
+
if value == "@-":
|
|
215
|
+
import sys
|
|
216
|
+
|
|
217
|
+
try: # UTF-8 like @file, whatever the console's code page
|
|
218
|
+
return sys.stdin.buffer.read().decode("utf-8")
|
|
219
|
+
except UnicodeError as error:
|
|
220
|
+
raise usage(f"stdin is not UTF-8: {error}") from None
|
|
221
|
+
if value.startswith("@"):
|
|
222
|
+
from pathlib import Path
|
|
223
|
+
|
|
224
|
+
try:
|
|
225
|
+
return Path(value[1:]).read_text(encoding="utf-8")
|
|
226
|
+
except (OSError, UnicodeError) as error:
|
|
227
|
+
raise usage(f"cannot read {value[1:]}: {getattr(error, 'strerror', None) or error}") from None
|
|
228
|
+
return value
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def extension(runtime: Any) -> Extension:
|
|
232
|
+
"""Factory named in ``cli.extensions``."""
|
|
233
|
+
return Extension(runtime)
|