Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function returns a 400 Bad Request when called on a work item with more than 200 children #138

Closed
sobjornstad opened this issue Jul 7, 2020 · 1 comment · Fixed by #139
Labels
bug Something isn't working fixed Bug got fixed or found a solution

Comments

@sobjornstad
Copy link

Steps to reproduce

Aggregator CLI 0.9.2.

Attempt to access the .Children property of a work item that has more than 200 child items.

Here is the rule that is failing intermittently for us (when the work item posted happens to have more than 200 children...and before you ask, I'm not the one who decided to have work items with more than 200 children!).

.lang=C#

/*
*
* AutoClose update PBI states based on child items.
*
*/

var debug=false;
var areaPathFilter = "Federated Projects\\GregTest";

var appliesToWorkItemTypes = new[] {"Task", "Bug", "Milestone"};
var statesCausingParentClose = new[] {"Removed", "Done"};
var statesParentIsUnchangedIn = new[] {"Removed", "Done"};
var closedState = "Done";

if(debug && self.AreaPath != areaPathFilter)
{
    logger.WriteInfo($"This rule is in debug mode, and therefore only applies to items in {areaPathFilter}");
    logger.WriteInfo("Exiting");
    return;
}

if (!appliesToWorkItemTypes.Contains(self.WorkItemType))
{
    return $"Work item type was {self.WorkItemType}, so this rule will not be considered.";
}

var parent = self.Parent;
if (parent == null)
{
    return $"Work item has no parent, so this rule will not be considered.";
}

var children = parent.Children;
if (children.All(c => statesCausingParentClose.Contains(c.State)))
{
    if(!statesParentIsUnchangedIn.Contains(parent.State))
    {
        parent.State = closedState;
        parent.History = "All work completed, auto-finishing: ";
        logger.WriteInfo($"Aggregator changed {parent.WorkItemType} {parent.Id}: State = {parent.State}");
    }
    else
    {
        logger.WriteInfo($"Parent {parent.Id} has state {parent.State}. Parent was not changed.");
    }
}
else
{
    logger.WriteInfo($"Parent was not changed.");
}
return;

Expected behavior

A list of the children of the work item is returned.

Actual behavior

A 400 Bad Request is returned from the function, with the error:

One or more errors occurred. (VS403474: You requested 307 work items which exceeds the limit of 200)

Suspected cause

Looking into the code of the Aggregator, here's the Children property:

        public IEnumerable<WorkItemWrapper> Children
        {
            get
            {
                if (ChildrenLinks != null && ChildrenLinks.Any())
                {
                    var store = new WorkItemStore(_context);
                    return store.GetWorkItems(ChildrenLinks);
                }
                else
                    return new WorkItemWrapper[0];
            }
        }

And the GetWorkItems() method:

        public IList<WorkItemWrapper> GetWorkItems(IEnumerable<int> ids)
        {
            _context.Logger.WriteVerbose($"Getting workitems {ids.ToSeparatedString()}");
            return _context.Tracker.LoadWorkItems(ids, (workItemIds) =>
            {
                _context.Logger.WriteInfo($"Loading workitems {workItemIds.ToSeparatedString()}");
                var items = _clients.WitClient.GetWorkItemsAsync(workItemIds, expand: WorkItemExpand.All).Result;
                return items.ConvertAll(i => new WorkItemWrapper(_context, i));
            });
        }

GetWorkItemsAsync is doing the dirty work and seems to be the problem. This has been cited in the past as a limitation of GetWorkItemsAsync, and it may be that ADO introduced a limit to head off this problem. In any case, I would imagine it should be easy to correct by splitting the set of work item IDs into batches if there are more than 200 of them and combining the batches.

@sobjornstad sobjornstad added the bug Something isn't working label Jul 7, 2020
@giuliov
Copy link
Member

giuliov commented Jul 7, 2020

Wow, I am curious about the scenario, as this is a limit on direct children.
Fixing shouldn't be hard, but I won't have much time in the next weeks.

@giuliov giuliov mentioned this issue Jul 9, 2020
giuliov added a commit that referenced this issue Jul 9, 2020
* Limit request to 200 work items a time, fixes #138

* Setup for Integration tests using Terraform

Squashed commit of the following:

commit cd8f95188498387860cc5a2fb18755db8b351e86
Author: Giulio Vian <giuliovdev@hotmail.com>
Date:   Thu Jul 9 18:07:10 2020 +0100

    fix: missed the RG for Scenario4

commit 74d16de
Author: Giulio Vian <giuliovdev@hotmail.com>
Date:   Wed Jul 8 23:20:26 2020 +0100

    fully scripted integration test environment

commit c7e6be3
Author: Giulio Vian <giuliovdev@hotmail.com>
Date:   Fri Jul 3 22:51:11 2020 +0100

    initial scripts to setup integration test
@giuliov giuliov added the fixed Bug got fixed or found a solution label Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Bug got fixed or found a solution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants