如何以编程方式从VSTS下载解决方案 [英] How to download solution from VSTS programatically

查看:171
本文介绍了如何以编程方式从VSTS下载解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种从Visual Studio Team Services下载源代码的方法,基本上我需要压缩解压缩,就像我在VSTS网站上手动下载一样。



我尝试过:



我一直通过VSTS API参考:Projects | Visual Studio Team Services和Team Foundation Server的REST API参考 [ ^ ]但不幸的是我找不到我需要的东西。



解决方法:



1.发出POST并检索 items batch 将返回主分支中所有文件的列表

谢谢。

2.遍历列表并<一个一个href =https://www.visualstudio.com/en-us/docs/integrate/api/tfvc/items#a-file>下载文件



但我真的以为会有一个选项来下载文件夹或分支中的文件压缩。

I need a way to download a source code from Visual Studio Team Services, basically I need a solution zipped, just as you can download it manually when I am on VSTS site.

What I have tried:

I've been through VSTS API reference: Projects | REST API Reference for Visual Studio Team Services and Team Foundation Server[^] but unfortunately I am failing to find what I need.

Workaround:

1. Issue a POST and retrieve items batch that will return a list of all files from Main branch
Thanks.
2. Iterate through list and download files one by one

but I'd really thought there will be an option to download a zip of files in folder or in a branch.

推荐答案

I终于设法使用解决了这个问题用于VSTS的.NET客户端库



我使用这两个软件包: Microsoft.TeamFoundationServer.Client Microsoft。 VisualStudio.Services.Client ,他们可以猜测是围绕VSTS REST API功能的包装。



为了从TFVC中检索压缩文件夹内容我使用方法:

I finally managed to solve this using .NET Client Libraries for VSTS

I use these 2 packages: Microsoft.TeamFoundationServer.Client and Microsoft.VisualStudio.Services.Client, they as one could guess are wrappers around the VSTS REST API functionalities.

In order to retrieve zipped folder content from TFVC I am using method:
Microsoft.TeamFoundation.SourceControl.WebApi.TfvcHttpClient.GetItemsBatchZipAsync(TfvcItemRequestData itemRequestData, Guid project)





这是一个完整的代码示例:





Here is a complete code example:

using System;
using System.IO;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;

namespace BuildAndDeploy.GetSourceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = "


/ myproject / Main;
var personalAccessToken = **** ***********************;
var uri = new Uri( https://myproject.visualstudio.com);

var credentials = new VssBasicCredential( ,personalAccessToken);

// 您可以使用硬编码的ProjectId,或者可能有一些更好的方法可以不总是打电话给这个,但目前它很好,就像这样
Guid projectGuid = GetProjectId(uri,credentials);

使用 var client = new TfvcHttpClient(uri,credentials))
{
var itemRequestData = Create(path);

Stream item = client.GetItemsBatchZipAsync(itemRequestData,projectGuid).Result;

SaveZippedContent( @ C:\ Output \ main.zip,item);
}
}

私人 静态 Guid GetProjectId (Uri uri,VssBasicCredential凭证)
{
使用 var client = < span class =code-keyword> new
ProjectHttpClient(uri,credentials))
{
return client.GetProject(< span class =code-string> MoveDesk)。Result.Id;
}
}

private static TfvcItemRequestData创建( string folderPath)
{
return new TfvcItemRequestData
{
IncludeContentMetadata = true
IncludeLinks = true
ItemDescriptors =
new []
{
new TfvcItemDescriptor
{
Path = folderPath,
RecursionLevel = VersionControlRecursionType.Full
}
}
};
}

private static void SaveZippedContent( string saveToPath,Stream content)
{
var buffer = new byte [ 1024 ];

尝试
{
使用 var output = new FileStream(saveToPath,FileMode.Create))
{
int readBytes;
while ((readBytes = content.Read(buffer, 0 ,buffer.Length)) > 0
{
output.Write(buffer, 0 ,readBytes);
}
}
}
最后
{
content.Dispose();
}
}
}
}
/myproject/Main"; var personalAccessToken = "***************************"; var uri = new Uri("https://myproject.visualstudio.com"); var credentials = new VssBasicCredential("", personalAccessToken); // You can use hard coded ProjectId or there is probably some better way to not always call this, but at the moment it's fine like this Guid projectGuid = GetProjectId(uri, credentials); using (var client = new TfvcHttpClient(uri, credentials)) { var itemRequestData = Create(path); Stream item = client.GetItemsBatchZipAsync(itemRequestData, projectGuid).Result; SaveZippedContent(@"C:\Output\main.zip", item); } } private static Guid GetProjectId(Uri uri, VssBasicCredential credentials) { using (var client = new ProjectHttpClient(uri, credentials)) { return client.GetProject("MoveDesk").Result.Id; } } private static TfvcItemRequestData Create(string folderPath) { return new TfvcItemRequestData { IncludeContentMetadata = true, IncludeLinks = true, ItemDescriptors = new[] { new TfvcItemDescriptor { Path = folderPath, RecursionLevel = VersionControlRecursionType.Full } } }; } private static void SaveZippedContent(string saveToPath, Stream content) { var buffer = new byte[1024]; try { using (var output = new FileStream(saveToPath, FileMode.Create)) { int readBytes; while ((readBytes = content.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, readBytes); } } } finally { content.Dispose(); } } } }


这篇关于如何以编程方式从VSTS下载解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