コード例 #1
0
 private static void FindAndReplaceInWiql(CopyQueryParameters parameters, WorkItemQuery sourceQuery)
 {
     if (parameters.QueryReplacements.QueryFindAndReplace != null && parameters.QueryReplacements.QueryFindAndReplace.Count > 0)
     {
         foreach (var item in parameters.QueryReplacements.QueryFindAndReplace)
         {
             if (!string.IsNullOrEmpty(item.Find) && !string.IsNullOrEmpty(item.Replace) &&
                 !item.Find.Equals(item.Replace, StringComparison.InvariantCultureIgnoreCase))
             {
                 var findIndex = sourceQuery.wiql.IndexOf(item.Find, StringComparison.InvariantCultureIgnoreCase);
                 while (findIndex > -1)
                 {
                     sourceQuery.wiql = sourceQuery.wiql.Remove(findIndex, item.Find.Length);
                     sourceQuery.wiql = sourceQuery.wiql.Insert(findIndex, item.Replace);
                     findIndex        = sourceQuery.wiql.IndexOf(item.Find, findIndex + item.Replace.Length, StringComparison.InvariantCultureIgnoreCase);
                 }
                 continue;
             }
             if (item.TryRemoveSource)
             {
                 var newFind    = item.Find.Replace("Source.", string.Empty);
                 var newReplace = item.Replace.Replace("Source.", string.Empty);
                 if (!string.IsNullOrEmpty(newFind) && !string.IsNullOrEmpty(newReplace) &&
                     !newFind.Equals(newReplace, StringComparison.InvariantCultureIgnoreCase))
                 {
                     var findIndex = sourceQuery.wiql.IndexOf(newFind, StringComparison.InvariantCultureIgnoreCase);
                     while (findIndex > -1)
                     {
                         sourceQuery.wiql = sourceQuery.wiql.Remove(findIndex, newFind.Length);
                         sourceQuery.wiql = sourceQuery.wiql.Insert(findIndex, newReplace);
                         findIndex        = sourceQuery.wiql.IndexOf(newFind, findIndex + newReplace.Length, StringComparison.InvariantCultureIgnoreCase);
                     }
                     continue;
                 }
             }
             if (item.TryRemoveTarget)
             {
                 var newFind    = item.Find.Replace("Target.", string.Empty);
                 var newReplace = item.Replace.Replace("Target.", string.Empty);
                 if (!string.IsNullOrEmpty(newFind) && !string.IsNullOrEmpty(newReplace) &&
                     !newFind.Equals(newReplace, StringComparison.InvariantCultureIgnoreCase))
                 {
                     var findIndex = sourceQuery.wiql.IndexOf(newFind, StringComparison.InvariantCultureIgnoreCase);
                     while (findIndex > -1)
                     {
                         sourceQuery.wiql = sourceQuery.wiql.Remove(findIndex, newFind.Length);
                         sourceQuery.wiql = sourceQuery.wiql.Insert(findIndex, newReplace);
                         findIndex        = sourceQuery.wiql.IndexOf(newFind, findIndex + newReplace.Length, StringComparison.InvariantCultureIgnoreCase);
                     }
                     continue;
                 }
             }
         }
     }
 }
コード例 #2
0
        public static WorkItemQuery CopyQuery(CopyQueryParameters parameters,
                                              string sourceProjectName, string sourceTeamName,
                                              string targetProjectName, string targetTeamName)
        {
            var sourceQuery = TfsStatic.GetWorkItemQuery(true, parameters.QueryId, QueryExpand.minimal, 0);

            var targetQueryInfo = GetTargetQueryFolderId(sourceQuery, parameters.QueryReplacements);

            if (TfsStatic.SourceTeamProjectBaseUri.Equals(TfsStatic.TargetTeamProjectBaseUri, StringComparison.InvariantCultureIgnoreCase) &&
                sourceQuery.path.Equals($"{targetQueryInfo.FolderPath}/{targetQueryInfo.QueryName}", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine($"Skipped: Target query path matches source query ({targetQueryInfo.FolderPath}/{targetQueryInfo.QueryName})");
                Console.ForegroundColor = ConsoleColor.White;
                return(sourceQuery);
            }

            sourceQuery.name = targetQueryInfo.QueryName;
            sourceQuery.path = targetQueryInfo.FolderPath;
            RemoveTeamAreaId(sourceQuery);
            if (!string.IsNullOrEmpty(sourceProjectName) || !string.IsNullOrEmpty(sourceTeamName) ||
                !string.IsNullOrEmpty(targetProjectName) || !string.IsNullOrEmpty(targetTeamName))
            {
                if (parameters.QueryReplacements == null)
                {
                    parameters.QueryReplacements = new QueryReplacementParameters();
                }
                if (parameters.QueryReplacements.QueryFindAndReplace == null)
                {
                    parameters.QueryReplacements.QueryFindAndReplace = new();
                }
                if (!parameters.QueryReplacements.QueryFindAndReplace.Any(o => o.Find.Equals($"[{sourceProjectName}]\\{sourceTeamName}")))
                {
                    parameters.QueryReplacements.QueryFindAndReplace.Add(new FindAndReplace
                    {
                        Find    = $"[{sourceProjectName}]\\{sourceTeamName}",
                        Replace = $"[{targetProjectName}]\\{targetTeamName}",
                    });
                }
                if (!parameters.QueryReplacements.QueryFindAndReplace.Any(o => o.Find.Equals($"[{sourceProjectName}]")))
                {
                    parameters.QueryReplacements.QueryFindAndReplace.Add(new FindAndReplace
                    {
                        Find    = $"[{sourceProjectName}]",
                        Replace = $"[{targetProjectName}]",
                    });
                }
            }
            FindAndReplaceInWiql(parameters, sourceQuery);

            WorkItemQuery result;
            int           tryCount = 0;

            while (true)
            {
                try
                {
                    result = TryWriteQuery(sourceQuery, targetQueryInfo);
                    break;
                }
                catch
                {
                    tryCount++;
                    if (tryCount >= 5)
                    {
                        Program.WriteFileProgress($"Query creation failed from source query {sourceQuery.path}");
                        throw;
                    }
                }
                Thread.Sleep(2500);
            }
            return(result);
        }