将变量传递给函数以创建 JIRA 的 JSON 正文
Passing variable into function to create JSON body for JIRA
Python代码:
def jira_create(
project,
summary,
description,
components,
issuetype,
labels,
reporter="valid_user_name",
assignee="__unassigned",
epicname=None,
):
jira_url = "valud_jira_url"
headers = {"Content-type": "application/json", "Accept": "application/json"}
body = {
"fields": {
"project": {"key": project},
"summary": summary,
"description": description,
"components": [{"name": components}],
"issuetype": {"name": issuetype}, # EPIC, BUG, STORY, TASK
"labels": [labels],
"reporter": {"name": reporter},
"assignee": {"name": assignee},
}
}
print(body)
jira_request = requests.post(
jira_url + "/rest/api/2/issue/",
json=body,
auth=HTTPBasicAuth(jira_user, jira_pass),
headers=headers,
)
print(jira_request.content)
return
if __name__ == "__main__":
jira_user = "valid_user"
jira_pass = "valid_pw"
project = "Project"
summary = "testMe - Text - More Text"
description = "Here's some text"
components = "Project - Some Text"
issuetype = "Epic"
labels = "text1"
# labels = "text1", "text2"
reporter = "valid_user_name"
jira_create(
project,
summary,
description,
components,
issuetype,
labels,
reporter,
)
这是从 Python 代码创建的 JSON 与 JIRA rest API 一起使用的代码 API 2:
{
"fields": {
"project": {
"key": "Project"
},
"summary": "testMe - Text - More Text",
"description": "Here's some text",
"components": [
{
"name": "Project - Some Text"
}
],
"issuetype": {
"name": "Epic"
},
"labels": [
"text1",
"text2"
],
"reporter": {
"name": "valid_user_name"
},
"assignee": {
"name": "__unassigned"
},
}
}
在上述元素 'labels' 的示例 JSON 代码中,当 Python 变量是只有一项的字符串时,它会正确传递并创建 JIRA。
例如在 python 代码中,当 labels = "text1"
时,一切正常 (class 'str')
但是,如果传递字符串 labels = "text1", "text2"
中的两个元素,这就是它失败的地方 (class 'tuple')。
我试过将项目作为元组、列表和集合传递,但运气不佳。
JSON 正文需要的是:
'labels' = ["text1", "text2"]
输出结果如下:
'labels': [('text1', 'text2')]
(class 'tuple') 注意额外的括号
结果:
"errors": {
"labels": "string expected at index 0"
}
'labels': [['text1', 'text2']]
(class 'list') 注意额外的括号
结果:
"errors": {
"labels": "string expected at index 0"
}
'labels': [{'text1', 'text2'}]
(class 'set') 注意额外的大括号
结果:
TypeError: Object of type set is not JSON serializable
我试过转换标签但没有成功:
labels = str(labels)[1:-1] > results in "contains spaces which is invalid"
labels = ", ".join(labels) > results in "contains spaces which is invalid"
labels = ", ".join(map(str, ("text1", "text2"))) > results in "contains spaces which is invalid"
labels = json.dumps(labels_multiple).replace("'[", "").replace("]'", "")
有没有办法解决这个问题?
如果我没理解错的话,你可以这样做:
def jira_create(labels):
if isinstance(labels, str):
labels = [labels]
body = {"labels": [*labels]}
return body
body = jira_create("text1")
print(body)
body = jira_create(("text1", "text2"))
print(body)
""" Prints:
{'labels': ['text1']}
{'labels': ['text1', 'text2']}
"""
Python代码:
def jira_create(
project,
summary,
description,
components,
issuetype,
labels,
reporter="valid_user_name",
assignee="__unassigned",
epicname=None,
):
jira_url = "valud_jira_url"
headers = {"Content-type": "application/json", "Accept": "application/json"}
body = {
"fields": {
"project": {"key": project},
"summary": summary,
"description": description,
"components": [{"name": components}],
"issuetype": {"name": issuetype}, # EPIC, BUG, STORY, TASK
"labels": [labels],
"reporter": {"name": reporter},
"assignee": {"name": assignee},
}
}
print(body)
jira_request = requests.post(
jira_url + "/rest/api/2/issue/",
json=body,
auth=HTTPBasicAuth(jira_user, jira_pass),
headers=headers,
)
print(jira_request.content)
return
if __name__ == "__main__":
jira_user = "valid_user"
jira_pass = "valid_pw"
project = "Project"
summary = "testMe - Text - More Text"
description = "Here's some text"
components = "Project - Some Text"
issuetype = "Epic"
labels = "text1"
# labels = "text1", "text2"
reporter = "valid_user_name"
jira_create(
project,
summary,
description,
components,
issuetype,
labels,
reporter,
)
这是从 Python 代码创建的 JSON 与 JIRA rest API 一起使用的代码 API 2:
{
"fields": {
"project": {
"key": "Project"
},
"summary": "testMe - Text - More Text",
"description": "Here's some text",
"components": [
{
"name": "Project - Some Text"
}
],
"issuetype": {
"name": "Epic"
},
"labels": [
"text1",
"text2"
],
"reporter": {
"name": "valid_user_name"
},
"assignee": {
"name": "__unassigned"
},
}
}
在上述元素 'labels' 的示例 JSON 代码中,当 Python 变量是只有一项的字符串时,它会正确传递并创建 JIRA。
例如在 python 代码中,当 labels = "text1"
时,一切正常 (class 'str')
但是,如果传递字符串 labels = "text1", "text2"
中的两个元素,这就是它失败的地方 (class 'tuple')。
我试过将项目作为元组、列表和集合传递,但运气不佳。
JSON 正文需要的是:
'labels' = ["text1", "text2"]
输出结果如下:
'labels': [('text1', 'text2')]
(class 'tuple') 注意额外的括号
结果:
"errors": {
"labels": "string expected at index 0"
}
'labels': [['text1', 'text2']]
(class 'list') 注意额外的括号
结果:
"errors": {
"labels": "string expected at index 0"
}
'labels': [{'text1', 'text2'}]
(class 'set') 注意额外的大括号
结果:
TypeError: Object of type set is not JSON serializable
我试过转换标签但没有成功:
labels = str(labels)[1:-1] > results in "contains spaces which is invalid"
labels = ", ".join(labels) > results in "contains spaces which is invalid"
labels = ", ".join(map(str, ("text1", "text2"))) > results in "contains spaces which is invalid"
labels = json.dumps(labels_multiple).replace("'[", "").replace("]'", "")
有没有办法解决这个问题?
如果我没理解错的话,你可以这样做:
def jira_create(labels):
if isinstance(labels, str):
labels = [labels]
body = {"labels": [*labels]}
return body
body = jira_create("text1")
print(body)
body = jira_create(("text1", "text2"))
print(body)
""" Prints:
{'labels': ['text1']}
{'labels': ['text1', 'text2']}
"""