devopshq / tfs

Microsoft TFS API Python client
https://devopshq.github.io/tfs/
MIT License
110 stars 28 forks source link

Documentation: How to create a new Work Item that is a child of an existing Work Item? #58

Closed dgtlrift closed 5 years ago

dgtlrift commented 5 years ago

There doesn't seem to be a clear example in the documentation to assert that a new work item is to be the child of another existing one. How does one do this with the API?

SAnCherepan commented 5 years ago

@dgtlrift, there is no "clean" way (yet) to do it with our library.

However, the connection.create_workitem method has the relations_raw parameter that you could use to add links right away.

The code for using relations_raw should look like this:


existing_wi = TFSAPI.get_workitem(123)

parent_link_raw = 
    [
        {
            "rel": "System.LinkTypes.Hierarchy-Reverse", # existing WI will be a parent, new WI will be a child
            "url": existing_wi.url
            "attributes": {
                "isLocked": false
            }
        },
    ]

new_wi = TFSAPI.create_workitem("Task", parent_link_raw)

This is a temporary solution as we are yet to implement adding "normal" links, attachments and now raw JSON, but it should work.

ihar512 commented 5 years ago

@SAnCherepan I actually tried what you suggested and it doesn't seems to work. Here is the error I am getting, could you give me some suggestions? Thanks.

Traceback (most recent call last): File "tfsfsa.py", line 40, in new_wi = client.create_workitem("Task", fields=None, relations_raw=parent_link_raw) File "C:\Python36\tfs\connection.py", line 197, in create_workitem api_version) File "C:\Python36\tfs\connection.py", line 170, in create_workitem raw = self.rest_client.send_post(uri=uri, data=data, headers=headers, project=True, payload=params) File "C:\Python36\tfs\connection.py", line 343, in send_post return self.send_request('POST', uri, data, headers, payload=payload, project=project) File "C:\Python36\tfs\connection.py", line 386, in __send_request response.status_code, result['error'] if 'error' in result else response.reason)) tfs.connection.TFSClientError: TFS API returned HTTP 400 (Bad Request)

ihar512 commented 5 years ago

Also here is my code,

existing_wi = client.get_workitem(9133) parent_link_raw = [ { "rel": "System.LinkTypes.Hierarchy-Reverse", # existing WI will be a parent, new WI will be a child "url": existing_wi.url, "attributes": { "isLocked":False } }, ] new_wi = client.create_workitem("Task", relations_raw=parent_link_raw)

SAnCherepan commented 5 years ago

@ihar512, sorry for the delay.

Troubleshooting steps would be the following:

  1. Ensure that creating a regular (non-linked) item works: new_wi = client.create_workitem("Task")
  2. Ensure that linking that item to the parent item works: new_wi.add_relations_raw(parent_link_raw)

Let me know if you need any extra info on any of these steps. When both creation and linking work, the whole thing should also work.

ihar512 commented 5 years ago

@SAnCherepan, I was able to resolve this issue with your suggestion. Thank you!