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

Python调用JIRA模块循环创建工单遇报错及逻辑问题求助

解决JIRA批量创建工单的两个问题:未定义的jira对象和循环流程优化

首先搞定NameError: name 'jira' is not defined的问题:

  • 你直接调用了jira.create_issue(),但这个jira对象还没有被初始化!得先完成JIRA模块的导入和客户端认证步骤:
    1. 确保你已经安装了jira包:执行pip install jira
    2. 导入JIRA类:from jira import JIRA
    3. 初始化JIRA客户端(用API Token认证更安全,替换成你的服务器地址、用户名和Token):
      jira = JIRA(
          server="https://your-jira-server-url.com",
          basic_auth=("your-email@example.com", "your-jira-api-token")
      )
      

接下来是循环流程的优化,原来的代码只能在第一次创建后问一次,没法持续循环。我们用while循环来实现“创建后询问是否继续”的逻辑:

from jira import JIRA

# 初始化JIRA客户端(替换成你的实际配置)
jira = JIRA(
    server="https://your-jira-server-url.com",
    basic_auth=("your-email@example.com", "your-jira-api-token")
)

project = "My project"
decision = input("Do you want to create a ticket? (Y/N) ").strip().upper()

while decision == 'Y':
    # 构建工单字段字典
    issue_dict = {
        'project': {'key': project},
        'summary': input("Please provide a summary: ").strip(),
        'description': input("Please describe the issue: ").strip(),
        'issuetype': {'name': 'Story'},
        'assignee': {"name": input("Who is this assigned to? ").strip()}
    }
    
    # 创建工单并打印ID
    ticket_id = jira.create_issue(fields=issue_dict)
    print(f"Created successfully, ticket reference: {ticket_id}")
    
    # 询问是否继续创建
    decision = input("Do you want to create another ticket? (Y/N) ").strip().upper()

print("No more tickets will be created.")

代码说明:

  • 用strip().upper()处理用户输入,避免大小写和空格导致的判断错误
  • while循环会持续执行,直到用户输入的不是Y(不区分大小写)
  • 每次循环都会重新收集工单信息,创建新工单

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:41:45