0

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.

2
  • Do you mean add Pull Request Labels? Commented Sep 18, 2024 at 1:25
  • Have tried as the sample shared in my answer below? Does it work for you? @anjalib Commented Sep 19, 2024 at 8:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is the sample of Python code shared in my answer above working for you? @anjalib
yes this worked thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.