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.
-
Do you mean add Pull Request Labels?Bright Ran– Bright Ran2024-09-18 01:25:00 +00:00Commented Sep 18, 2024 at 1:25
-
Have tried as the sample shared in my answer below? Does it work for you? @anjalibBright Ran– Bright Ran2024-09-19 08:52:24 +00:00Commented Sep 19, 2024 at 8:52
Add a comment
|
1 Answer
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>')
2 Comments
Bright Ran
Is the sample of Python code shared in my answer above working for you? @anjalib
anjalib
yes this worked thank you
