【问题标题】:Add/Update BuildDefinition.Repository.Properties.tfvcMapping using BuildHttpClient - TFS 2017 U3使用 BuildHttpClient 添加/更新 BuildDefinition.Repository.Properties.tfvcMapping - TFS 2017 U3
【发布时间】: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


    【解决方案1】:

    好吧,您还可以使用带有api-version=3.2 的 REST API 来更新 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

    【讨论】:

    • 有没有办法通过构建 HTTP 客户端来做到这一点,还是我需要切换到其他 API?
    • @gperrego 引用此线程:serverfault.com/questions/799607/…
    • 是的,我的 httpbuild 客户端正在工作,我可以毫无问题地创建新的构建定义。我可以更改名称、更新/添加/删除变量等,但问题是如何更改存储库属性,特别是通过 httpclient 的映射。我错过了什么吗?
    【解决方案2】:

    上面的代码有效,这是一个 JSON 解析问题。我希望这个例子对其他人有所帮助。当我在做这个项目时,我会看到关于在 GitHub 上托管完整的解决方案。

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2019-04-13
      • 2018-04-18
      • 2020-05-15
      • 1970-01-01
      • 2018-07-31
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多