GitHub API Update File

I am trying to update a file via the GitHub API.

I have all the settings and it ends up updating the actual file, which is good.

However, let's say I have these 2 files in my repo

  • FileToBeUpdated.txt
  • README.md

And then I run my program

Updated FileToBeUpdated.txt as it should, however README.md removed.

This is the 5 step code to update the file:

private static void Main()
{
    string shaForLatestCommit = GetSHAForLatestCommit();

    string shaBaseTree = GetShaBaseTree(shaForLatestCommit);

    string shaNewTree = CreateTree(shaBaseTree);

    string shaNewCommit = CreateCommit(shaForLatestCommit, shaNewTree);

    SetHeadPointer(shaNewCommit);
}

private static void SetHeadPointer(string shaNewCommit)
{
    WebClient webClient = GetMeAFreshWebClient();

    string contents = "{" +
                      "\"sha\":" + "\"" + shaNewCommit + "\", " +
                      // "\"force\":" + "\"true\"" +
                      "}";

    // TODO validate ?
    string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "refs/heads/master", "PATCH", contents);
}

private static string CreateCommit(string latestCommit, string shaNewTree)
{
    WebClient webClient = GetMeAFreshWebClient();

    string contents = "{" +
                      "\"parents\" :[ \"" + latestCommit + "\" ], " +
                      "\"tree\":" + "\"" + shaNewTree + "\", " +
                      "\"message\":" + "\"test\", " +
                      "\"author\": { \"name\": \""+ Constants.Username +"\", \"email\": \""+ Constants.Email+"\",\"date\": \"" + DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture) + "\"}" +
                      "}";


    string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "commits", contents);

    var foo = JsonConvert.DeserializeObject<CommitRootObject>(downloadString);

    return foo.sha;
}

private static string CreateTree(string shaBaseTree)
{
    WebClient webClient = GetMeAFreshWebClient();



    string contents = "{" +
                      "\"tree\" :" +
                      "[ {" +
                      "\"base_tree\": " + "\"" + shaBaseTree + "\"" + "," +
                      "\"path\": " + "\"" + Constants.FileToChange + "\"" + " ," +
                      "\"content\":" + "\"" + DateTime.Now.ToLongTimeString() + "\"" +
                      "} ]" +
                      "}";


    string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "trees", contents);

    var foo = JsonConvert.DeserializeObject<TreeRootObject>(downloadString);

    return foo.sha;
}

private static string GetShaBaseTree(string latestCommit)
{
    WebClient webClient = GetMeAFreshWebClient();

    string downloadString = webClient.DownloadString(Constants.Start + Constants.PathToRepo + "commits/" + latestCommit);

    var foo = JsonConvert.DeserializeObject<CommitRootObject>(downloadString);

    return foo.tree.sha;
}

private static string GetSHAForLatestCommit()
{
    WebClient webClient = GetMeAFreshWebClient();

    string downloadString = webClient.DownloadString(Constants.Start + Constants.PathToRepo + "refs/heads/master");

    var foo = JsonConvert.DeserializeObject<HeadRootObject>(downloadString);

    return foo.@object.sha;
}

private static WebClient GetMeAFreshWebClient()
{
    var webClient = new WebClient();

    webClient.Headers.Add(string.Format("Authorization: token {0}", Constants.OAuthToken));

    return webClient;
}

      

Which part am I missing? Am I using the wrong tree to start with?

+3


source to share


1 answer


It seems that in the CreateTree function, I have to set the base_tree at the root level, not at the level of every tree I create.

So, in the CreateTree function, the content will become:



string contents = "{" +
                      "\"base_tree\": " + "\"" + shaBaseTree + "\"" + "," + // IT THIS LINE!
                      "\"tree\" :" +
                          "[ {" +
                              "\"path\": " + "\"" + Constants.FileToChange + "\"" + " ," +
                              "\"content\":" + "\"" + DateTime.Now.ToLongTimeString() + "\"" +
                      "} ]" +
                  "}";

      

Updated GitHub documentation: https://github.com/Snakiej/developer.github.com/commit/2c4f93003703f68c4c8a436df8cf18e615e293c7

+3


source







All Articles