如何使用Python SDK获取Azure Git Repo中提交中的特定更改行?

0 投票
1 回答
51 浏览
提问于 2025-04-14 17:54

我正在测试一个由人工智能驱动的PR(拉取请求)审核工具。为此,我需要获取PR中的更改内容,然后请AI(GPT-4)来审核这些更改。

因为我使用的是Azure的Git仓库,所以我想Azure DevOps的开发工具包能帮我获取这些更改。不过,到目前为止我看到的功能有限,似乎无法直接比较文件的差异。

举个例子,下面的代码可以让我读取那些有些行被修改的文件内容,但我还没找到方法只获取那些被修改的行:

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from dotenv import load_dotenv
import os

load_dotenv()

personal_access_token = os.getenv('PAT')
organization_url = os.getenv('ORG_URL')

credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

git_client = connection.clients.get_git_client()


project_name = os.getenv('PROJECT_NAME')
repository_id = os.getenv('REPO_NAME') 

# List pull requests
pull_requests = git_client.get_pull_requests(repository_id=repository_id, project=project_name, search_criteria=None)

pr_id = 2  


credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

git_client = connection.clients.get_git_client()

# Fetch the PR
pull_request = git_client.get_pull_request(repository_id, pr_id, project=project_name)

# Fetch commits associated with the PR
commits = git_client.get_pull_request_commits(repository_id, pr_id, project=project_name)

print(f"Pull Request ID: {pull_request.pull_request_id}, Title: {pull_request.title}\nCommits:")

for commit in commits:
    print(f"\nCommit ID: {commit.commit_id}")
    # Fetch changes introduced by this commit
    changes = git_client.get_changes(commit_id=commit.commit_id, repository_id=repository_id, project=project_name)
    for change in changes.changes:
            # Assuming 'change' is a dictionary and it contains an 'item' dictionary that has a 'path' key
        item_path = change.get('item', {}).get('path', 'Unknown path')
        print(f"Change: {item_path}")
        if change['changeType']:
            print(f"Change Type: {change['changeType']}")


    if change['changeType'] in ['edit', 'add']:
        # Retrieve the blob content
        blob_content = git_client.get_blob_content(repository_id=repository_id, sha1=change['item']['objectId'], project=project_name)
        # The content is returned as a byte stream
        content_bytes = b''.join(blob_content)

        # Then, decode the bytes to get a string representation of the content
        content_str = content_bytes.decode('utf-8')

        print(content_str)

有没有办法让我只获取那些被修改的行呢?

1 个回答

0

我没有找到这样的功能。不过,你可以通过一些REST API来实现这个需求。

  1. 首先,运行Commits - Get Changes,通过提交ID获取提交的路径。
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/changes?api-version=7.1-preview.1
  1. 接着,运行Commits - Get,来获取父提交的ID。
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=7.1-preview.1
  1. 然后,运行以下REST API来获取更改的行。

请求头:

POST https://dev.azure.com/{Org name}/_apis/Contribution/HierarchyQuery/project/{Project name}?api-version=7.1-preview.1

请求体:

{
  "contributionIds": [
    "ms.vss-code-web.file-diff-data-provider"
  ],
  "dataProviderContext": {
    "properties": {
      "repositoryId": "{Repo ID}",
      "diffParameters": {
        "includeCharDiffs": true,
        "modifiedPath": "{Commit path}",
        "modifiedVersion": "GC{Commit ID}",
        "originalPath": "{Commit path}",
        "originalVersion": "GC{parents commit ID}",
        "partialDiff": true
      }
    }
  }
}

这里有一个示例供你参考:

响应体:

这里是图片描述

在用户界面中进行比较:

这里是图片描述

你可以从这个帖子中查看详细信息。

撰写回答