@@ -201,10 +201,6 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
201201 branch = repoInfo .GetDefaultBranch ()
202202 }
203203
204- if err := d .Set ("repository_id" , int (repoInfo .GetID ())); err != nil {
205- return diag .FromErr (err )
206- }
207-
208204 opts := resourceGithubRepositoryFileOptions (d )
209205
210206 if opts .Message == nil {
@@ -243,10 +239,6 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
243239 return diag .FromErr (err )
244240 }
245241
246- if err := d .Set ("branch" , branch ); err != nil {
247- return diag .FromErr (err )
248- }
249-
250242 newResourceID , err := buildID (repo , file , branch )
251243 if err != nil {
252244 return diag .FromErr (err )
@@ -255,9 +247,17 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
255247 "id" : newResourceID ,
256248 })
257249 d .SetId (newResourceID )
250+
251+ // Set computed values after the resource is created and in state
258252 if err = d .Set ("commit_sha" , create .GetSHA ()); err != nil {
259253 return diag .FromErr (err )
260254 }
255+ if err := d .Set ("branch" , branch ); err != nil {
256+ return diag .FromErr (err )
257+ }
258+ if err := d .Set ("repository_id" , int (repoInfo .GetID ())); err != nil {
259+ return diag .FromErr (err )
260+ }
261261
262262 return resourceGithubRepositoryFileRead (ctx , d , meta )
263263}
@@ -266,22 +266,17 @@ func resourceGithubRepositoryFileRead(ctx context.Context, d *schema.ResourceDat
266266 client := meta .(* Owner ).v3client
267267 owner := meta .(* Owner ).name
268268
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" ))
276- }
269+ repoName := d .Get ("repository" ).(string )
270+ file := d .Get ("file" ).(string )
271+ branch := d .Get ("branch" ).(string )
277272
278273 ctx = tflog .SetField (ctx , "repository" , repoName )
279274 ctx = tflog .SetField (ctx , "file" , file )
280275 ctx = tflog .SetField (ctx , "owner" , owner )
276+ ctx = tflog .SetField (ctx , "owner" , owner )
281277
282278 opts := & github.RepositoryContentGetOptions {}
283279
284- branch := d .Get ("branch" ).(string )
285280 if err := checkRepositoryBranchExists (ctx , client , owner , repoName , branch ); err != nil {
286281 if d .Get ("autocreate_branch" ).(bool ) {
287282 branch = d .Get ("autocreate_branch_source_branch" ).(string )
@@ -405,6 +400,7 @@ func resourceGithubRepositoryFileUpdate(ctx context.Context, d *schema.ResourceD
405400 opts .Message = github .Ptr (fmt .Sprintf ("Update %s" , file ))
406401 }
407402
403+ // TODO: Use UpdateFile if the file already exists
408404 create , _ , err := client .Repositories .CreateFile (ctx , owner , repo , file , opts )
409405 if err != nil {
410406 return diag .FromErr (err )
@@ -414,6 +410,14 @@ func resourceGithubRepositoryFileUpdate(ctx context.Context, d *schema.ResourceD
414410 return diag .FromErr (err )
415411 }
416412
413+ if d .HasChanges ("repository" , "file" , "branch" ) {
414+ newResourceID , err := buildID (repo , file , branch )
415+ if err != nil {
416+ return diag .FromErr (err )
417+ }
418+ d .SetId (newResourceID )
419+ }
420+
417421 return resourceGithubRepositoryFileRead (ctx , d , meta )
418422}
419423
@@ -472,23 +476,12 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
472476 if err != nil {
473477 return nil , err
474478 }
475- if branch != "" {
476- opts .Ref = branch
477- if err := d .Set ("branch" , branch ); err != nil {
478- return nil , err
479- }
480- } else {
481-
482- defaultBranch := repoInfo .GetDefaultBranch ()
483- branch = defaultBranch
484- if err := d .Set ("branch" , branch ); err != nil {
485- return nil , err
486- }
487- }
488- if err := d .Set ("repository_id" , int (repoInfo .GetID ())); err != nil {
489- return nil , err
479+ if branch == "" {
480+ branch = repoInfo .GetDefaultBranch ()
490481 }
491482
483+ opts .Ref = branch
484+
492485 fc , _ , _ , err := client .Repositories .GetContents (ctx , owner , repo , filePath , opts )
493486 if err != nil {
494487 return nil , err
@@ -512,6 +505,13 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
512505 "id" : newResourceID ,
513506 })
514507 d .SetId (newResourceID )
508+
509+ if err := d .Set ("branch" , branch ); err != nil {
510+ return nil , err
511+ }
512+ if err := d .Set ("repository_id" , int (repoInfo .GetID ())); err != nil {
513+ return nil , err
514+ }
515515 if err = d .Set ("overwrite_on_create" , false ); err != nil {
516516 return nil , err
517517 }
0 commit comments