From 615db4fdaea016314dba26085018b671f6e9bdd4 Mon Sep 17 00:00:00 2001 From: Kosta Petan Date: Mon, 22 Apr 2024 13:24:27 +0200 Subject: [PATCH] check if file exists in repo (#51) --- .../Services/GithubService.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/samples/gh-flow/src/Microsoft.AI.DevTeam/Services/GithubService.cs b/samples/gh-flow/src/Microsoft.AI.DevTeam/Services/GithubService.cs index e43641e7f..7a1316b9a 100644 --- a/samples/gh-flow/src/Microsoft.AI.DevTeam/Services/GithubService.cs +++ b/samples/gh-flow/src/Microsoft.AI.DevTeam/Services/GithubService.cs @@ -51,22 +51,26 @@ public class GithubService : IManageGithub { var value = reader.ReadToEnd(); - // Check if the file exists - var existingFiles = await _ghClient.Repository.Content.GetAllContentsByRef(org, repo, filePath, branch); - if (existingFiles.Any()) + try { + // Check if the file exists + var existingFiles = await _ghClient.Repository.Content.GetAllContentsByRef(org, repo, filePath, branch); var existingFile = existingFiles.First(); // If the file exists, update it var updateChangeSet = await _ghClient.Repository.Content.UpdateFile( 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 var createChangeSet = await _ghClient.Repository.Content.CreateFile( 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}"); } } }