使用Octokit更新GitHub存储库中的文件 [英] Updating a file in GitHub repository using Octokit

查看:287
本文介绍了使用Octokit更新GitHub存储库中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个Windows窗体应用程序,该应用程序可以使用Octokit在GitHub存储库中创建,更新和删除文件.

I am trying to develop a windows forms application that can create, update and delete files in a GitHub repository using Octokit.

 public Form1()
    {
        InitializeComponent();

        var ghClient = new GitHubClient(new ProductHeaderValue("Octokit-Test"));
        ghClient.Credentials = new Credentials("-personal access token here-");

        // github variables
        var owner = "username";
        var repo = "repository name";
        var branch = "master";

        // create file
        //var createChangeSet = ghClient.Repository.Content.CreateFile(owner,repo,"path/file2.txt",new CreateFileRequest("File creation", "Hello World!", branch));

        // update file
        var updateChangeSet = ghClient.Repository.Content.UpdateFile(owner, repo,"path/file2.txt", new UpdateFileRequest("File update","Hello Universe!", "SHA value should be here", branch));

    }

首先,我设法创建了一个功能齐全的文件(检查注释掉的代码).然后,我尝试使用来更新该文件,

Firstly, I managed to create a file (check the commented out code), which is fully functional. Then I tried to update that file using,

var updateChangeSet = ghClient.Repository.Content.UpdateFile(owner, repo,"path/file2.txt", new UpdateFileRequest("File update","Hello Universe!", "SHA value should be here", branch));

如您所见,在这种情况下,我必须获取sha值,因为"UpdateFileRequest"的要求是

As you can see, in this situation, I have to get the sha value since the requirement for the "UpdateFileRequest" is,

UpdateFileRequest(string message, string content, string sha, string branch)

如何从GitHub接收我的文件的Sha值?

How can I receive this Sha value for my file from GitHub?

我正在关注教程但是当我尝试"createChangeSet.Content.Sha"(不注释掉createChangeSet)时,它在"Content"下方画了一条红线并说,

I am following this tutorial but when I try "createChangeSet.Content.Sha"(without commenting out createChangeSet), it draws a red line underneath "Content" and says,

Task<RepositoryChangeSet> does not contain a definition for 'Content' and no extention method 'Content' accepting a first argument of type Task<RepositoryChangeSet> could be found

我查看了 GitHub文档和它说我应该使用,

I looked at GitHub Documentation and it says I should use,

GET /repos/:owner/:repo/contents/:path

返回存储库中文件或目录的内容,因此我假设我将能够通过这种方式获取sha值.

to return the contents of a file or directory in a repository so I assume I will be able to obtain the sha value this way.

如何实现此方法以在存储库中接收文件的sha值,以便可以使用该值更新文件?

How can I implement this method to receive the sha value for my file in the repository so I can use that value to update the file?

推荐答案

我遇到了同样的问题,要获取sha,您将需要首先获取现有文件,并且使用此文件,您还将获取最后的commit sha,这可以用于更新文件.

I had the same problem and to get the sha you will need to get the existing file first and with this file you also get the last commit sha, which can be used to update the file.

完整的演示代码:

            var ghClient = new GitHubClient(new ProductHeaderValue("Octokit-Test"));
            ghClient.Credentials = new Credentials("//...//");

            // github variables
            var owner = "owner";
            var repo = "repo";
            var branch = "branch";

            var targetFile = "_data/test.txt";

            try
            {
                // try to get the file (and with the file the last commit sha)
                var existingFile = await ghClient.Repository.Content.GetAllContentsByRef(owner, repo, targetFile, branch);

                // update the file
                var updateChangeSet = await ghClient.Repository.Content.UpdateFile(owner, repo, targetFile,
                   new UpdateFileRequest("API File update", "Hello Universe! " + DateTime.UtcNow, existingFile.First().Sha, branch));
            }
            catch (Octokit.NotFoundException)
            {
                // if file is not found, create it
                var createChangeSet = await ghClient.Repository.Content.CreateFile(owner,repo, targetFile, new CreateFileRequest("API File creation", "Hello Universe! " + DateTime.UtcNow, branch));
            }

我不确定是否有更好的方法-如果找不到搜索到的文件,则会引发异常.

I'm not sure if there is a better way to do it - if the searched file is not found an exception is thrown.

但是它似乎可以这样工作.

But it seems to work that way.

这篇关于使用Octokit更新GitHub存储库中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