Search code examples
c#githubsha

How to programmatically find the sha of a github file


I am trying to make an interface between my code and my personal gihub repo and my current issue is that I need to be able to automatically make calls to rewrite the data of a github file. However the main article which has helped me write this code, as well as the github OctoKit docs claim I need the file's sha key in order to make any changes to it. These are all of my imports currently: using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO.Packaging; using System.Net; using System.IO; using Octokit; using System.Text;

I have tried in-depth research through the .Net docs as well as the OctoKit docs and digging through the attributes of every single line. Nothing has yielded any clues as to how to find the sha of a github file. The main article I was following used this line to get the sha, fileDetails.First().sha and 'fileDetails' is created using this line: var fileDetails = gitHubClient.Repository.Content.GetAllContentsByRef(owner, repoName,filePath, branch); however, for Visual Studio 2019, it throws a CS1061 Error claiming this data type cannot have First() applied to it. My code is below for this specific problem. If you know anything thank you very much for your time. If not thanks for a visit. Again if anyone has any insight to fixing this I am in debt to you truly. `private void write_new_announcements_to_website(string websiteurl, string newAnnouncements) {

        GitHubClient gitHubClient = new GitHubClient(new ProductHeaderValue("jerry-spice.github.io"));
        gitHubClient.Credentials = new Credentials("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");

        var sb = new StringBuilder("");
        sb.AppendLine(newAnnouncements);
        var (owner, repoName, filePath, branch) = ("Jerry-Spice", "jerry-spice.github.io",
                websiteurl, "main");

        /*gitHubClient.Repository.Content.CreateFile(
             owner, repoName, filePath,
             new CreateFileRequest($"Updated info for {filePath}", sb.ToString(), branch));*/

        var fileDetails = gitHubClient.Repository.Content.GetAllContentsByRef(owner, repoName,filePath, branch);
        //Console.WriteLine(fileDetails)

        
        var updateResult = gitHubClient.Repository.Content.UpdateFile(owner, repoName, filePath,
            new UpdateFileRequest("My updated file", sb.ToString(), fileDetails.First().sha)); 
    }

`


Solution

  • The problem is GetAllContentsByRef returns as Task. You'll need to await the call to get the data. Also, the SHA property is proper case. See the following changes.

    GitHubClient gitHubClient = new GitHubClient(new ProductHeaderValue("jerry-spice.github.io"));
    gitHubClient.Credentials = new Credentials("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");
    
    var sb = new StringBuilder("");
    sb.AppendLine(newAnnouncements);
    var (owner, repoName, filePath, branch) = ("Jerry-Spice", "jerry-spice.github.io",
        websiteurl, "main");
    
    /*gitHubClient.Repository.Content.CreateFile(
         owner, repoName, filePath,
         new CreateFileRequest($"Updated info for {filePath}", sb.ToString(), branch));*/
    
    var fileDetails = await gitHubClient.Repository.Content.GetAllContentsByRef(owner, repoName,filePath, branch);
    //Console.WriteLine(fileDetails)
    
    
    var updateResult = gitHubClient.Repository.Content.UpdateFile(owner, repoName, filePath,
        new UpdateFileRequest("My updated file", sb.ToString(), fileDetails.First().Sha));