0
votes

I'm trying to automate some post-release processes for creating Pull Requests (release branch -> master) after a release. While I'm able to automate the PR creation, and it links the commits, it does not link the work items.

Note: I'm using Python for this, using the (officially supported) azure-devops module from PyPi (https://pypi.org/project/azure-devops/). It is a thin wrapper around the REST API, and I've read the REST API documentation itself to see if there are any other options (haven't found any so far). Here's my code:

def create_pull_request(self, repo_id, source_branch, target_branch, title, description):

    pull_request = {
        "title": title,
        "description": description,
        "sourceRefName": "refs/heads/" + source_branch,
        "targetRefName": "refs/heads/" + target_branch,
    }
    response = self._git_client.create_pull_request(pull_request, repository_id=repo_id)

Here's my function for connection to the git client:

def __init__(self, personal_access_token=None, organization_url=None):
    # Create a connection to the org
    credentials = BasicAuthentication('', personal_access_token)
    self._connection = azure.devops.connection.Connection(base_url=organization_url,
                                                         creds=credentials)

    # Get a client (the "core" client provides access to projects, teams, etc)
    self._git_client = self._connection.clients.get_git_client()

I've also tried pulling the individual commits to see if I can find the work items associated with them and attached them to the pull request after creation, but those responses are coming back with the work items empty.

Is there some other way I can accomplish this?

2

2 Answers

0
votes

A couple of ideas to get your work items linked:

First, your pull_request should probably include:

    pull_request = {
        "title": title,
        "description": description,
        "sourceRefName": "refs/heads/" + source_branch,
        "targetRefName": "refs/heads/" + target_branch,
        "workItemRefs": # collection of work item refs
    }

workItemRefs is documented in the REST API docs at https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/create?view=azure-devops-rest-6.0 . What you need to put in there is probably a matter of some experimentation.

Second, as an alternative, your commits could be created in the form:

git commit -m "My commit message #workitemid1 #workitemid2, etc."

Then, when the PR is created, it will automatically link those work items in the commits it finds in the PR.

0
votes

We cannot add work item link via the Pull Request Create API and Pull Request Update API. I found a feature request, you could follow the ticket to get the latest news.

As a work item, we could link work item to pull request via this REST API Work Items - Update

Steps:

Get pull request field artifactId via below API:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=6.0

Link work item to Pull Request:

Request URL:

PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{work item ID}?api-version=5.1

Request Body:

[ {
  "op": "add", 
  "path": "/relations/-",
  "value": {
    "rel": "ArtifactLink",
    "url": "{This url is artifactId}",
    "attributes": { 
        "name": "pull request" 
    }
  }
} ]

Result:

enter image description here

Update1

Is there somewhere that I could find the work items associated with the commits

We could list the work items associated via the commit ID.

Request URL:

POST https://dev.azure.com/{Org name}/_apis/wit/artifactUriQuery?api-version=5.0-preview

Request Body:

{
  "artifactUris": [
    "vstfs:///Git/Commit/{Project ID}%2F{Repo ID}%2F{Commit ID}"
  ]
}

Result:

enter image description here