Basically, same as the title. How can I use the Azure Devops API in Python to fetch all modified files in a PR? I have found code references for C#, but not Python, to do this specifically.
Have gone through the REST API reference and PyPi package code, but the Python package documentation is not super helpful.
You can use the get_pull_request_commits to get target commit and base commit of the pull request, then use the get_commit_diffs to get the changed files between the two commits.
The following is the sample python script.
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_0.git.models import GitBaseVersionDescriptor, GitTargetVersionDescriptor
# Fill in with your personal information
personal_access_token = ''
organization_url = 'https://dev.azure.com/orgname'
project = 'projectname'
repository_name = 'testrepo'
pull_request_id = '67'
# Create a connection
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
git_client = connection.clients.get_git_client()
# Get the commits for the pull request
commits = git_client.get_pull_request_commits(project=project, repository_id=repository_name, pull_request_id=pull_request_id)
# Get the target commit ID
target_commit_id = commits[0].commit_id
# Get the base commit ID
base_commit_id = commits[-1].commit_id
# Create GitBaseVersionDescriptor and GitTargetVersionDescriptor objects
base_version_descriptor = GitBaseVersionDescriptor(base_version=base_commit_id, base_version_type="commit")
target_version_descriptor = GitTargetVersionDescriptor(target_version=target_commit_id, target_version_type ="commit")
# Get the commit diffs
commit_diffs = git_client.get_commit_diffs(repository_id=repository_name, project=project, base_version_descriptor=base_version_descriptor, target_version_descriptor=target_version_descriptor)
# Get the list of changed files
for change in commit_diffs.changes:
print(change['item']['path'])