我正在开展一个小型学校项目,我需要从 GitHub 存储库更新文件。 一切都工作正常,直到我突然出现错误。 我将 Octokit .net 与 C# WPF 应用程序一起使用。 这里有例外:
Octokit.ApiException:“位于 1ce907108c4582d5a0986d3a37b2777e271a0105,但预计为 47fa57debd39ee6a63f24d39e9513f87814a5ed6”
我不知道为什么会出现这个错误,因为在错误发生之前我没有更改任何内容,现在什么也不起作用了。有人可以帮助我吗? 这里是代码:
private static async void UpdateFile(string fileName, string fileContent)
{
var ghClient = new GitHubClient(new ProductHeaderValue(HEADER));
ghClient.Credentials = new Credentials(API_KEY);
// github variables
var owner = OWNER;
var repo = REPO;
var branch = "main";
var targetFile = fileName;
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 Config Updated", fileContent, 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 Config Created", fileContent, branch));
}
}
经过一番试验,我发现了这个问题。 我同时更新了3个文件,结果Octokit无法同时处理超过1个请求...
如果您也遇到这个问题,只需在发布新请求之前添加约 2 秒的延迟即可。