无法通过atlassian-python-api操作Jira Cloud资产,求批量写入方案
Jira Cloud资产批量读写解决方案
问题根源
atlassian-python-api的Jira类并未封装资产(原Insight)相关功能,资产属于Jira Service Management模块,需使用专门的客户端或直接调用官方REST API。
方案1:使用atlassian-python-api的Insight客户端
资产功能在库中以Insight类封装,需单独初始化客户端,而非通过Jira实例调用。
示例代码
from atlassian import Insight # 初始化Insight客户端 insight = Insight( url="https://<your-domain>.atlassian.net", api_token="<your-api-token>", email="<your-atlassian-email>" # 必须填写绑定API token的邮箱 ) # 用Assets JQL搜索资产(空JQL返回全部) assets_result = insight.search_assets(jql="") # 解析并输出资产信息 print("Asset ID\tAsset Type\tName\tDescription") for asset in assets_result.get('objects', []): asset_type = asset['objectType']['name'] name = asset['label'] description = asset.get('description', '') print(f"{asset['id']}\t{asset_type}\t{name}\t{description}") # 批量更新资产示例 # 需提前获取目标字段的objectTypeAttributeId(可通过Insight API或Assets界面查看) update_data = { "objects": [ { "id": 12345, "attributes": [ { "objectTypeAttributeId": 67890, "values": [{"value": "更新后的字段值"}] } ] }, # 添加更多待更新资产... ] } insight.bulk_update_objects(update_data)
注意事项
- 确保安装最新版atlassian-python-api:
pip install --upgrade atlassian-python-api - 账号需拥有Jira Service Management及Assets的访问/编辑权限
方案2:直接调用官方Assets REST API
若库方法无法满足需求,可直接调用官方API实现批量操作。
示例代码(使用requests)
import requests from requests.auth import HTTPBasicAuth BASE_URL = "https://<your-domain>.atlassian.net/rest/servicedeskapi/insight" EMAIL = "<your-atlassian-email>" API_TOKEN = "<your-api-token>" auth = HTTPBasicAuth(EMAIL, API_TOKEN) headers = {"Accept": "application/json"} # 获取资产列表 search_response = requests.get( f"{BASE_URL}/object/search", auth=auth, headers=headers, params={"jql": ""} ) assets = search_response.json() # 批量更新资产 update_payload = { "objects": [ { "id": 12345, "attributes": [ { "objectTypeAttributeId": 67890, "values": [{"value": "批量更新值"}] } ] }, # 更多资产... ] } update_response = requests.put( f"{BASE_URL}/object/bulk", auth=auth, headers={**headers, "Content-Type": "application/json"}, json=update_payload ) if update_response.status_code == 200: print("批量更新成功") else: print(f"更新失败:{update_response.text}")
关键说明
- 字段ID(
objectTypeAttributeId)可通过GET /rest/servicedeskapi/insight/objecttype/{objectTypeId}/attribute接口获取 - 需确保API token拥有对应的Assets操作权限
内容的提问来源于stack exchange,提问作者venkatesh
相关产品推荐
相关产品推荐

