GitHub API 更新文件

本文关键字:文件 更新 API GitHub | 更新日期: 2023-09-27 17:56:44

我正在尝试通过GitHub API更新文件。

我已经设置好了一切,最后更新了实际文件,这是一件好事。

但是,假设我的存储库中有这两个文件

  • 文件更新.txt
  • README.md

然后我运行我的程序

FileToBeUpdate.txt 会像它应该的那样更新,但 README.md 被删除。

这是包含更新文件的 5 个步骤的代码:

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

我缺少哪一部分?我是否使用了错误的树开始?

GitHub API 更新文件

似乎在函数 CreateTree 中,我应该在根级别设置base_tree,而不是在我创建的每棵树的级别。

所以在函数 CreateTree 中,内容变成这样:

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

更新的 GitHub 文档:https://github.com/Snakiej/developer.github.com/commit/2c4f93003703f68c4c8a436df8cf18e615e293c7