anjalib
anjalib

Reputation: 41

Using Azure Devops Python API to create PR and add tags to it in the same call

So I want to create pull requests using the POST request endpoint, but want to add a tag to the PR along with it. I know there is a separate endpoint to add a tag to a PR, but was wondering if these can be done in the same call. Right now my PR creation request body contains source ref, target ref, title and description.

Upvotes: 0

Views: 191

Answers (1)

Bright Ran-MSFT
Bright Ran-MSFT

Reputation: 13944

When calling the API to create a new PR, you can add tags/labels to the PR via the request body of the API.

Below is sample of using the Azure DevOps Python client libraries to add tags/labels when creating a new PR.

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication

# Fill in with your PAT and organization URL.
personal_access_token = '<PAT>'
organization_url = 'https://dev.azure.com/<organization-name>'

# 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()

# Set the request body of the API call. Add 2 labels when creating the new PR.
body = {
    "title": "Merge changes - 2024091801",
    "description": "Merge changes - 2024091801\nCreate PR using Azure DevOps Python client libraries.",
    "isDraft": False,
    "sourceRefName": "refs/heads/dev",
    "targetRefName": "refs/heads/main",
    "labels": [
        {
            "name": "lable01"
        },
        {
            "name": "lable02"
        }
    ]
}

# Call the API to create a new PR with the request boby.
git_client.create_pull_request(body, '<repository-name>', '<project-name>')

enter image description here

Upvotes: 1

Related Questions