示例#1
0
 public EngineContext(WorkItemTrackingHttpClientBase client, Guid projectId, string projectName, string personalAccessToken, IAggregatorLogger logger)
 {
     Client              = client;
     Logger              = logger;
     Tracker             = new Tracker();
     ProjectId           = projectId;
     ProjectName         = projectName;
     PersonalAccessToken = personalAccessToken;
 }
示例#2
0
 public WorkItemExposed(WorkItem original, WorkItemTrackingHttpClientBase witClient, RequestContextBase context)
 {
     this.current   = original ?? throw new ArgumentNullException(nameof(original));
     this.witClient = witClient ?? throw new ArgumentNullException(nameof(witClient));
     this.context   = context;
     changeLog      = new FieldsChangeLog()
     {
         WorkItemId = original.Id.GetValueOrDefault(0),
         RevisionNo = original.Rev.GetValueOrDefault(0)
     };
 }
示例#3
0
        public async Task <string> ExecuteAsync(string collectionUrl, Guid projectId, string projectName, string personalAccessToken, int workItemId, WorkItemTrackingHttpClientBase witClient, CancellationToken cancellationToken)
        {
            if (State == EngineState.Error)
            {
                return(string.Empty);
            }

            var context = new EngineContext(witClient, projectId, projectName, personalAccessToken, logger);
            var store   = new WorkItemStore(context);
            var self    = store.GetWorkItem(workItemId);

            logger.WriteInfo($"Initial WorkItem {workItemId} retrieved from {collectionUrl}");

            var globals = new Globals
            {
                self  = self,
                store = store
            };

            logger.WriteInfo($"Executing Rule...");
            var result = await roslynScript.RunAsync(globals, cancellationToken);

            if (result.Exception != null)
            {
                logger.WriteError($"Rule failed with {result.Exception}");
                State = EngineState.Error;
            }
            else if (result.ReturnValue != null)
            {
                logger.WriteInfo($"Rule succeeded with {result.ReturnValue}");
            }
            else
            {
                logger.WriteInfo($"Rule succeeded, no return value");
            }

            State = EngineState.Success;

            logger.WriteVerbose($"Post-execution, save any change (mode {saveMode})...");
            var saveRes = await store.SaveChanges(saveMode, !DryRun, cancellationToken);

            if (saveRes.created + saveRes.updated > 0)
            {
                logger.WriteInfo($"Changes saved to Azure DevOps (mode {saveMode}): {saveRes.created} created, {saveRes.updated} updated.");
            }
            else
            {
                logger.WriteInfo($"No changes saved to Azure DevOps.");
            }

            return(result.ReturnValue);
        }
示例#4
0
 public WorkItemRepository(WorkItemTrackingHttpClientBase client)
 {
     this.client = client;
 }
示例#5
0
        private static async Task UpdateWorkItemAsync(WorkItem workItem, string version, WorkItemTrackingHttpClientBase witClient)
        {
            var jsonPatchDocument = new JsonPatchDocument
            {
                new JsonPatchOperation
                {
                    Operation = Operation.Add,
                    Path      = "/fields/ScrumforHeroAG.VersionDeployed",
                    Value     = version
                }
            };

            await witClient.UpdateWorkItemAsync(jsonPatchDocument, workItem.Id.Value);
        }
示例#6
0
 public WorkItemRepositoryExposed(RequestContextBase context)
 {
     this.context   = context;
     this.witClient = context.WitClient;
 }
示例#7
0
 public TfsNotUpdatedReceiver(WorkItemTrackingHttpClientBase client, IConfiguration configuration) : base(client, configuration)
 {
 }
