You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python和REST API操作Jira Insight对象架构的对象与属性

Jira Insight REST API 核心操作实现(Python)

下面是针对你需求的具体实现代码,基于Insight 1.0 API:

1. 获取指定Object Schema下的所有对象列表

你之前的代码仅能获取Schema列表,要拿到Schema内的对象,需先提取目标Schema的ID,再调用对象列表接口:

import requests

# 配置基础参数
auth = ("你的用户名", "你的密码")
certificates = ("证书文件路径", "密钥文件路径")  # 无证书可移除cert参数
headers = {"Accept": "application/json", "Content-Type": "application/json"}
base_url = "https://insight-api.riada.io/rest/insight/1.0"

# 1. 获取Object Schema列表,提取目标Schema的ID
schema_list_url = f"{base_url}/objectschema/list"
schema_response = requests.get(schema_list_url, headers=headers, auth=auth, cert=certificates)
schemas = schema_response.json()
# 可根据Schema名称筛选,示例取第一个Schema
target_schema_id = schemas[0]["id"]

# 2. 获取该Schema下的所有对象(支持分页,示例用pageSize拉取全量)
objects_list_url = f"{base_url}/object/list"
params = {"schemaId": target_schema_id, "pageSize": 1000}
objects_response = requests.get(objects_list_url, headers=headers, auth=auth, cert=certificates, params=params)
all_objects = objects_response.json()
print("Schema内所有对象:", all_objects)

注意:如果对象数量超过pageSize,需要循环调整startAt参数实现分页拉取。

2. 在Object Schema内创建带属性的对象

创建对象需要先确定目标对象类型(Object Type)的ID,再构造包含属性的请求体:

# 1. 获取目标Schema下的对象类型列表,提取目标类型ID
object_types_url = f"{base_url}/objectschema/{target_schema_id}/objecttype"
object_types_response = requests.get(object_types_url, headers=headers, auth=auth, cert=certificates)
object_types = object_types_response.json()
# 示例取第一个对象类型
target_object_type_id = object_types[0]["id"]

# 2. 构造创建请求体,属性需匹配对象类型的配置
create_object_url = f"{base_url}/object/create"
payload = {
    "objectTypeId": target_object_type_id,
    "attributes": [
        {
            "attributeKey": "name",  # 替换为你的属性键(可在Insight界面属性配置中查看)
            "objectAttributeValues": [{"value": "测试对象001"}]
        },
        {
            "attributeKey": "asset_tag",
            "objectAttributeValues": [{"value": "TAG-2024-0001"}]
        }
    ]
}

create_response = requests.post(create_object_url, headers=headers, auth=auth, cert=certificates, json=payload)
if create_response.status_code == 200:
    print("对象创建成功:", create_response.json())
else:
    print("创建失败:", create_response.json())

注意:attributeKey可通过Insight界面的属性详情页查看,或调用/objecttype/{objectTypeId}/attribute接口获取属性的详细信息。

3. 获取指定对象的属性

通过对象ID调用详情接口即可获取完整属性信息:

# 从之前的对象列表中取目标对象ID,或直接指定已知ID
target_object_id = all_objects[0]["id"]

# 获取对象详情及属性
object_details_url = f"{base_url}/object/{target_object_id}"
details_response = requests.get(object_details_url, headers=headers, auth=auth, cert=certificates)
object_details = details_response.json()

# 提取并打印属性
print("\n对象属性详情:")
for attr in object_details["attributes"]:
    attr_name = attr["name"]
    attr_values = [val["value"] for val in attr["objectAttributeValues"]]
    print(f"{attr_name}: {', '.join(attr_values)}")

内容的提问来源于stack exchange,提问作者karthik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 19:30:38