check if file exists in repo (#51)

This commit is contained in:
Kosta Petan 2024-04-22 13:24:27 +02:00 committed by GitHub
parent 4808f2145a
commit 615db4fdae

View File

@ -51,22 +51,26 @@ public class GithubService : IManageGithub
{ {
var value = reader.ReadToEnd(); var value = reader.ReadToEnd();
// Check if the file exists try
var existingFiles = await _ghClient.Repository.Content.GetAllContentsByRef(org, repo, filePath, branch);
if (existingFiles.Any())
{ {
// Check if the file exists
var existingFiles = await _ghClient.Repository.Content.GetAllContentsByRef(org, repo, filePath, branch);
var existingFile = existingFiles.First(); var existingFile = existingFiles.First();
// If the file exists, update it // If the file exists, update it
var updateChangeSet = await _ghClient.Repository.Content.UpdateFile( var updateChangeSet = await _ghClient.Repository.Content.UpdateFile(
org, repo, filePath, org, repo, filePath,
new UpdateFileRequest("Updated file via AI", value, existingFile.Sha, branch)); // TODO: add more meaningfull commit message new UpdateFileRequest("Updated file via AI", value, existingFile.Sha, branch)); // TODO: add more meaningful commit message
} }
else catch (NotFoundException)
{ {
// If the file doesn't exist, create it // If the file doesn't exist, create it
var createChangeSet = await _ghClient.Repository.Content.CreateFile( var createChangeSet = await _ghClient.Repository.Content.CreateFile(
org, repo, filePath, org, repo, filePath,
new CreateFileRequest("Created file via AI", value, branch)); // TODO: add more meaningfull commit message new CreateFileRequest("Created file via AI", value, branch)); // TODO: add more meaningful commit message
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error while uploading file {item.Name}");
} }
} }
} }