示例#1
0
        public async Task <IActionResult> Post([FromBody] JObject payload)
        {
            PayloadViewModel vm = BuildPayloadViewModel(payload);

            //make sure pat is not empty, if it is, pull from appsettings
            vm.pat = _appSettings.Value.PersonalAccessToken;

            //if the event type is something other the updated, then lets just return an ok
            if (vm.eventType != "workitem.updated")
            {
                return(new OkResult());
            }

            // create our azure devops connection
            Uri baseUri = new Uri("https://dev.azure.com/" + vm.organization);

            VssCredentials clientCredentials = new VssCredentials(new VssBasicCredential("username", vm.pat));
            VssConnection  vssConnection     = new VssConnection(baseUri, clientCredentials);

            // load the work item posted
            WorkItem workItem = await _workItemRepo.GetWorkItem(vssConnection, vm.workItemId);

            // this should never happen, but if we can't load the work item from the id, then exit with error
            if (workItem == null)
            {
                return(new StandardResponseObjectResult("Error loading workitem '" + vm.workItemId + "'", StatusCodes.Status500InternalServerError));
            }

            // get the related parent
            WorkItemRelation parentRelation = workItem.Relations.Where <WorkItemRelation>(x => x.Rel.Equals("System.LinkTypes.Hierarchy-Reverse")).FirstOrDefault();

            // if we don't have any parents to worry about, then just abort
            if (parentRelation == null)
            {
                return(new OkResult());
            }

            Int32    parentId       = _helper.GetWorkItemIdFromUrl(parentRelation.Url);
            WorkItem parentWorkItem = await _workItemRepo.GetWorkItem(vssConnection, parentId);

            if (parentWorkItem == null)
            {
                return(new StandardResponseObjectResult("Error loading parent work item '" + parentId.ToString() + "'", StatusCodes.Status500InternalServerError));
            }

            string parentState = parentWorkItem.Fields["System.State"] == null ? string.Empty : parentWorkItem.Fields["System.State"].ToString();

            // load rules for updated work item
            RulesModel rulesModel = _rulesRepo.ListRules(vm.workItemType);

            //loop through each rule
            foreach (var rule in rulesModel.Rules)
            {
                if (rule.IfChildState.Equals(vm.state))
                {
                    if (!rule.AllChildren)
                    {
                        if (!rule.NotParentStates.Contains(parentState))
                        {
                            await _workItemRepo.UpdateWorkItemState(vssConnection, parentWorkItem, rule.SetParentStateTo);

                            return(new OkResult());
                        }
                    }
                    else
                    {
                        // get a list of all the child items to see if they are all closed or not
                        List <WorkItem> childWorkItems = await _workItemRepo.ListChildWorkItemsForParent(vssConnection, parentWorkItem);

                        // check to see if any of the child items are not closed, if so, we will get a count > 0
                        int count = childWorkItems.Where(x => !x.Fields["System.State"].ToString().Equals(rule.IfChildState)).ToList().Count;

                        if (count.Equals(0))
                        {
                            await _workItemRepo.UpdateWorkItemState(vssConnection, parentWorkItem, rule.SetParentStateTo);
                        }

                        return(new OkResult());
                    }
                }
            }

            return(new StandardResponseObjectResult("success", StatusCodes.Status200OK));
        }