@@ -177,12 +177,17 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
177177 checkOpt := github.RepositoryContentGetOptions {}
178178
179179 repoInfo , _ , err := client .Repositories .Get (ctx , owner , repo )
180+ if err != nil {
181+ return diag .FromErr (err )
182+ }
183+ var branch string
180184
181- if branch , ok := d .GetOk ("branch" ); ok {
185+ if branchFieldVal , ok := d .GetOk ("branch" ); ok {
186+ branch = branchFieldVal .(string )
182187 tflog .Debug (ctx , "Using explicitly set branch" , map [string ]any {
183- "branch" : branch .( string ) ,
188+ "branch" : branch ,
184189 })
185- if err := checkRepositoryBranchExists (client , owner , repo , branch .( string ) ); err != nil {
190+ if err := checkRepositoryBranchExists (ctx , client , owner , repo , branch ); err != nil {
186191 if d .Get ("autocreate_branch" ).(bool ) {
187192 if err := resourceGithubRepositoryFileCreateBranch (ctx , d , meta ); err != nil {
188193 return diag .FromErr (err )
@@ -191,14 +196,9 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
191196 return diag .FromErr (err )
192197 }
193198 }
194- checkOpt .Ref = branch .( string )
199+ checkOpt .Ref = branch
195200 } else {
196- if err != nil {
197- return diag .FromErr (err )
198- }
199- if err := d .Set ("branch" , repoInfo .GetDefaultBranch ()); err != nil {
200- return diag .FromErr (err )
201- }
201+ branch = repoInfo .GetDefaultBranch ()
202202 }
203203
204204 if err := d .Set ("repository_id" , int (repoInfo .GetID ())); err != nil {
@@ -243,12 +243,17 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
243243 return diag .FromErr (err )
244244 }
245245
246- branch := d .Get ("branch" ).(string )
246+ if err := d .Set ("branch" , branch ); err != nil {
247+ return diag .FromErr (err )
248+ }
247249
248250 newResourceID , err := buildID (repo , file , branch )
249251 if err != nil {
250252 return diag .FromErr (err )
251253 }
254+ tflog .Debug (ctx , "Setting ID" , map [string ]any {
255+ "id" : newResourceID ,
256+ })
252257 d .SetId (newResourceID )
253258 if err = d .Set ("commit_sha" , create .GetSHA ()); err != nil {
254259 return diag .FromErr (err )
@@ -261,34 +266,34 @@ func resourceGithubRepositoryFileRead(ctx context.Context, d *schema.ResourceDat
261266 client := meta .(* Owner ).v3client
262267 owner := meta .(* Owner ).name
263268
264- repo , file , _ , err := parseID3 (d .Id ())
265- if err != nil {
266- return diag .FromErr (err )
269+ repoName , ok := d .Get ("repository" ).(string )
270+ if ! ok {
271+ return diag .FromErr (fmt .Errorf ("repository not found or is not a string" ))
272+ }
273+ file , ok := d .Get ("file" ).(string )
274+ if ! ok {
275+ return diag .FromErr (fmt .Errorf ("file not found or is not a string" ))
267276 }
268277
269- ctx = tflog .SetField (ctx , "repository" , repo )
278+ ctx = tflog .SetField (ctx , "repository" , repoName )
270279 ctx = tflog .SetField (ctx , "file" , file )
271280 ctx = tflog .SetField (ctx , "owner" , owner )
272281
273282 opts := & github.RepositoryContentGetOptions {}
274283
275- if branch , ok := d .GetOk ("branch" ); ok {
276- tflog .Debug (ctx , "Using explicitly set branch" , map [string ]any {
277- "branch" : branch .(string ),
278- })
279- if err := checkRepositoryBranchExists (client , owner , repo , branch .(string )); err != nil {
280- if d .Get ("autocreate_branch" ).(bool ) {
281- branch = d .Get ("autocreate_branch_source_branch" ).(string )
282- } else {
283- tflog .Info (ctx , "Removing repository path from state because the branch no longer exists in GitHub" )
284- d .SetId ("" )
285- return nil
286- }
284+ branch := d .Get ("branch" ).(string )
285+ if err := checkRepositoryBranchExists (ctx , client , owner , repoName , branch ); err != nil {
286+ if d .Get ("autocreate_branch" ).(bool ) {
287+ branch = d .Get ("autocreate_branch_source_branch" ).(string )
288+ } else {
289+ tflog .Info (ctx , "Removing repository path from state because the branch no longer exists in GitHub" )
290+ d .SetId ("" )
291+ return nil
287292 }
288- opts .Ref = branch .(string )
289293 }
294+ opts .Ref = branch
290295
291- fc , _ , _ , err := client .Repositories .GetContents (ctx , owner , repo , file , opts )
296+ fc , _ , _ , err := client .Repositories .GetContents (ctx , owner , repoName , file , opts )
292297 if err != nil {
293298 var errorResponse * github.ErrorResponse
294299 if errors .As (err , & errorResponse ) && errorResponse .Response .StatusCode == http .StatusTooManyRequests {
@@ -309,12 +314,7 @@ func resourceGithubRepositoryFileRead(ctx context.Context, d *schema.ResourceDat
309314 if err = d .Set ("content" , content ); err != nil {
310315 return diag .FromErr (err )
311316 }
312- if err = d .Set ("repository" , repo ); err != nil {
313- return diag .FromErr (err )
314- }
315- if err = d .Set ("file" , file ); err != nil {
316- return diag .FromErr (err )
317- }
317+
318318 if err = d .Set ("sha" , fc .GetSHA ()); err != nil {
319319 return diag .FromErr (err )
320320 }
@@ -339,10 +339,10 @@ func resourceGithubRepositoryFileRead(ctx context.Context, d *schema.ResourceDat
339339 tflog .Debug (ctx , "Using known commit SHA" , map [string ]any {
340340 "commit_sha" : sha .(string ),
341341 })
342- commit , _ , err = client .Repositories .GetCommit (ctx , owner , repo , sha .(string ), nil )
342+ commit , _ , err = client .Repositories .GetCommit (ctx , owner , repoName , sha .(string ), nil )
343343 } else {
344344 tflog .Debug (ctx , "Commit SHA unknown for file, looking for commit" )
345- commit , err = getFileCommit (ctx , client , owner , repo , file , ref )
345+ commit , err = getFileCommit (ctx , client , owner , repoName , file , ref )
346346 }
347347 if err != nil {
348348 return diag .FromErr (err )
@@ -387,18 +387,15 @@ func resourceGithubRepositoryFileUpdate(ctx context.Context, d *schema.ResourceD
387387 ctx = tflog .SetField (ctx , "file" , file )
388388 ctx = tflog .SetField (ctx , "owner" , owner )
389389
390- if branch , ok := d .GetOk ("branch" ); ok {
391- tflog .Debug (ctx , "Using explicitly set branch" , map [string ]any {
392- "branch" : branch .(string ),
393- })
394- if err := checkRepositoryBranchExists (client , owner , repo , branch .(string )); err != nil {
395- if d .Get ("autocreate_branch" ).(bool ) {
396- if err := resourceGithubRepositoryFileCreateBranch (ctx , d , meta ); err != nil {
397- return diag .FromErr (err )
398- }
399- } else {
390+ branch := d .Get ("branch" ).(string )
391+
392+ if err := checkRepositoryBranchExists (ctx , client , owner , repo , branch ); err != nil {
393+ if d .Get ("autocreate_branch" ).(bool ) {
394+ if err := resourceGithubRepositoryFileCreateBranch (ctx , d , meta ); err != nil {
400395 return diag .FromErr (err )
401396 }
397+ } else {
398+ return diag .FromErr (err )
402399 }
403400 }
404401
@@ -433,22 +430,18 @@ func resourceGithubRepositoryFileDelete(ctx context.Context, d *schema.ResourceD
433430 opts .Message = github .Ptr (fmt .Sprintf ("Delete %s" , file ))
434431 }
435432
436- if b , ok := d .GetOk ("branch" ); ok {
437- tflog .Debug (ctx , "Using explicitly set branch" , map [string ]any {
438- "branch" : b .(string ),
439- })
440- if err := checkRepositoryBranchExists (client , owner , repo , b .(string )); err != nil {
441- if d .Get ("autocreate_branch" ).(bool ) {
442- if err := resourceGithubRepositoryFileCreateBranch (ctx , d , meta ); err != nil {
443- return diag .FromErr (err )
444- }
445- } else {
433+ branch := d .Get ("branch" ).(string )
434+
435+ if err := checkRepositoryBranchExists (ctx , client , owner , repo , branch ); err != nil {
436+ if d .Get ("autocreate_branch" ).(bool ) {
437+ if err := resourceGithubRepositoryFileCreateBranch (ctx , d , meta ); err != nil {
446438 return diag .FromErr (err )
447439 }
440+ } else {
441+ return diag .FromErr (err )
448442 }
449- branch := b .(string )
450- opts .Branch = github .Ptr (branch )
451443 }
444+ opts .Branch = github .Ptr (branch )
452445
453446 _ , _ , err := client .Repositories .DeleteFile (ctx , owner , repo , file , opts )
454447 return diag .FromErr (handleArchivedRepoDelete (err , "repository file" , file , owner , repo ))
@@ -476,16 +469,16 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
476469 opts := & github.RepositoryContentGetOptions {}
477470
478471 repoInfo , _ , err := client .Repositories .Get (ctx , owner , repo )
472+ if err != nil {
473+ return nil , err
474+ }
479475 if branch != "" {
480476 opts .Ref = branch
481477 if err := d .Set ("branch" , branch ); err != nil {
482478 return nil , err
483479 }
484480 } else {
485481
486- if err != nil {
487- return nil , err
488- }
489482 defaultBranch := repoInfo .GetDefaultBranch ()
490483 branch = defaultBranch
491484 if err := d .Set ("branch" , branch ); err != nil {
@@ -504,10 +497,20 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
504497 return nil , fmt .Errorf ("filePath %s is not a file in repository %s/%s or repository is not readable" , filePath , owner , repo )
505498 }
506499
500+ if err := d .Set ("repository" , repo ); err != nil {
501+ return nil , err
502+ }
503+ if err := d .Set ("file" , filePath ); err != nil {
504+ return nil , err
505+ }
506+
507507 newResourceID , err := buildID (repo , filePath , branch )
508508 if err != nil {
509509 return nil , err
510510 }
511+ tflog .Debug (ctx , "Setting ID" , map [string ]any {
512+ "id" : newResourceID ,
513+ })
511514 d .SetId (newResourceID )
512515 if err = d .Set ("overwrite_on_create" , false ); err != nil {
513516 return nil , err
0 commit comments