Skip to content

Commit 9b9f8b8

Browse files
committed
fixup! fixup! fixup! Add computed repository_id field to resource
Signed-off-by: Timo Sand <[email protected]>
1 parent 581f684 commit 9b9f8b8

2 files changed

Lines changed: 55 additions & 99 deletions

File tree

github/resource_github_repository_file.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ func resourceGithubRepositoryFile() *schema.Resource {
3838
"repository": {
3939
Type: schema.TypeString,
4040
Required: true,
41-
ForceNew: true,
4241
Description: "The repository name",
4342
},
43+
"repository_id": {
44+
Type: schema.TypeInt,
45+
Computed: true,
46+
Description: "The repository ID",
47+
},
4448
"file": {
4549
Type: schema.TypeString,
4650
Required: true,
@@ -125,6 +129,7 @@ func resourceGithubRepositoryFile() *schema.Resource {
125129
DiffSuppressFunc: autoBranchDiffSuppressFunc,
126130
},
127131
},
132+
CustomizeDiff: diffRepository,
128133
}
129134
}
130135

@@ -171,6 +176,8 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
171176

172177
checkOpt := github.RepositoryContentGetOptions{}
173178

179+
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
180+
174181
if branch, ok := d.GetOk("branch"); ok {
175182
tflog.Debug(ctx, "Using explicitly set branch", map[string]any{
176183
"branch": branch.(string),
@@ -186,7 +193,6 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
186193
}
187194
checkOpt.Ref = branch.(string)
188195
} else {
189-
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
190196
if err != nil {
191197
return diag.FromErr(err)
192198
}
@@ -195,6 +201,10 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
195201
}
196202
}
197203

204+
if err := d.Set("repository_id", int(repoInfo.GetID())); err != nil {
205+
return diag.FromErr(err)
206+
}
207+
198208
opts := resourceGithubRepositoryFileOptions(d)
199209

200210
if opts.Message == nil {
@@ -465,13 +475,14 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
465475

466476
opts := &github.RepositoryContentGetOptions{}
467477

478+
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
468479
if branch != "" {
469480
opts.Ref = branch
470481
if err := d.Set("branch", branch); err != nil {
471482
return nil, err
472483
}
473484
} else {
474-
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
485+
475486
if err != nil {
476487
return nil, err
477488
}
@@ -481,6 +492,9 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
481492
return nil, err
482493
}
483494
}
495+
if err := d.Set("repository_id", int(repoInfo.GetID())); err != nil {
496+
return nil, err
497+
}
484498

