【问题标题】:Cannot successfully call CreatePushAsync无法成功调用 CreatePushAsync
【发布时间】:2018-09-30 08:05:19
【问题描述】:

我正在尝试以编程方式更新现有分支中的文件,但出现此异常。

参数组合无效或不完整。 参数名称:refUpdate

这是我的代码:

GitRefUpdate desiredBranch = new GitRefUpdate()
{
    RepositoryId = sourceRepoGuid,
    OldObjectId = branch.ObjectId
};

GitCommitRef newCommit = new GitCommitRef()
{
    Comment = $"Update config to match new branch ",
    Changes = new GitChange[]
    {
        new GitChange()
        {
            ChangeType = VersionControlChangeType.Edit,
            Item = new GitItem() { Path = $"/{fileName}" },
            NewContent = new ItemContent()
            {
                Content = fileContent,
                ContentType = ItemContentType.RawText,
            },
        }
    },
};
        
GitPush push = gitClient.CreatePushAsync(new GitPush()
{
    RefUpdates = new GitRefUpdate[] { desiredBranch },
        Commits = new GitCommitRef[] { newCommit }
}, sourceRepoGuid).Result;

我会从错误消息中想象我对 GitRefUpdate 对象做错了什么。我已经尝试了OldObjectIdNewObjectId 与从文件的最后一次提交到分支本身的 SHA 的 SHA 的几种不同组合,没有任何东西可以满足对 CreatePushAsync 的调用。

我发现的唯一示例是在 MS 示例中,它创建了一个新分支并添加了一个新文件。我想更新现有分支中的现有文件。

我没有想法。

【问题讨论】:

    标签: c# git tfs tfs-sdk


    【解决方案1】:

    您可以按照以下步骤尝试 TFS REST API:

    1. 获取提交ID值:请求方法:GET; URL [集合 url]/[团队项目名称]/_apis/git/repositories/[存储库名称]/commits?api-version=1.0&branch=master&$top=1
    2. 更新文件内容:请求方式:发布; URL:[集合 url]/[团队项目名称]/_apis/git/repositories/[存储库名称]/pushes?api-version=3.0-preview.2;内容类型:application/json;

    JSON 数据:

    {
        "refUpdates": [{
            "name": "refs/heads/master",
            "oldObjectId": "[step 1 commit ID]"
        }],
        "commits": [{
            "comment": "Updated BuildLog.cs",
            "changes": [{
                "changeType": 2,
                "item": {"path": "/BuildLog.cs"},
                "newContent": {
                    "content": "using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        public class BuildLog
        {
            public int count;
            public string[] value6;
        }
    }
    ",
                    "contentType": 0
                }
            }]
        }]
    }
    

    详情请参考本案例:Updating a file using REST Api Visual Studio Team Services

    【讨论】:

    • 我需要客户端示例的原因是我正在移动我的 REST API 包装类以使用 C# 客户端库。我会看看我是否可以使用您的步骤和示例来弄清楚我需要什么才能让它与客户端库一起使用。
    • 请发布您的测试结果。
    • 按照你的例子,我所要做的就是在我的 GitRefUpdate GitRefUpdate desiredBranch = new GitRefUpdate() { Name= sourceRepoName, OldObjectId = branch.ObjectId }; 中将 RepositoryId 更改为 Name;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2016-04-01
    • 1970-01-01
    相关资源
    最近更新 更多