Python Jira API受让人未使用“填充”;名称“;不再需要,而是需要”;Accounded“;

Python Jira API受让人未使用“填充”;名称“;不再需要,而是需要”;Accounded“;,python,python-3.x,api,jira,Python,Python 3.x,Api,Jira,我正在使用Python3.x 以前,我有一个创建票证的函数,如下所示 def jira_incident(jira_subject, jira_description): user = "username" apikey = 'apikey' server = 'https://serverName.atlassian.net' options = { 'server': server, 'verify': False }

我正在使用Python3.x

以前,我有一个创建票证的函数,如下所示

def jira_incident(jira_subject, jira_description):
    user = "username"
    apikey = 'apikey'
    server = 'https://serverName.atlassian.net'
    options = {
        'server': server,
        'verify': False
    }
    issue_dict = {
        'project': {'key': 'project_name'},
        'summary': str(jira_subject),
        'description': str(jira_description),
        'issuetype': {'name': 'Incident'},
        'assignee': {'name': my_username},
        'priority': {'name': 'Low'},
        'customfield_10125':
            {'value': 'Application Ops'}
    }
    jira = JIRA(options, basic_auth=(user, apikey))
    new_issue = jira.create_issue(fields=issue_dict)
    return new_issue
my_username是一个全局变量,也用于其他事情

无论如何,从大约2天前开始,受让人不再工作。我在谷歌上搜索了一下,发现它现在需要的是accountId而不是名称,我可以通过web用户界面在评论中留下评论,称之为某人。作为一个临时解决方案,我已经填充了一个字典以供参考(这是可行的),但是我想让它更具动态性,以便将来验证脚本

'assignee': {'accountId': jira_dict[my_username]},
我似乎找不到任何关于从名称中查找accountId的文档,我想我应该继续问社区,看看是否有其他人遇到/解决了这个问题

我正在考虑编写一个新函数,为我执行这个查询,然后返回accountId

编辑 我确实发现:

import requests
from requests.auth import HTTPBasicAuth
import json

url = "/rest/api/3/user/bulk/migration"

auth = HTTPBasicAuth("email@example.com", "<api_token>")

headers = {
   "Accept": "application/json"
}

response = requests.request(
   "GET",
   url,
   headers=headers,
   auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
导入请求
从requests.auth导入HTTPBasicAuth
导入json
url=“/rest/api/3/user/bulk/migration”
auth=HTTPBasicAuth(“email@example.com", "")
标题={
“接受”:“应用程序/json”
}
response=requests.request(
“得到”,
网址,
标题=标题,
auth=auth
)
打印(json.dumps(json.loads(response.text),sort_keys=True,indent=4,分隔符=(“,”,“:”)

然而,404在我身上,我将服务器地址添加到url的开头,并将用户替换为相关用户名。

好的,我找到了一个解决方案,这不是一个优雅的解决方案,但它正是我所需要的。下面是新函数:

def jira_account_id_from_username(username):
    r = requests.get('https://serverName.atlassian.net/rest/api/3/user?username=' + username, auth=("username",api_key), verify=False)
    value = re.search('"accountId":"(.*?)",', str(r.text)).group(1)
    return value

我强烈建议您不要再依赖
用户名
。您使用的端点已弃用,另请参见

“新的”或可能更好的方法是使用此处所述的
/user/search
端点:您可以在这里定义一个
查询
,该查询与用户的某些属性相匹配(
displayName
emailAddress
),或者搜索
accountId
,如果您已经拥有它的话。因此,如果您正在将用户从云链接到其他一些“用户目录”(或只是存储了一些用户名的脚本),请使用电子邮件地址或accountId替换它,以便您可以正确链接用户。

url=”https://server.atlassian.net/rest/api/3/user/search“
auth=HTTPBasicAuth(email\u address,api\u key)
query={/code>`username':username,`emailAddress':emailAddress`
response=requests.request(
`GET',`url,`params=query,`auth=auth`
value=re.search(''accountId:'(.*?)、str(response.text)).group(1)
这返回的值与我上面发布的值相同,但使用第二个链接中的信息,这看起来正确吗?我不喜欢拆分str以获得我想要的内容。