0

I am parsing pull requests one by one with azure.devops python library

for each PR I need the related branch and the commits

is there a way to do this ?

repositories = git_client.get_repositories(project=None, include_links=None, include_all_urls=None, include_hidden=None)
for repo in repositories:
    if repo.remote_url == repo_url:
        pull_requests = git_client.get_pull_requests(repository_id=repo.id,search_criteria=GitPullRequestSearchCriteria(status='completed'))
        for pr in pull_requests:
            print(f'Pull Request #{pr.pull_request_id}: {pr.title}')

been looking at the doc, I cannot find anything related to this

thanks for your help

1 Answer 1

1

You need to install azure-devops package via command pip install azure-devops.

Then use python script to get the info needed. My python script below:

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_0.git.git_client import GitClient
from azure.devops.v7_0.git.models import GitPullRequestSearchCriteria

# Replace with your Azure DevOps organization URL and PAT
organization_url = "https://dev.azure.com/{orgname}"
personal_access_token = "PAT"

# Create a connection
credentials = BasicAuthentication("", personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get the Git client
git_client = connection.clients.get_git_client()

# Replace with your repository URL
repo_url = "https://{orgname}@dev.azure.com/{orgname}/{projectname}/_git/{reponame}"

# Get all repositories
repositories = git_client.get_repositories(project=None, include_links=None, include_all_urls=None, include_hidden=None)
for repo in repositories:
   print(f"testreporemoteurl .......{repo.remote_url}")   # used to check the remote url.

for repo in repositories:
    if repo.remote_url == repo_url:
        pull_requests = git_client.get_pull_requests(repository_id=repo.id, search_criteria=GitPullRequestSearchCriteria(status='Completed'))
        for pr in pull_requests:
            print(f"Pull Request #{pr.pull_request_id}: {pr.title}")
            print(f"Source Branch: {pr.source_ref_name}")
            print(f"Target Branch: {pr.target_ref_name}")
            print(f" - {pr}")     # check the whole PR content
            print("Commits:")
            print(f" - {pr.last_merge_source_commit.commit_id}")   # get the last merget source commit id
            print("\n")

Please replace orgname, pat, projectname, reponame to yours.

The output as below. I output the PR ID, title, source branch, target branch. I also output the whole {pr} content. For commits, it's none. Hence, i output the last merget source commit id. You can parse the {pr} content to get the info you need.

enter image description here

1
  • 1
    @pf12345678910, you can parse the {pr} content to get the value needed, for example, {pr.pull_request_id}..etc, glad to help if you have any queries. Commented Mar 26 at 12:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.