python jira 读取表数据批量新建子任务

小李在Jira中处理任务时,发现一个表格数据很有趣。他决定为每一行数据创建一个新的子任务。他复制粘贴,忙得不亦乐乎。同事小张路过,好奇地问:"你在做什么?"小李得意地回答:"我在批量新建子任务,让这些数据都活起来!"小张瞪大眼睛:"你确定这不是在玩'俄罗斯方块'?"

于是,有了它~

python 复制代码
# encoding=utf-8

from jira import JIRA
from openpyxl import load_workbook
import os, pytest, traceback

jira_server = "xxxxxx"
jira_username = "xxxxx"
jira_password = "xxxxx"
file_name = 'xxxxx'
sheet_name = 'xxxx'

#获取jira消息
def jira_info(jira_server, jira_username, jira_password):
    jira = JIRA(server=jira_server, basic_auth=(jira_username, jira_password))
    return jira

#读取表数据
def get_TestExcel(file_name, sheet_name):
    print("======", os.getcwd())
    workbook = load_workbook(file_name)
    sheet = workbook[sheet_name]
    test_data = []
    for i in range(2, sheet.max_row + 1):
        sub_data = {}
        for j in range(1, sheet.max_column + 1):
            sub_data[sheet.cell(1, j).value] = sheet.cell(i, j).value
        test_data.append(sub_data)
    return test_data


case_data = get_TestExcel(file_name, sheet_name)

idss = ["标题: {} ".format(data["标题"]) for data in case_data]

#根据读取数据新建任务
@pytest.mark.parametrize("test_data", case_data, ids=idss)
def test_create_task(test_data):
    project_key = test_data['项目key']
    summary = test_data['标题']
    description = test_data['描述']
    issuetype = test_data['类型id']
    assignee = test_data['指向人']
    parent = test_data['挂靠需求key']
    test_environment_value = test_data['测试环境id']
    system_module_value = test_data['信增系统模块id']
    print('project_key:%s,summary:%s,description:%s,issuetype:%s,assignee:%s,parent:%s,test_env:%s,sys_module:%s' % (
    project_key, summary, description, issuetype, assignee, parent, test_environment_value, system_module_value))
    try:
        new_issue = jira_info(jira_server, jira_username, jira_password).create_issue(
            project={'key': str(project_key)},
            summary=str(summary),
            description=str(description),
            issuetype={'id': int(issuetype)},
            assignee={'name': str(assignee)},
            parent={'key': str(parent)},
            customfield_10265={"id": str(test_environment_value)},
            customfield_10268={"id": str(system_module_value)},
        )
        print(f"新建的任务已创建,其ID为:{new_issue.key}")
    except Exception as e:
        print('异常: %s' % e)

    finally:

        print("Operation completed. Check the error log if any issues.")