Вот что я хочу сделать: каждый раз, когда в моем репозитории происходит фиксация определенной ветки, я хочу извлечь все файлы YAML, измененные в этой фиксации, внести некоторые изменения в их содержимое, а затем отправить результат в виде PR в новая, отдельная ветка. Я разобрался с большинством шагов здесь, но застрял на части содержимого файла фиксации синтаксического анализа. Я попробовал get_item_content
и get_blob_content
при коммите, но не увидел ни одного загружаемого файла. Поэтому я конкретно хочу знать:
Да, вы можете получить измененные файлы из коммита (НЕ PR) и проанализировать их. Пожалуйста, проверьте пример кода ниже:
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_0.git.models import GitVersionDescriptor
# Set up connection
personal_access_token = 'PAT'
organization_url = 'https://dev.azure.com/ORGNAME'
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get the Git client
git_client = connection.clients.get_git_client()
repository_id = 'REPOID'
commit_id = 'COMMITID' # it's the 40-character string commit id
# Get the changes for the commit
changes = git_client.get_changes(commit_id, repository_id)
changed_files = [change['item']['path'] for change in changes.changes]
# Filter for YAML files
yaml_files = [file for file in changed_files if file.endswith('.yaml') or file.endswith('.yml')]
print(yaml_files)
# Get the contents of each YAML file
for file_path in yaml_files:
version_descriptor = GitVersionDescriptor(version=commit_id, version_type='commit')
item_content = git_client.get_item_content(repository_id, path=file_path, version_descriptor=version_descriptor)
# Read the content from the generator
content = b''.join(item_content).decode('utf-8')
print(f"Contents of {file_path}:\n{content}")
Он распечатывает измененный файл yaml и его содержимое, как показано ниже: