Skip to content

Commit db068df

Browse files
committed
Add ID migration in the same bunch
Signed-off-by: Timo Sand <[email protected]>
1 parent 76e4f34 commit db068df

4 files changed

Lines changed: 17 additions & 10 deletions

github/resource_github_repository_file.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
234234
return diag.FromErr(err)
235235
}
236236

237-
d.SetId(fmt.Sprintf("%s/%s", repo, file))
237+
branch := d.Get("branch").(string)
238+
239+
d.SetId(fmt.Sprintf("%s/%s:%s", repo, file, branch))
238240
if err = d.Set("commit_sha", create.GetSHA()); err != nil {
239241
return diag.FromErr(err)
240242
}
@@ -484,7 +486,9 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
484486
return nil, fmt.Errorf("file %s is not a file in repository %s/%s or repository is not readable", file, owner, repo)
485487
}
486488

487-
d.SetId(fmt.Sprintf("%s/%s", repo, file))
489+
branch := d.Get("branch").(string)
490+
491+
d.SetId(fmt.Sprintf("%s/%s:%s", repo, file, branch))
488492
if err = d.Set("overwrite_on_create", false); err != nil {
489493
return nil, err
490494
}

github/resource_github_repository_file_migration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ func resourceGithubRepositoryFileStateUpgradeV0(ctx context.Context, rawState ma
108108
rawState["branch"] = repo.GetDefaultBranch()
109109
}
110110

111+
rawState["id"] = fmt.Sprintf("%s/%s:%s", rawState["repository"], rawState["file"], rawState["branch"])
112+
111113
tflog.Debug(ctx, "GitHub Repository File State after migration", map[string]any{
112114
"rawState": rawState,
113115
})

github/resource_github_repository_file_migration_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package github
22

33
import (
44
"context"
5-
"reflect"
65
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
78
)
89

910
func Test_resourceGithubRepositoryFileStateUpgradeV0(t *testing.T) {
@@ -28,7 +29,7 @@ func Test_resourceGithubRepositoryFileStateUpgradeV0(t *testing.T) {
2829
"overwrite_on_create": false,
2930
},
3031
want: map[string]any{
31-
"id": "test-repo/path/to/file.txt",
32+
"id": "test-repo/path/to/file.txt:main",
3233
"repository": "test-repo",
3334
"file": "path/to/file.txt",
3435
"content": "file content",
@@ -49,7 +50,7 @@ func Test_resourceGithubRepositoryFileStateUpgradeV0(t *testing.T) {
4950
"branch": "develop",
5051
},
5152
want: map[string]any{
52-
"id": "test-repo/README.md",
53+
"id": "test-repo/README.md:develop",
5354
"repository": "test-repo",
5455
"file": "README.md",
5556
"content": "# README",
@@ -67,7 +68,7 @@ func Test_resourceGithubRepositoryFileStateUpgradeV0(t *testing.T) {
6768
// "content": "file content",
6869
// },
6970
// want: map[string]any{
70-
// "id": "test-repo/path/to/file.txt",
71+
// "id": "test-repo/path/to/file.txt:main",
7172
// "repository": "test-repo",
7273
// "file": "path/to/file.txt",
7374
// "content": "file content",
@@ -86,7 +87,7 @@ func Test_resourceGithubRepositoryFileStateUpgradeV0(t *testing.T) {
8687
// "branch": "",
8788
// },
8889
// want: map[string]any{
89-
// "id": "test-repo/path/to/file.txt",
90+
// "id": "test-repo/path/to/file.txt:main",
9091
// "repository": "test-repo",
9192
// "file": "path/to/file.txt",
9293
// "content": "file content",
@@ -103,8 +104,8 @@ func Test_resourceGithubRepositoryFileStateUpgradeV0(t *testing.T) {
103104
t.Fatalf("unexpected error state: got error %v, shouldError %v", err, d.shouldError)
104105
}
105106

106-
if !d.shouldError && !reflect.DeepEqual(got, d.want) {
107-
t.Fatalf("got %+v, want %+v", got, d.want)
107+
if diff := cmp.Diff(got, d.want); diff != "" && !d.shouldError {
108+
t.Fatalf("got %+v, want %+v, diff %s", got, d.want, diff)
108109
}
109110
})
110111
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24.4
44

55
require (
66
github.com/go-jose/go-jose/v3 v3.0.4
7+
github.com/google/go-cmp v0.7.0
78
github.com/google/go-github/v82 v82.0.0
89
github.com/google/uuid v1.6.0
910
github.com/hashicorp/go-cty v1.5.0
@@ -21,7 +22,6 @@ require (
2122
github.com/cloudflare/circl v1.6.1 // indirect
2223
github.com/fatih/color v1.18.0 // indirect
2324
github.com/golang/protobuf v1.5.4 // indirect
24-
github.com/google/go-cmp v0.7.0 // indirect
2525
github.com/google/go-querystring v1.2.0 // indirect
2626
github.com/hashicorp/errwrap v1.0.0 // indirect
2727
github.com/hashicorp/go-checkpoint v0.5.0 // indirect

0 commit comments

Comments
 (0)