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

如何通过Python调用Azure DevOps API获取PR中文件的差异片段而非完整文件内容

如何通过Python调用Azure DevOps API获取PR中文件的差异片段而非完整文件内容

你好!我看了你的代码,现在的问题是你通过get_item_content获取的是文件的完整内容,而要拿到PR里和主分支对比的差异片段,其实Azure DevOps的Git API已经在拉取请求迭代变更的接口里提供了diff信息,不需要再单独拉取整个文件。另外我还发现你代码里有个小bug:循环里的change_type取的是第一个变更条目的类型,这会导致所有文件的变更类型都和第一个文件一致,这显然不对,得改成当前循环项的变更类型。

下面是修改后的完整代码,我会在关键部分加注释说明:

def fetch_pr_diff(pr_id):
    try:
        # Establish connection and get the Git client
        connection = get_azure_devops_connection()
        git_client = connection.clients.get_git_client()
        
        # Retrieve pull request iterations
        iterations = git_client.get_pull_request_iterations(
            repository_id=repository_id,
            project=project_name,
            pull_request_id=pr_id
        )
        
        # Get the latest iteration
        latest_iteration = max(iterations, key=lambda x: x.id)
        
        # Fetch changes for the latest iteration
        iteration_changes = git_client.get_pull_request_iteration_changes(
            repository_id=repository_id,
            project=project_name,
            pull_request_id=pr_id,
            iteration_id=latest_iteration.id
        )
        
        # Initialize the diff content
        diff_content = ""
        
        # Loop through the changes to extract diffs for each file
        for change_entry in iteration_changes.change_entries:
            # Get the additional properties for current change entry
            change_props = change_entry.additional_properties
            
            # Skip entries without valid file information
            if 'item' not in change_props or change_props['item'] is None:
                continue
                
            change_item = change_props['item']
            file_path = change_item['path']
            # 修正:获取当前文件的变更类型,而不是第一个文件的
            change_type = change_props.get('changeType', 'unknown')
            
            # Initialize file diff info
            file_diff = f"File: {file_path}\n"
            file_diff += f"Change Type: {change_type}\n"
            
            # Process different change types
            if change_type in ['edit', 'add']:
                # 从当前变更条目中直接获取diff片段,这就是和base版本的差异
                file_diff_content = change_props.get('diff', 'No diff content available')
                file_diff += f"Diff Content:\n{file_diff_content}\n\n"
            elif change_type == 'delete':
                file_diff += "Content: File deleted.\n\n"
            else:
                # 处理其他变更类型(比如重命名等)
                file_diff += "Content: Unsupported change type.\n\n"
                
            # Append to total diff content
            diff_content += file_diff

        return diff_content
    except Exception as e:
        print(f"Error fetching PR diff: {str(e)}")
        return ""

关键修改点说明:

  1. 修正变更类型的获取:原来的代码错误地复用了第一个变更条目的类型,现在改为从当前循环的change_entry中获取changeType,确保每个文件的变更类型是正确的。
  2. 直接提取diff片段:不再调用get_item_content拉取完整文件,而是直接从change_entry的additional_properties['diff']中获取该文件和base版本的差异内容,这正是你需要的修改部分。
  3. 优化空值判断:增加了对item字段存在性的判断,避免KeyError。
  4. 增加异常捕获:添加了全局异常捕获,方便调试错误。
  5. 变量名优化:把循环变量改为change_entry,用change_item指代文件条目,避免原代码中变量覆盖的问题。

这样修改后,你就能得到每个文件的具体差异片段,而不是完整的文件内容了。

备注:内容来源于stack exchange,提问作者Asaf Ben Shabat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 16:29:37