簡體   English   中英

是否可以通過Azure DevOps REST API獲取一批文本內容?

[英]Is it possible to get a batch of text content through Azure DevOps REST API?

我需要從 Azure DevOps 中的項目中的 10.000~ 清單文件中獲取(而不是下載)內容,但我無法實現這一點。 我已經找到了多種方法來一次從一個文件中檢索內容,但在這種情況下,它既不是有效的也不是可持續的解決方案。 通過檢查文件路徑是否以文件名結尾,然后使用 TfvcHttpClientBase.GetItemsBatch 方法,我設法檢索了特定文件類型的所有文件。 但是,此方法不返回項目的內容。

程序.cs

using Microsoft.TeamFoundation.SourceControl.WebApi;

AzureRest azureRest = new AzureRest();
var tfvcItems = azureRest.GetTfvcItems();
List<TfvcItemDescriptor> itemDescriptorsList = new List<TfvcItemDescriptor>();
foreach(var item in tfvcItems)
{
//Example manifest file .NET
    if (item.Path.EndsWith("packages.config"))
    {
            var itemDescriptor = new TfvcItemDescriptor()
            {
                Path = item.Path,
                RecursionLevel = VersionControlRecursionType.None,
                Version = "",
                VersionOption = TfvcVersionOption.None,
                VersionType = TfvcVersionType.Latest
            };
            itemDescriptorsList.Add(itemDescriptor);
    }
}
TfvcItemDescriptor[] itemDescriptorsArray = itemDescriptorsList.ToArray();
var itemBatch = azureRest.GetTfvcItemsBatch(itemDescriptorsArray);
foreach(var itemList in itemBatch)
{
    foreach(var itemListList in itemList)
    {
        Console.WriteLine("Content: " + itemListList.Content); //empty/null
        Console.WriteLine("ContentMetadata: " + itemListList.ContentMetadata); //not empty/null
    }
}

AzureRest.cs

using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
public class AzureRest
    {
        const string ORG_URL = "https://org/url/url";
        const string PROJECT = "Project";
        const string PAT = "PersonalAccessToken";

        private string GetTokenConfig()
        {
            return PAT;
        }

        private string GetProjectNameConfig()
        {
            return PROJECT;
        }

        private VssConnection Authenticate()
        {
            string token = GetTokenConfig();
            string projectName = GetProjectNameConfig();
            var credentials = new VssBasicCredential(string.Empty, token);
            var connection = new VssConnection(new Uri(ORG_URL), credentials);
            return connection;
        }
        public List<TfvcItem> GetTfvcItems()
        {
            var connection = Authenticate();
            using (TfvcHttpClient tfvcClient = connection.GetClient<TfvcHttpClient>())
            {
                var tfvcItems = tfvcClient.GetItemsAsync(scopePath: "/Path", recursionLevel: VersionControlRecursionType.Full, true).Result;
                return tfvcItems;
            }
        }
        public List<List<TfvcItem>> GetTfvcItemsBatch(TfvcItemDescriptor[] itemDescriptors)
        {
            TfvcItemRequestData requestData = new TfvcItemRequestData()
            {
                IncludeContentMetadata = true,
                IncludeLinks = true,
                ItemDescriptors = itemDescriptors
            };
            var connection = Authenticate();
            using (TfvcHttpClient tfvcClient = connection.GetClient<TfvcHttpClient>())
            {
                var tfvcItems = tfvcClient.GetItemsBatchAsync(requestData).Result;
                return tfvcItems;
            }
        }
    }
}

以供參考:
我已經測試了你分享的代碼和在“itemDescriptorsList”調試時發現其中沒有指定內容,所以你無法獲取txt內容。 在此處輸入圖像描述 您應該首先檢查內容屬性並將其添加到“itemDescriptorsList”中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM