我如何获取所有团队服务帐户中的所有工作项?

How can i get all work items across all Team Services accounts?

我正在使用 .NET 库访问 Visual Studio Team Services,我正试图解决 Microsoft 方面的一个明显设计缺陷。显然每个 server/account 不能有超过一个集合,所以我必须使用多个帐户,在这个例子中我将其称为集合,因为微软甚至已经明确 they map to the same thing.

我实际上想要实现的是有一个列表,其中包含我所属的所有集合中的所有工作项目。我有一个 QueryWorkItems() 方法,它使用 GetAllCollections() 来获取我的所有集合。该方法已经过测试并且有效,它确实 return 我拥有的两个帐户。触发整个事件的顶级方法是 AssignedWorkItems()。我的代码如下:

        public static List<TfsTeamProjectCollection> GetAllCollections()
        {
            // Get collections/accounts associated with user
            string request = "https://app.vssps.visualstudio.com/_apis/Accounts?memberId=" + versionControl.AuthorizedIdentity.TeamFoundationId + "&api-version=3.2-preview";
            string content = MakeRequestToAPI(request).Result;
            dynamic results = JsonConvert.DeserializeObject<dynamic>(content);
            List<TfsTeamProjectCollection> collections = new List<TfsTeamProjectCollection>();
            // Iterate through all collections
            Parallel.ForEach((IEnumerable<dynamic>)results.value, collection =>
           {
               TfsTeamProjectCollection col = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri((string)collection.accountUri));
               collections.Add(col);
           });
            return collections;
        }


        public static List<WorkItem> QueryWorkItems(string query)
        {
            List<WorkItem> workItems = new List<WorkItem>();
            List<TfsTeamProjectCollection> collections = GetAllCollections();
            //Parallel.ForEach(collections, collection =>
            foreach(var collection in collections)
            {
                WorkItemCollection items = collection.GetService<WorkItemStore>().Query(query);
                // Add each work item to the overall list
                Parallel.For(0, items.Count, i =>
                {
                    Console.WriteLine(items[i].Title);
                    lock (workItems)
                    {
                        workItems.Add(items[i]);
                    }
                });
            }

            return workItems;
        }

        public static List<WorkItem> AssignedWorkItems()
        {
            Init(); //initializes variables like projectName, workItemStore and VersionControlServer(versionControl)
            string myItems = "select * from issue where [System.AssignedTo]=@me";
            return QueryWorkItems(myItems);
        }

当我调用 AssignedWorkItems 方法时,我收到登录提示,即使我已经设置了默认连接:

虽然我输入了我的凭据后,在这一行中:

WorkItemCollection items = collection.GetService().Query(query);

我收到以下错误:

An unhandled exception of type 'Microsoft.TeamFoundation.TeamFoundationServiceUnavailableException' occurred in Microsoft.TeamFoundation.Client.dll

Additional information: TF31002: Unable to connect to this Team Foundation Server: https://xxxxxx.vssps.visualstudio.com/.

Team Foundation Server Url: https://xxxxxx.vssps.visualstudio.com/

Possible reasons for failure include:

  • The name, port number, or protocol for the Team Foundation Server is incorrect.

  • The Team Foundation Server is offline.

  • The password has expired or is incorrect.

有趣的是,每次我 运行 这个错误中提到的 URL 在我拥有的两个集合之间来回切换。知道为什么会这样吗?

我可以成功测试方法QueryWorkItems

根据您收到的错误消息,VSTS URL 似乎以 https://account.vssps.visualstudio.com instead of https://account.visualstudio.com 格式存储在 collections 中。所以请确认方法 GetAllCollections 中存储在集合中的 URL 是正确的。

我用这个GetAllCollections方法来验证QueryWorkItems:

public static List<TfsTeamProjectCollection> GetAllCollections()
{

    List<TfsTeamProjectCollection> collections = new List<TfsTeamProjectCollection>();

    NetworkCredential cred1 = new NetworkCredential("username for Alternate authentication", "password for Alternate authentication");
    TfsTeamProjectCollection tpc1 = new TfsTeamProjectCollection(new Uri("https://account1.visualstudio.com"), cred1);

    collections.Add(tpc1);

    NetworkCredential cred2 = new NetworkCredential("username for Alternate authentication", "password for Alternate authentication");
    TfsTeamProjectCollection tpc2 = new TfsTeamProjectCollection(new Uri("https://account2.visualstudio.com"), cred2);

    collections.Add(tpc2);
    return collections;
}