Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Conversation

vtbassmatt
Copy link
Member

fixes #7

@jfthuong
Copy link

jfthuong commented Oct 24, 2018

Great, exactly was I needed.

Now, I have a question on that:

I have following code, based on your example:

def print_work_items(work_items):
    for work_item in work_items:
        print(work_item)
        print(
            "{0} {1}: {2}".format(
                work_item.fields["System.WorkItemType"],
                work_item.id,
                work_item.fields["System.Title"],
            )
        )

def get_TC_from_query(query):
    query_wiql = Wiql(query=query)
    result_query = wit_client.query_by_wiql(query_wiql)
    print_work_items(result_query.work_items)

It returns the following message

{'additional_properties': {}, 'id': 1063128, 'url': 'https://myserver.com:8443/tfs/Development/_apis/wit/workItems/1063128'}
Traceback (most recent call last):
  File "c:\SVN\Scripts\Playground\TFS\test_TFS.py", line 58, in <module>
    """
  File "c:\SVN\Scripts\Playground\TFS\test_TFS.py", line 39, in get_TC_from_query
    print_work_items(result_query.work_items)
  File "c:\SVN\Scripts\Playground\TFS\test_TFS.py", line 18, in print_work_items
    work_item.fields["System.WorkItemType"],
AttributeError: 'WorkItemReference' object has no attribute 'fields'

I use following query, that works in the WIQL PlayGround of TFS.

SELECT
        [System.Id],
        [System.WorkItemType],
        [System.Title],
        [System.State],
        [System.AreaPath],
        [System.IterationPath]
FROM workitems
WHERE
        [System.WorkItemType] = 'Test Case'
        AND [System.AreaPath] UNDER 'TestPortfolio\Program\MySoftware'
ORDER BY [System.ChangedDate] DESC"""

NOTE: I have removed [System.TeamProject] = @project because it returns no result. And neither is your example on my code.... Weird.

What am I missing?

Thanks

@jfthuong
Copy link

Note that the following patch can return WorkItems but it is very ugly and might be very slow (1 additional query for each WorkItem...)

def get_TC_from_query(query):
    query_wiql = Wiql(query=query)
    results = wit_client.query_by_wiql(query_wiql).work_items
    # We do a ugly patch to get the WorkItem:
    # WIQL query gives a WorkItemReference => we get the corresponding WorkItem from id
    work_items = (wit_client.get_work_item(int(result.id)) for result in results)

@danhellem
Copy link

The query by wiql will only get you the id's of the results. it also includes the columns. But you can't loop through the results to get the work item fields. You need to make separate call for each id to get the details.

Step 1: query
https://docs.microsoft.com/en-us/rest/api/vsts/wit/wiql/query%20by%20wiql?view=vsts-rest-4.1

Step 2: get the details
https://docs.microsoft.com/en-us/rest/api/vsts/wit/work%20items/list?view=vsts-rest-4.1

@jfthuong
Copy link

jfthuong commented Oct 30, 2018

Thanks @danhellem,

It's actually what I concluded from the CLI examples, and in particular the code for WorkItems:
https://github.com/Microsoft/vsts-cli/blob/dev/src/common_modules/vsts-cli-work-common/vsts/cli/work/common/work_item.py

So my code was right, good to know.

The example should be updated and #9 can be closed.

@vtbassmatt vtbassmatt closed this Nov 9, 2018
@vtbassmatt vtbassmatt deleted the wit branch November 9, 2018 16:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Samples are rather limited

3 participants