问题描述
我正在尝试使某些过程自动化,以使生活更加轻松。我们团队收到多个请求,要求在TFS 2017中创建一个文件夹(它们没有权限),然后为该源代码管理文件夹设置关联的内部版本。 我认为构建创建部分有办法,但是查询本地TFS 2017服务器以获取特定路径下的文件夹列表非常棘手。到目前为止,我什至无法使用此连接到服务器:
var collectionUri = "http://tfs-server:8080/tfs/DefaultCollection/";
var teamProjectName = "MYPROJECT";
Uri uri = new Uri(collectionUri);
var clientCredentials = new VssCredentials(new WindowsCredential(new NetworkCredential("USERNAME","PASSWORD","COLLECTIONNAME")));
var connection = new VssConnection(uri,clientCredentials);
var sourceControlServer = connection.GetClient<TfvcHttpClient>();
抛出异常:转换值“ System.Security.Principal.WindowsIdentity;”时出错键入“ Microsoft.VisualStudio.Services.Identity.IdentityDescriptor”
请先有人帮助我连接到服务器!关于此的文档很难找到,我看不到任何实际可行的示例。
接下来我要看的是创建不存在的文件夹。不知道该怎么做,也许使用
sourceControlServer.GetBranchAsync(teamProjectName + FolderName);
谢谢!
编辑: 好的,我这样做不是错误地创建连接:
Uri uri = new Uri("http://tfs-server:8080/tfs/DefaultCollection/");
var clientCredentials = new VssCredentials(new WindowsCredential(new NetworkCredential("USERNAME","DOMAIN")));
var buildServer = new BuildHttpClient(uri,clientCredentials);
var sourceControlServer = new TfvcHttpClient(uri,clientCredentials);
所以现在只想弄清楚如何从TFS列出和创建文件夹以及创建版本!
编辑:
所以我可以执行查询了,所以我可以检查这样的路径下是否存在文件夹:
var teamProjectName = "USA";
Uri uri = new Uri("http://tfs-server:8080/tfs/DefaultCollection/");
var clientCredentials = new VssCredentials(new WindowsCredential(new NetworkCredential("USERNAME","DOMAIN")));
TfvcHttpClient sourceControlServer = new TfvcHttpClient(uri,clientCredentials);
List<TfvcItem> branchItems;
using (sourceControlServer) {
branchItems = sourceControlServer.GetItemsAsync("$/USA/Development/NewFolder",VersionControlRecursionType.OneLevel).Result;
}
return branchItems.Count > 0;
这将找到该文件夹下的所有项目。因此,如果没有文件夹,它将返回0,所以我可以继续创建该文件夹。 因此,下一个问题是如何创建文件夹。使用CreateChangesetAsync。
解决方法
更新:
要使用Client API和CreateChangesetAsync方法在TFVC中创建文件,可以参考以下示例控制台应用程序:
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
internal static async Task Main(string[] args)
{
var orgUrl = new Uri(args[0]);
string serverPath = args[1];
string localPath = args[2];
string contentType = args[3];
string pat = args[4];
var changes = new List<TfvcChange>()
{
new TfvcChange()
{
ChangeType = VersionControlChangeType.Add,Item = new TfvcItem()
{
Path = serverPath,ContentMetadata = new FileContentMetadata()
{
Encoding = Encoding.UTF8.WindowsCodePage,ContentType = contentType,}
},NewContent = new ItemContent()
{
Content = Convert.ToBase64String(File.ReadAllBytes(localPath)),ContentType = ItemContentType.Base64Encoded
}
}
};
var changeset = new TfvcChangeset()
{
Changes = changes,Comment = $"Added {serverPath} from {localPath}"
};
var connection = new VssConnection(orgUrl,new VssBasicCredential(string.Empty,pat));
var tfvcClient = connection.GetClient<TfvcHttpClient>();
await tfvcClient.CreateChangesetAsync(changeset);
}
}
}
此外,请不要使用tf命令行,请在此处检查解决方案: