这个问题与这篇文章非常相似:Updating source mappings但我正在尝试使用BuildHttpClient而不是直接调用RestAPI。
最终目标是创建构建/发布定义的副本,并将其用于不同的应用程序。
我传递的'dr'对象是一个数据行,其中包含我想用来更新构建定义的数据。这是ClientConnection代码:
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;
}
那么,您也可以使用REST API和api-version=3.2
来更新TFS 2017 U3中的源映射。
您可以使用以下PowerShell示例添加/更新源映射:
Param(
[string]$collectionurl = "http://tfs2017-test:8080/tfs/DefaultCollection",
[string]$project = "ProjectName",
[string]$definitionid = "6",
[string]$user = "Domain\username",
[string]$token = "Password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#Get resonse of the build definition
$defurl = "$collectionurl/$project/_apis/build/definitions/$($definitionid)?api-version=3.2"
$definition = Invoke-RestMethod -Uri $defurl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
#Set repository.properties, source mapping for example:
$definition.repository.properties.tfvcMapping = '{"mappings":[{"serverPath":"$/ScrumProject/Dev","mappingType":"map","localPath":"\\"}]}'
$json = @($definition) | ConvertTo-Json -Depth 99
#Update build definition
$updatedef = Invoke-RestMethod -Uri $defurl -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
您还可以使用TFS BuildHttpClient更新构建定义,请参阅此主题以获取详细信息:https://serverfault.com/questions/799607/tfs-buildhttpclient-updatedefinition-c-example
上面的代码有效,这是一个JSON解析问题。我希望这个例子有助于其他人。当我正在使用这个项目时,我会看到在GitHub上托管完整的解决方案。