306581e70b
Jira Cloud and On-Prem/Data Center (27 commands): JQL ingestion with an OCSF mapper and JQL query, full issue lifecycle (create/get/edit/delete, transitions, assign), comments, remote web links and issue links, attachments (upload/download/delete), field and user lookups, and agile boards/sprints/epics. Basic (email + API token) or Personal Access Token authentication; API v3 + ADF bodies on Cloud, v2 on On-Prem. Stdlib-only, no extra Python dependencies. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
105 lines
3.5 KiB
Python
105 lines
3.5 KiB
Python
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
|
|
|
|
|
def _cfg():
|
|
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
|
|
|
|
|
def _is_cloud():
|
|
return bool(str(_cfg().get("cloud_id") or "").strip())
|
|
|
|
|
|
def _api():
|
|
return "3" if _is_cloud() else "2"
|
|
|
|
|
|
def _base():
|
|
cfg = _cfg()
|
|
root = str(cfg.get("server_url") or "https://api.atlassian.com/ex/jira").rstrip("/")
|
|
cloud_id = str(cfg.get("cloud_id") or "").strip()
|
|
return root + "/" + cloud_id if cloud_id else root
|
|
|
|
|
|
def _auth():
|
|
cfg = _cfg()
|
|
pat = str(cfg.get("pat") or "")
|
|
if pat:
|
|
return "Bearer " + pat
|
|
cred = (str(cfg.get("username") or "") + ":" + str(cfg.get("api_key") or "")).encode("utf-8")
|
|
return "Basic " + base64.b64encode(cred).decode("ascii")
|
|
|
|
|
|
def request(method, path, params=None, body=None, full_url=None, raw=False):
|
|
url = full_url or (_base() + path)
|
|
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
|
if q:
|
|
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
|
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
headers = {"Accept": "application/json", "Authorization": _auth()}
|
|
if data is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
with urllib.request.urlopen(req, timeout=90) as r:
|
|
content = r.read()
|
|
if raw:
|
|
return content
|
|
return json.loads(content) if content else {}
|
|
|
|
|
|
def main():
|
|
args = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
|
attribute = str(args.get("attribute") or "").strip()
|
|
if not attribute:
|
|
raise Exception("attribute is required")
|
|
max_results = int(args.get("max_results") or 50)
|
|
|
|
cloud = _is_cloud()
|
|
if cloud:
|
|
res = request("GET", "/rest/api/3/user/search",
|
|
params={"query": attribute, "maxResults": max_results})
|
|
else:
|
|
res = request("GET", "/rest/api/2/user/search",
|
|
params={"username": attribute, "maxResults": max_results})
|
|
|
|
users = res if isinstance(res, list) else (res.get("values") or [])
|
|
|
|
if len(users) == 1:
|
|
u = users[0]
|
|
account_id = u.get("accountId") or u.get("name") or ""
|
|
print(json.dumps({"attribute": attribute, "account_id": account_id}))
|
|
return
|
|
|
|
needle = attribute.lower()
|
|
candidates = []
|
|
for u in users:
|
|
values = [str(u.get("displayName") or ""), str(u.get("emailAddress") or "")]
|
|
if not cloud:
|
|
values.append(str(u.get("name") or ""))
|
|
if any(v.lower() == needle for v in values if v):
|
|
candidates.append(u.get("accountId") or "" if cloud else u.get("name") or "")
|
|
|
|
if len(candidates) == 1:
|
|
print(json.dumps({"attribute": attribute, "account_id": candidates[0]}))
|
|
elif len(candidates) == 0:
|
|
print(json.dumps({
|
|
"attribute": attribute,
|
|
"account_id": "",
|
|
"message": "no unambiguous match — try the exact display name or email",
|
|
}))
|
|
else:
|
|
print(json.dumps({
|
|
"attribute": attribute,
|
|
"account_id": "",
|
|
"message": "multiple matches — provide a more specific attribute",
|
|
}))
|
|
|
|
|
|
try:
|
|
main()
|
|
except urllib.error.HTTPError as e:
|
|
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(json.dumps({"error": str(e)}))
|
|
sys.exit(1)
|