示例#8
0
        private static WorkItemQueryResult GetWorkItemQueryResult(string teamProjectName, DateTime endDate, DateTime startDate,
                                                                  IReadOnlyList <NtTeamMember> ntTeamMembers, WorkItemTrackingHttpClientBase witClient, IEnumerable <TeamSettingsIteration> iterations, out WorkItemLink[] workItemLinks)
        {
            var rangeIterations = iterations.Where(i =>
                                                   i.Attributes.StartDate.HasValue && i.Attributes.FinishDate.HasValue &&
                                                   ((startDate.CompareTo(i.Attributes.StartDate) <= 0 && endDate.CompareTo(i.Attributes.StartDate) >= 0) ||
                                                    startDate.CompareTo(i.Attributes.FinishDate) <= 0 && endDate.CompareTo(i.Attributes.FinishDate) >= 0)).ToList();

            var query =
                "Select [System.Id], [System.WorkItemType], [Microsoft.VSTS.Common.Activity], [System.Title], [System.AssignedTo], [System.State], [System.IterationPath], [Microsoft.VSTS.Common.ClosedDate], [System.AreaPath], [Nt.Duration], [System.Tags] " +
                "From WorkItemLinks " +
                "Where (Source.[System.TeamProject] = '" + teamProjectName +
                "' and Source.[System.State] <> 'Removed' and Source.[System.WorkItemType] <> 'Task') " +
                "And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') " +
                "And (Target.[System.TeamProject] = '" + teamProjectName +
                "' and Target.[System.State] = 'Done' ";

            if (rangeIterations.Count > 0)
            {
                query += " and ( ";
                for (var i = 0; i < rangeIterations.Count; i++)
                {
                    var iteration = rangeIterations[i];
                    query += " Target.[System.IterationPath] == '" + iteration.Path + "' ";

                    if (i < rangeIterations.Count - 1)
                    {
                        query += " or ";
                    }
                }
                query += " ) ";
            }
            query += " and ";

            if (ntTeamMembers.Count > 0)
            {
                query += " ( ";

                for (var i = 0; i < ntTeamMembers.Count; i++)
                {
                    query += " Target.[System.AssignedTo] = '" + ntTeamMembers[i].UniqueName + "' ";
                    if (i < ntTeamMembers.Count - 1)
                    {
                        query += " or ";
                    }
                }

                query += " ) and ";
            }
            else
            {
                query += " Target.[System.AssignedTo] = '' and ";
            }

            query += " Target.[System.WorkItemType] = 'Task') " +
                     "Order By [System.Id] mode (Recursive, ReturnMatchingChildren) ";

            //create a wiql object and build our query
            var wiql = new Wiql()
            {
                Query = query
            };


            var workItemQueryResult = witClient.QueryByWiqlAsync(wiql).Result;
            var relations           = workItemQueryResult.WorkItemRelations;

            workItemLinks = relations as WorkItemLink[] ?? relations.ToArray();
            return(workItemQueryResult);
        }
