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

Python项目中Azure Boards工作流状态设置用户查询异常求助

问题分析与解决方案

问题原因

你的代码存在两个核心问题:

  1. 未处理字段不存在的情况:直接访问update['fields']['System.Tags']['newValue'],但并非所有修改System.State的记录都会附带System.Tags的更新,这会触发KeyError,导致程序跳过后续判断逻辑,最终返回空值。
  2. 逻辑分支设计不合理:嵌套的if-elif结构没有清晰区分「带MYVALUE标签」和「不带MYVALUE标签」的场景,导致无标签的合法状态更新无法被正确识别。

修正后的代码

def find_user(updates):
    # 定义允许的状态集合,提升判断效率和可读性
    allowed_states = {'New', 'Approved', 'Committed', 'Resolved'}
    # 带MYVALUE标签时需要排除的状态
    excluded_state = {'New'}
    
    for update in updates:
        # 跳过非System.State更新的记录
        if 'fields' not in update or 'System.State' not in update['fields']:
            continue
        
        current_state = update['fields']['System.State']['newValue']
        # 检查是否包含MYVALUE标签,默认不包含
        has_myvalue_tag = False
        if 'System.Tags' in update['fields']:
            has_myvalue_tag = update['fields']['System.Tags']['newValue'] == 'MYVALUE'
        
        # 根据标签情况判断是否符合条件
        if has_myvalue_tag:
            if current_state in allowed_states and current_state not in excluded_state:
                return update['revisedBy']['uniqueName'], update['revisedBy']['displayName']
        else:
            if current_state in allowed_states:
                return update['revisedBy']['uniqueName'], update['revisedBy']['displayName']
    
    # 未找到符合条件的记录时返回空值
    return '', ''

关键修正点

  • 字段存在性检查:先判断System.Tags是否存在于更新字段中,避免触发KeyError。
  • 场景拆分:明确区分「带MYVALUE标签」和「不带标签」的逻辑分支,严格匹配需求:
    • 带MYVALUE标签:只返回将状态设置为Approved/Committed/Resolved的用户
    • 不带标签:返回将状态设置为New/Approved/Committed/Resolved的用户
  • 集合优化:使用集合存储状态值,让条件判断更简洁高效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 04:50:43