485499
fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, filePath, opts)
486500
if err != nil {

github/resource_github_repository_file_test.go

Lines changed: 38 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
272272
t.Run("creates and manages files on auto created branch if branch does not exist", func(t *testing.T) {
273273
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
274274
repoName := fmt.Sprintf("%srepo-file-%s", testResourcePrefix, randomID)
275-
config := fmt.Sprintf(`
275+
config := `
276276
resource "github_repository" "test" {
277277
name = "%s"
278278
auto_init = true
@@ -287,53 +287,33 @@ func TestAccGithubRepositoryFile(t *testing.T) {
287287
commit_message = "Managed by Terraform"
288288
commit_author = "Terraform User"
289289
commit_email = "[email protected]"
290-
autocreate_branch = false
290+
autocreate_branch = %t
291291
}
292-
`, repoName)
293-
294-
check := resource.ComposeTestCheckFunc(
295-
resource.TestCheckResourceAttr(
296-
"github_repository_file.test", "content",
297-
"bar",
298-
),
299-
resource.TestCheckResourceAttr(
300-
"github_repository_file.test", "sha",
301-
"ba0e162e1c47469e3fe4b393a8bf8c569f302116",
302-
),
303-
resource.TestCheckResourceAttr(
304-
"github_repository_file.test", "ref",
305-
"does/not/exist",
306-
),
307-
resource.TestCheckResourceAttrSet(
308-
"github_repository_file.test", "commit_author",
309-
),
310-
resource.TestCheckResourceAttrSet(
311-
"github_repository_file.test", "commit_email",
312-
),
313-
resource.TestCheckResourceAttrSet(
314-
"github_repository_file.test", "commit_message",
315-
),
316-
resource.TestCheckResourceAttrSet(
317-
"github_repository_file.test", "commit_sha",
318-
),
319-
resource.TestCheckResourceAttr("github_repository_file.test", "autocreate_branch", "true"),
320-
resource.TestCheckResourceAttr("github_repository_file.test", "autocreate_branch_source_branch", "main"),
321-
resource.TestCheckResourceAttrSet("github_repository_file.test", "autocreate_branch_source_sha"),
322-
)
292+
`
323293

324294
resource.Test(t, resource.TestCase{
325295
PreCheck: func() { skipUnauthenticated(t) },
326296
ProviderFactories: providerFactories,
327297
Steps: []resource.TestStep{
328298
{
329-
Config: config,
299+
Config: fmt.Sprintf(config, repoName, false),
330300
ExpectError: regexp.MustCompile(`unexpected status code: 404 Not Found`),
331301
},
332302
{
333-
Config: strings.Replace(config,
334-
"autocreate_branch = false",
335-
"autocreate_branch = true", 1),
336-
Check: check,
303+
Config: fmt.Sprintf(config, repoName, true),
304+
Check: resource.ComposeTestCheckFunc(
305+
resource.TestCheckResourceAttr("github_repository_file.test", "content", "bar"),
306+
resource.TestCheckResourceAttr("github_repository_file.test", "sha", "ba0e162e1c47469e3fe4b393a8bf8c569f302116"),
307+
resource.TestCheckResourceAttr("github_repository_file.test", "ref", "does/not/exist"),
308+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_author"),
309+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_email"),
310+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_message"),
311+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_sha"),
312+
resource.TestCheckResourceAttr("github_repository_file.test", "autocreate_branch", "true"),
313+
resource.TestCheckResourceAttr("github_repository_file.test", "autocreate_branch_source_branch", "main"),
314+
resource.TestCheckResourceAttrSet("github_repository_file.test", "autocreate_branch_source_sha"),
315+
resource.TestCheckResourceAttrSet("github_repository_file.test", "repository_id"),
316+
),
337317
},
338318
},
339319
})
@@ -371,19 +351,13 @@ func TestAccGithubRepositoryFile(t *testing.T) {
371351
{
372352
Config: config,
373353
Check: resource.ComposeTestCheckFunc(
374-
resource.TestCheckResourceAttr(
375-
"github_repository_file.test", "file",
376-
"archived-test.md",
377-
),
354+
resource.TestCheckResourceAttr("github_repository_file.test", "file", "archived-test.md"),
378355
),
379356
},
380357
{
381358
Config: archivedConfig,
382359
Check: resource.ComposeTestCheckFunc(
383-
resource.TestCheckResourceAttr(
384-
"github_repository.test", "archived",
385-
"true",
386-
),
360+
resource.TestCheckResourceAttr("github_repository.test", "archived", "true"),
387361
),
388362
},
389363
// This step should succeed - the file should be removed from state
@@ -426,34 +400,18 @@ func TestAccGithubRepositoryFile(t *testing.T) {
426400
Steps: []resource.TestStep{
427401
{
428402
Config: config,
429-
Check: resource.ComposeTestCheckFunc(
430-
resource.TestCheckResourceAttr(
431-
"github_repository_file.test", "content",
432-
"bar",
433-
),
434-
resource.TestCheckResourceAttr(
435-
"github_repository_file.test", "sha",
436-
"ba0e162e1c47469e3fe4b393a8bf8c569f302116",
437-
),
438-
resource.TestCheckResourceAttr(
439-
"github_repository_file.test", "ref",
440-
"main",
441-
),
442-
resource.TestCheckResourceAttrSet(
443-
"github_repository_file.test", "commit_author",
444-
),
445-
resource.TestCheckResourceAttrSet(
446-
"github_repository_file.test", "commit_email",
447-
),
448-
resource.TestCheckResourceAttrSet(
449-
"github_repository_file.test", "commit_message",
450-
),
451-
resource.TestCheckResourceAttrSet(
452-
"github_repository_file.test", "commit_sha",
453-
),
403+
Check: resource.ComposeAggregateTestCheckFunc(
404+
resource.TestCheckResourceAttr("github_repository_file.test", "content", "bar"),
405+
resource.TestCheckResourceAttrSet("github_repository_file.test", "sha"),
406+
resource.TestCheckResourceAttr("github_repository_file.test", "ref", "main"),
407+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_author"),
408+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_email"),
409+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_message"),
410+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_sha"),
454411
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
455412
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
456413
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
414+
resource.TestCheckResourceAttrSet("github_repository_file.test", "repository_id"),
457415
),
458416
},
459417
{
@@ -492,34 +450,18 @@ func TestAccGithubRepositoryFile(t *testing.T) {
492450
Steps: []resource.TestStep{
493451
{
494452
Config: config,
495-
Check: resource.ComposeTestCheckFunc(
496-
resource.TestCheckResourceAttr(
497-
"github_repository_file.test", "content",
498-
"bar",
499-
),
500-
resource.TestCheckResourceAttr(
501-
"github_repository_file.test", "sha",
502-
"ba0e162e1c47469e3fe4b393a8bf8c569f302116",
503-
),
504-
resource.TestCheckResourceAttr(
505-
"github_repository_file.test", "ref",
506-
"main",
507-
),
508-
resource.TestCheckResourceAttrSet(
509-
"github_repository_file.test", "commit_author",
510-
),
511-
resource.TestCheckResourceAttrSet(
512-
"github_repository_file.test", "commit_email",
513-
),
514-
resource.TestCheckResourceAttrSet(
515-
"github_repository_file.test", "commit_message",
516-
),
517-
resource.TestCheckResourceAttrSet(
518-
"github_repository_file.test", "commit_sha",
519-
),
453+
Check: resource.ComposeAggregateTestCheckFunc(
454+
resource.TestCheckResourceAttr("github_repository_file.test", "content", "bar"),
455+
resource.TestCheckResourceAttrSet("github_repository_file.test", "sha"),
456+
resource.TestCheckResourceAttr("github_repository_file.test", "ref", "main"),
457+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_author"),
458+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_email"),
459+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_message"),
460+
resource.TestCheckResourceAttrSet("github_repository_file.test", "commit_sha"),
520461
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
521462
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
522463
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
464+
resource.TestCheckResourceAttrSet("github_repository_file.test", "repository_id"),
523465
),
524466
},
525467
{

0 commit comments

Comments
 (0)