示例#9
0
        private static List <NtWorkItem> GetNtWorkItems(IEnumerable <WorkItemLink> workItemLinks, WorkItemTrackingHttpClientBase witClient,
                                                        WorkItemQueryResult workItemQueryResult, string teamProjectName, IReadOnlyCollection <TeamSettingsIteration> iterations, DateTime starDate, DateTime endDate)
        {
            var fields        = FIELDS;
            var allIds        = new List <int>();
            var epicIds       = new List <int>();
            var miscParentTag = new Dictionary <int, string>();
            var reverseIdDict = new Dictionary <int, bool>();
            var idDict        = new Dictionary <int, int?>();
            var categoryDict  = new Dictionary <int, string>();

            foreach (var workItemLink in workItemLinks)
            {
                allIds.Add(workItemLink.Target.Id);
                if (!workItemLink.Rel.IsNullOrEmpty())
                {
                    reverseIdDict[workItemLink.Source.Id] = true;
                    idDict[workItemLink.Target.Id]        = workItemLink.Source.Id;
                    continue;
                }
                idDict.Add(workItemLink.Target.Id, null);
                epicIds.Add(workItemLink.Target.Id);
            }
            var epicWorkItems = witClient.GetWorkItemsAsync(epicIds, fields, workItemQueryResult.AsOf).Result;

            foreach (var epicWorkItem in epicWorkItems)
            {
                var id = int.Parse(epicWorkItem.Fields["System.Id"].ToString());

                var title = "MISC";
                var tags  = epicWorkItem.Fields.ContainsKey("System.Tags") ? epicWorkItem.Fields["System.Tags"]?.ToString().Trim() : string.Empty;
                if (epicWorkItem.Fields["System.WorkItemType"].ToString().ToLower().Equals("epic"))
                {
                    title = epicWorkItem.Fields["System.Title"].ToString();
                }
                else if (!string.IsNullOrEmpty(tags))
                {
                    if (tags == NtInsight)
                    {
                        title = NtInsight;
                    }
                    miscParentTag.Add(id, tags);
                }
                categoryDict.Add(id, title);
            }

            var taskIds = new List <int>();

            foreach (var id in allIds)
            {
                if (reverseIdDict.ContainsKey(id))
                {
                    continue;
                }
                taskIds.Add(id);
                var refId = id;
                while (idDict[refId].HasValue)
                {
                    refId = idDict[refId].Value;
                }
                categoryDict[id] = categoryDict[refId];

                if (!miscParentTag.ContainsKey(refId))
                {
                    continue;
                }
                miscParentTag[id] = miscParentTag[refId];
            }

            var       skip = 0;
            const int take = 100;

            var ntWorkItems = new List <NtWorkItem>();

            int[] curTaskIds;
            do
            {
                curTaskIds = taskIds.Skip(skip).Take(take).ToArray();
                if (curTaskIds.Length == 0)
                {
                    break;
                }
                var taskWorkItems = witClient.GetWorkItemsAsync(curTaskIds, fields, workItemQueryResult.AsOf).Result;

                foreach (var taskWorkItem in taskWorkItems)
                {
                    var    id           = int.Parse(taskWorkItem.Fields["System.Id"].ToString());
                    string workItemType = null;
                    if (taskWorkItem.Fields.ContainsKey("System.WorkItemType"))
                    {
                        workItemType = taskWorkItem.Fields["System.WorkItemType"].ToString();
                    }
                    string activity = null;
                    if (taskWorkItem.Fields.ContainsKey("Microsoft.VSTS.Common.Activity"))
                    {
                        activity = taskWorkItem.Fields["Microsoft.VSTS.Common.Activity"].ToString();
                    }
                    var    title      = taskWorkItem.Fields["System.Title"].ToString();
                    string assignedTo = null;
                    if (taskWorkItem.Fields.ContainsKey("System.AssignedTo"))
                    {
                        assignedTo = taskWorkItem.Fields["System.AssignedTo"].ToString();
                    }
                    var      state            = taskWorkItem.Fields["System.State"].ToString();
                    string   closedDateString = null;
                    DateTime?closedDate       = null;
                    if (taskWorkItem.Fields.ContainsKey("Microsoft.VSTS.Common.ClosedDate"))
                    {
                        closedDate       = DateTime.Parse(taskWorkItem.Fields["Microsoft.VSTS.Common.ClosedDate"].ToString());
                        closedDateString = closedDate.Value.ToString("yyyy MMMM dd");
                    }
                    string iterationPath = null;
                    if (taskWorkItem.Fields.ContainsKey("System.IterationPath"))
                    {
                        iterationPath = taskWorkItem.Fields["System.IterationPath"].ToString();
                        var iteration = iterations
                                        .FirstOrDefault(i => i.Path.Trim().Equals(iterationPath.Trim()));

                        if (closedDate.HasValue && iteration?.Attributes.StartDate != null && iteration.Attributes.FinishDate.HasValue)
                        {
                            if (closedDate.Value.CompareTo(iteration.Attributes.StartDate) >= 0 &&
                                closedDate.Value.CompareTo(iteration.Attributes.FinishDate) <= 0)
                            {
                                //correct case
                            }
                            else
                            {
                                closedDate = iteration.Attributes.FinishDate;
                            }

                            closedDateString = closedDate.Value.ToString("yyyy MMMM dd");
                        }
                    }
                    string areaPath = null;
                    if (taskWorkItem.Fields.ContainsKey("System.AreaPath"))
                    {
                        areaPath = taskWorkItem.Fields["System.AreaPath"].ToString();
                    }
                    double?duration = null;
                    if (taskWorkItem.Fields.ContainsKey("Nt.Duration"))
                    {
                        duration = double.Parse(taskWorkItem.Fields["Nt.Duration"].ToString());
                    }
                    var product    = categoryDict[id];
                    var ntWorkItem = new NtWorkItem
                    {
                        Id            = id,
                        WorkItemType  = workItemType,
                        Activity      = activity,
                        Title         = title,
                        AssignedTo    = assignedTo,
                        State         = state,
                        IterationPath = iterationPath,
                        ClosedDate    = closedDateString,
                        AreaPath      = areaPath,
                        Duration      = duration,
                        Product       = product,
                        TeamProject   = teamProjectName,
                        ParentTags    = miscParentTag.ContainsKey(id) ? miscParentTag[id] : string.Empty
                    };
                    ntWorkItems.Add(ntWorkItem);
                }

                skip += take;
            } while (curTaskIds.Count() == take);
            return(ntWorkItems);
        }
示例#10
0
 protected TfsReceiver(WorkItemTrackingHttpClientBase client, IConfiguration configuration)
 {
     this.client        = client;
     this.configuration = configuration;
 }
示例#11
0
 public TfsReceiver1(WorkItemTrackingHttpClientBase client, IConfiguration configuration) : base(client, configuration)
 {
 }