Updating a file in GitHub repository using Octokit

大兔子大兔子 提交于 2019-12-09 22:35:23

问题


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));

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)

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

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

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

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

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.

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?


回答1:


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.

Full demo code:

            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.



来源:https://stackoverflow.com/questions/41316067/updating-a-file-in-github-repository-using-octokit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!