【发布时间】:2019-01-31 03:25:11
【问题描述】:
这个问题与这篇文章非常相似:Updating source mappings 但我正在尝试使用 BuildHttpClient 而不是直接调用 RestAPI。
最终目标是创建构建/发布定义的副本并将其用于不同的应用程序。
我传递的“dr”对象是一个数据行,其中包含我想用来更新构建定义的数据。这是客户端连接代码:
VssConnection connection = new VssConnection(serverUrl, new VssCredentials());
BuildHttpClient bdClient = connection.GetClient<BuildHttpClient>();
// using Wait on the task
Task<BuildDefinition> templateTask = bdClient.GetDefinitionAsync(teamProjectName, IDtoClone);
templateTask.Wait();
BuildDefinition updatedDefinition = ReplaceBuildParameters(templateTask.Result, dr);
Task<BuildDefinition> updatedTask = bdClient.CreateDefinitionAsync(updatedDefinition, teamProjectName);
updatedTask.Wait();
return updatedTask.Result;
更新
根据下面 Andy 的反馈,我更新了代码。我没有尝试更新属性对象,而是替换它。我想我有这个工作,我还有一些测试要验证。我正在尝试使用 JObject 获取该值并在修改后对其进行更新。
private static BuildDefinition ReplaceBuildParameters(BuildDefinition resultDef, DataRow dr)
{
resultDef.Name = "myCreateBuildAttempt";
resultDef.Path = "\\Templates\\POCSandbox";
foreach (DataColumn column in dr.Table.Columns)
{
switch (column.ColumnName)
{
case "ServerPath":
JObject tfvcObj = new JObject();
foreach (KeyValuePair<string, string> prop in resultDef.Repository.Properties)
{
if (prop.Key == "tfvcMapping")
{
KeyValuePair<string, string> myPath = new KeyValuePair<string, string>("serverPath", "$/MASTER/PRES");
tfvcObj = JObject.Parse(prop.Value);
var mappings = tfvcObj["mappings"];
JToken myToken = JToken.Parse(mappings[0].ToString());
myToken["serverPath"] = "$/MASTER/PRES";
mappings[0] = myToken;
tfvcObj["mappings"] = mappings;
}
}
resultDef.Repository.Properties["tfvcMapping"] = tfvcObj.ToString(Newtonsoft.Json.Formatting.None);
break;
default:
break;
}
}
return resultDef;
}
【问题讨论】:
标签: c# tfs tfsbuild azure-pipelines-release-pipeline