在任何地方都找不到直接的方法。如何通过 Python 中的 Azure Devops API 将注释添加到拉取请求?
可以使用 Azure DevOps Python API 将注释添加到拉取请求。以下是执行此操作的方法:
1. 安装 Azure DevOps 库:
pip install azure-devops
2. 导入必要的类并对的组织和项目进行身份验证:
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
# Fill in with your personal access token and organization URL
personal_access_token = 'YOUR_PERSONAL_ACCESS_TOKEN'
organization_url = 'https://dev.azure.com/YOUR_ORGANIZATION'
# Create a connection to the organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
3. 获取 Git 客户端并检索拉取请求:
# Get the Git client
git_client = connection.clients.get_git_client()
# Specify the project ID or name and pull request ID
project_name = 'YOUR_PROJECT_NAME'
pull_request_id = YOUR_PULL_REQUEST_ID
# Get the pull request
pull_request = git_client.get_pull_request(project_name, pull_request_id)
4. 创建一个线程并添加的注释:
# Create a thread for the comment
thread = git_client.create_thread(
comment_thread={
'comments': [{'content': 'This is my comment!'}],
'status': 'active'
},
repository_id=pull_request.repository.id,
pull_request_id=pull_request.pull_request_id,
project=project_name
)
这将在拉取请求中创建一个新线程,并在其中添加的注释。
完整脚本:
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
# Fill in with your personal access token and organization URL
personal_access_token = 'YOUR_PERSONAL_ACCESS_TOKEN'
organization_url = 'https://dev.azure.com/YOUR_ORGANIZATION'
# Create a connection to the organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get the Git client
git_client = connection.clients.get_git_client()
# Specify the project ID or name and pull request ID
project_name = 'YOUR_PROJECT_NAME'
pull_request_id = YOUR_PULL_REQUEST_ID
# Get the pull request
pull_request = git_client.get_pull_request(project_name, pull_request_id)
# Create a thread for the comment
thread = git_client.create_thread(
comment_thread={
'comments': [{'content': 'This is my comment!'}],
'status': 'active'
},
repository_id=pull_request.repository.id,
pull_request_id=pull_request.pull_request_id,
project=project_name
)
print(f"Comment added to pull request: {pull_request.title}")
注意: * 将占位符替换为的实际个人访问令牌、组织 URL、项目名称和拉取请求 ID。 * 确保的个人访问令牌具有访问代码和创建注释的足够权限。
标签:python,git,azure-devops,ado,pull-request From: 78802146