@@ -47,7 +47,8 @@ public static async Task<string> GetFileContentAsync(this IGitHubClient gitHubCl
4747 string defaultBranchName ,
4848 string branch ,
4949 string path ,
50- string content )
50+ string ? newPath ,
51+ string content )
5152 {
5253 if ( gitHubClient == null )
5354 {
@@ -71,25 +72,63 @@ public static async Task<string> GetFileContentAsync(this IGitHubClient gitHubCl
7172 }
7273 }
7374
75+ string pathForCreationOrNonExistent = newPath ?? path ;
76+
7477 try
7578 {
76- // Check if the file already exists
79+ // Check if the file already exists at the original 'path'
7780 var existingFile = await gitHubClient . Repository . Content . GetAllContentsByRef ( owner , repo , path , branch ) ;
7881
7982 if ( existingFile != null && existingFile . Count > 0 )
8083 {
81- var updateFileRequest = new UpdateFileRequest ( $ "Updating file { path } ", content , existingFile [ 0 ] . Sha , branch ) ;
82- return await gitHubClient . Repository . Content . UpdateFile ( owner , repo , path , updateFileRequest ) ;
84+ // File exists at 'path'
85+ if ( newPath != null && newPath != path )
86+ {
87+ // Path change requested: delete old file, then create new file
88+ var deleteFileRequest = new DeleteFileRequest (
89+ $ "Deleting old file at { path } to move to { newPath } ",
90+ existingFile [ 0 ] . Sha ,
91+ branch ) ;
92+ await gitHubClient . Repository . Content . DeleteFile ( owner , repo , path , deleteFileRequest ) ;
93+
94+ var createFileRequest = new CreateFileRequest (
95+ $ "Adding file { newPath } (moved from { path } )",
96+ content ,
97+ branch ) ;
98+ return await gitHubClient . Repository . Content . CreateFile ( owner , repo , newPath , createFileRequest ) ;
99+ }
100+ else
101+ {
102+ // Update in place (newPath is null or same as path)
103+ var updateFileRequest = new UpdateFileRequest (
104+ $ "Updating file { path } ",
105+ content ,
106+ existingFile [ 0 ] . Sha ,
107+ branch ) ;
108+ return await gitHubClient . Repository . Content . UpdateFile ( owner , repo , path , updateFileRequest ) ;
109+ }
110+ }
111+ else
112+ {
113+ // File does not exist at 'path' (e.g., GetAllContentsByRef returned empty list)
114+ // Create the file at 'pathForCreationOrNonExistent'
115+ var createFileRequest = new CreateFileRequest (
116+ $ "Adding file { pathForCreationOrNonExistent } ",
117+ content ,
118+ branch ) ;
119+ return await gitHubClient . Repository . Content . CreateFile ( owner , repo , pathForCreationOrNonExistent , createFileRequest ) ;
83120 }
84121 }
85122 catch ( NotFoundException )
86123 {
87- var createFileRequest = new CreateFileRequest ( $ "Adding file { path } ", content , branch ) ;
88-
89- return await gitHubClient . Repository . Content . CreateFile ( owner , repo , path , createFileRequest ) ;
124+ // File does not exist at 'path' (GetAllContentsByRef threw NotFoundException)
125+ // Create the file at 'pathForCreationOrNonExistent'
126+ var createFileRequest = new CreateFileRequest (
127+ $ "Adding file { pathForCreationOrNonExistent } ",
128+ content ,
129+ branch ) ;
130+ return await gitHubClient . Repository . Content . CreateFile ( owner , repo , pathForCreationOrNonExistent , createFileRequest ) ;
90131 }
91-
92- return null ;
93132 }
94133
95134 public static async Task < PullRequest > CreatePullRequestAsync ( this IGitHubClient gitHubClient , string owner , string repo , string headBranch , string baseBranch , string title , string body )
@@ -149,22 +188,19 @@ public static async Task<Dictionary<string, string>> GetCommitFilesWithContentAs
149188
150189 foreach ( var file in files )
151190 {
152- if ( file . Status == "added" || file . Status == "modified" ) // Only fetch content for added/modified files
191+ try
153192 {
154- try
155- {
156- // Fetch the raw content of the file
157- var rawContent = await gitHubClient . Repository . Content . GetRawContent ( owner , repo , file . Filename ) ;
158- var contentString = System . Text . Encoding . UTF8 . GetString ( rawContent ) ;
193+ // Fetch the raw content of the file
194+ var rawContent = await gitHubClient . Repository . Content . GetRawContent ( owner , repo , file . Filename ) ;
195+ var contentString = System . Text . Encoding . UTF8 . GetString ( rawContent ) ;
159196
160- // Add the file path and content to the dictionary
161- fileContents . Add ( file . Filename , contentString ) ;
162- }
163- catch ( NotFoundException )
164- {
165- // Handle cases where the file content is not accessible
166- fileContents . Add ( file . Filename , "Content not accessible" ) ;
167- }
197+ // Add the file path and content to the dictionary
198+ fileContents . Add ( file . Filename , contentString ) ;
199+ }
200+ catch ( NotFoundException )
201+ {
202+ // Handle cases where the file content is not accessible
203+ fileContents . Add ( file . Filename , "Content not accessible" ) ;
168204 }
169205 }
170206
0 commit comments