Skip to content

Commit 2b6677b

Browse files
authored
Merge branch 'main' into say5/repo-custom-prop-empty-value-3358
2 parents 41d7f7a + f494cc6 commit 2b6677b

10 files changed

Lines changed: 283 additions & 174 deletions

docs/resources/actions_environment_secret.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ The following arguments are supported:
9090

9191
## Import
9292

93-
This resource can be imported using an ID made of the repository name, environment name (URL escaped), and secret name all separated by a `:`.
93+
This resource can be imported using an ID made of the repository name, environment name (any `:` in the environment name need to be escaped as `??`), and secret name all separated by a `:`.
9494

9595
~> **Note**: When importing secrets, the `value`, `value_encrypted`, `encrypted_value`, or `plaintext_value` fields will not be populated in the state. You may need to ignore changes for these as a workaround if you're not planning on updating the secret through Terraform.
9696

docs/resources/repository_file.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,26 @@ resource "github_repository_file" "foo" {
3535
### Auto Created Branch
3636

3737
```terraform
38-
resource "github_repository" "foo" {
39-
name = "example"
38+
resource "github_repository" "bar" {
39+
name = "example2"
4040
auto_init = true
4141
}
4242
43-
resource "github_repository_file" "foo" {
44-
repository = github_repository.foo.name
43+
resource "github_branch" "bar" {
44+
branch = "does/not/exist"
45+
repository = github_repository.bar.name
46+
47+
}
48+
49+
resource "github_repository_file" "bar" {
50+
repository = github_repository.bar.name
4551
branch = "does/not/exist"
4652
file = ".gitignore"
4753
content = "**/*.tfstate"
4854
commit_message = "Managed by Terraform"
4955
commit_author = "Terraform User"
5056
commit_email = "terraform@example.com"
5157
overwrite_on_create = true
52-
autocreate_branch = true
5358
}
5459
```
5560

@@ -93,7 +98,7 @@ The following additional attributes are exported:
9398

9499
## Import
95100

96-
Repository files can be imported using a combination of the `repo`, `file` and `branch` or empty branch for the default branch, e.g.
101+
Repository files can be imported using a combination of the `repo`, `file path` (any `:` in the file path need to be escaped as `??`) and `branch` or empty branch for the default branch, e.g.
97102

98103
```sh
99104
terraform import github_repository_file.gitignore example:.gitignore:feature-branch

examples/dev.tfrc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
provider_installation {
22
dev_overrides {
3-
"integrations/github" = "~/go/bin/"
3+
"integrations/github" = "$HOME/go/bin/"
44
}
55

6-
direct {}
7-
}
6+
direct {
7+
exclude = ["registry.terraform.io/hashicorp/github"]
8+
}
9+
}

examples/resources/repository_file/example_2.tf

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11

2-
resource "github_repository" "foo" {
3-
name = "example"
2+
resource "github_repository" "bar" {
3+
name = "example2"
44
auto_init = true
55
}
66

7-
resource "github_repository_file" "foo" {
8-
repository = github_repository.foo.name
7+
resource "github_branch" "bar" {
8+
branch = "does/not/exist"
9+
repository = github_repository.bar.name
10+
11+
}
12+
13+
resource "github_repository_file" "bar" {
14+
repository = github_repository.bar.name
915
branch = "does/not/exist"
1016
file = ".gitignore"
1117
content = "**/*.tfstate"
1218
commit_message = "Managed by Terraform"
1319
commit_author = "Terraform User"
1420
commit_email = "terraform@example.com"
1521
overwrite_on_create = true
16-
autocreate_branch = true
1722
}
18-

github/resource_github_repository_file.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
242242
return diag.FromErr(err)
243243
}
244244

245-
newResourceID, err := buildID(repo, file, branch)
245+
newResourceID, err := buildID(repo, escapeIDPart(file), branch)
246246
if err != nil {
247247
return diag.FromErr(err)
248248
}
@@ -392,7 +392,7 @@ func resourceGithubRepositoryFileUpdate(ctx context.Context, d *schema.ResourceD
392392
}
393393

394394
if d.HasChanges("repository", "file", "branch") {
395-
newResourceID, err := buildID(repo, file, branch)
395+
newResourceID, err := buildID(repo, escapeIDPart(file), branch)
396396
if err != nil {
397397
return diag.FromErr(err)
398398
}
@@ -433,11 +433,13 @@ func autoBranchDiffSuppressFunc(k, _, _ string, d *schema.ResourceData) bool {
433433
}
434434

435435
func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
436-
repo, filePath, branch, err := parseID3(d.Id())
436+
repo, filePathPart, branch, err := parseID3(d.Id())
437437
if err != nil {
438438
return nil, fmt.Errorf("invalid ID specified. Supplied ID must be written as <repository>:<file path>: (when branch is default) or <repository>:<file path>:<branch>. %w", err)
439439
}
440440

441+
filePath := unescapeIDPart(filePathPart)
442+
441443
client := meta.(*Owner).v3client
442444
owner := meta.(*Owner).name
443445

@@ -468,7 +470,7 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
468470
return nil, err
469471
}
470472

471-
newResourceID, err := buildID(repo, filePath, branch)
473+
newResourceID, err := buildID(repo, escapeIDPart(filePath), branch)
472474
if err != nil {
473475
return nil, err
474476
}

github/resource_github_repository_file_migration.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,38 +85,62 @@ func resourceGithubRepositoryFileV0() *schema.Resource {
8585
}
8686

8787
func resourceGithubRepositoryFileStateUpgradeV0(ctx context.Context, rawState map[string]any, m any) (map[string]any, error) {
88-
tflog.Debug(ctx, "GitHub Repository File State before migration", map[string]any{
88+
tflog.Debug(ctx, "GitHub Repository File State before v0 migration", map[string]any{
8989
"rawState": rawState,
9090
})
9191

9292
meta := m.(*Owner)
9393
client := meta.v3client
9494
owner := meta.name
9595

96-
repoName, ok := rawState["repository"].(string)
97-
if !ok {
98-
return nil, fmt.Errorf("repository not found or is not a string")
96+
repoName := ""
97+
if v, ok := rawState["repository"]; ok {
98+
if s, ok := v.(string); ok && s != "" {
99+
repoName = s
100+
}
101+
}
102+
if repoName == "" {
103+
return nil, fmt.Errorf("state upgrade v0: repository is not a string or not set")
99104
}
100105

101106
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
102107
if err != nil {
103-
return nil, fmt.Errorf("failed to retrieve repository '%s': %w", repoName, err)
108+
return nil, fmt.Errorf("state upgrade v0: failed to retrieve repository '%s': %w", repoName, err)
104109
}
105110

106111
rawState["repository_id"] = int(repo.GetID())
107112

108-
// If branch is missing or empty, fetch the default branch from the repository
109-
if branch, ok := rawState["branch"].(string); !ok || branch == "" {
110-
rawState["branch"] = repo.GetDefaultBranch()
113+
branch := ""
114+
if v, ok := rawState["branch"]; ok {
115+
if s, ok := v.(string); ok && s != "" {
116+
branch = s
117+
}
118+
}
119+
120+
// If branch is empty, fetch the default branch from the repository
121+
if branch == "" {
122+
branch = repo.GetDefaultBranch()
123+
rawState["branch"] = branch
124+
}
125+
126+
filePath := ""
127+
if v, ok := rawState["file"]; ok {
128+
if s, ok := v.(string); ok && s != "" {
129+
filePath = s
130+
}
131+
}
132+
133+
if filePath == "" {
134+
return nil, fmt.Errorf("state upgrade v0: file path is not a string")
111135
}
112136

113-
newResourceID, err := buildID(rawState["repository"].(string), rawState["file"].(string), rawState["branch"].(string))
137+
newResourceID, err := buildID(repoName, escapeIDPart(filePath), branch)
114138
if err != nil {
115-
return nil, fmt.Errorf("failed to build ID: %w", err)
139+
return nil, fmt.Errorf("state upgrade v0: failed to build ID: %w", err)
116140
}
117141
rawState["id"] = newResourceID
118142

119-
tflog.Debug(ctx, "GitHub Repository File State after migration", map[string]any{
143+
tflog.Debug(ctx, "GitHub Repository File State after v0 migration", map[string]any{
120144
"rawState": rawState,
121145
})
122146
return rawState, nil

0 commit comments

Comments
 (0)