Skip to content
Merged
2 changes: 1 addition & 1 deletion docs/resources/actions_environment_secret.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The following arguments are supported:

## Import

This resource can be imported using an ID made of the repository name, environment name (URL escaped), and secret name all separated by a `:`.
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 `:`.

~> **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.

Expand Down
17 changes: 11 additions & 6 deletions docs/resources/repository_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,26 @@ resource "github_repository_file" "foo" {
### Auto Created Branch

```terraform
resource "github_repository" "foo" {
name = "example"
resource "github_repository" "bar" {
name = "example2"
auto_init = true
}

resource "github_repository_file" "foo" {
repository = github_repository.foo.name
resource "github_branch" "bar" {
branch = "does/not/exist"
repository = github_repository.bar.name

}

resource "github_repository_file" "bar" {
repository = github_repository.bar.name
branch = "does/not/exist"
file = ".gitignore"
content = "**/*.tfstate"
commit_message = "Managed by Terraform"
commit_author = "Terraform User"
commit_email = "terraform@example.com"
overwrite_on_create = true
autocreate_branch = true
}
```

Expand Down Expand Up @@ -93,7 +98,7 @@ The following additional attributes are exported:

## Import

Repository files can be imported using a combination of the `repo`, `file` and `branch` or empty branch for the default branch, e.g.
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.

```sh
terraform import github_repository_file.gitignore example:.gitignore:feature-branch
Expand Down
8 changes: 5 additions & 3 deletions examples/dev.tfrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
provider_installation {
Comment thread
deiga marked this conversation as resolved.
dev_overrides {
"integrations/github" = "~/go/bin/"
"integrations/github" = "$HOME/go/bin/"
}

direct {}
}
direct {
exclude = ["registry.terraform.io/hashicorp/github"]
}
}
16 changes: 10 additions & 6 deletions examples/resources/repository_file/example_2.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@

resource "github_repository" "foo" {
name = "example"
resource "github_repository" "bar" {
name = "example2"
auto_init = true
}

resource "github_repository_file" "foo" {
repository = github_repository.foo.name
resource "github_branch" "bar" {
branch = "does/not/exist"
repository = github_repository.bar.name

}

resource "github_repository_file" "bar" {
repository = github_repository.bar.name
branch = "does/not/exist"
file = ".gitignore"
content = "**/*.tfstate"
commit_message = "Managed by Terraform"
commit_author = "Terraform User"
commit_email = "terraform@example.com"
overwrite_on_create = true
autocreate_branch = true
}

10 changes: 6 additions & 4 deletions github/resource_github_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

newResourceID, err := buildID(repo, file, branch)
newResourceID, err := buildID(repo, escapeIDPart(file), branch)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -392,7 +392,7 @@ func resourceGithubRepositoryFileUpdate(ctx context.Context, d *schema.ResourceD
}

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

func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
repo, filePath, branch, err := parseID3(d.Id())
repo, filePathPart, branch, err := parseID3(d.Id())
if err != nil {
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)
}

filePath := unescapeIDPart(filePathPart)

client := meta.(*Owner).v3client
owner := meta.(*Owner).name

Expand Down Expand Up @@ -468,7 +470,7 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
return nil, err
}

newResourceID, err := buildID(repo, filePath, branch)
newResourceID, err := buildID(repo, escapeIDPart(filePath), branch)
Comment thread
deiga marked this conversation as resolved.
if err != nil {
return nil, err
}
Expand Down
46 changes: 35 additions & 11 deletions github/resource_github_repository_file_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,62 @@ func resourceGithubRepositoryFileV0() *schema.Resource {
}

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

meta := m.(*Owner)
client := meta.v3client
owner := meta.name

repoName, ok := rawState["repository"].(string)
if !ok {
return nil, fmt.Errorf("repository not found or is not a string")
repoName := ""
if v, ok := rawState["repository"]; ok {
if s, ok := v.(string); ok && s != "" {
repoName = s
}
}
if repoName == "" {
return nil, fmt.Errorf("state upgrade v0: repository is not a string or not set")
}

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

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

// If branch is missing or empty, fetch the default branch from the repository
if branch, ok := rawState["branch"].(string); !ok || branch == "" {
rawState["branch"] = repo.GetDefaultBranch()
branch := ""
if v, ok := rawState["branch"]; ok {
if s, ok := v.(string); ok && s != "" {
branch = s
}
}

// If branch is empty, fetch the default branch from the repository
if branch == "" {
branch = repo.GetDefaultBranch()
rawState["branch"] = branch
}

filePath := ""
if v, ok := rawState["file"]; ok {
if s, ok := v.(string); ok && s != "" {
filePath = s
}
}

if filePath == "" {
return nil, fmt.Errorf("state upgrade v0: file path is not a string")
}

newResourceID, err := buildID(rawState["repository"].(string), rawState["file"].(string), rawState["branch"].(string))
newResourceID, err := buildID(repoName, escapeIDPart(filePath), branch)
if err != nil {
return nil, fmt.Errorf("failed to build ID: %w", err)
return nil, fmt.Errorf("state upgrade v0: failed to build ID: %w", err)
}
rawState["id"] = newResourceID

tflog.Debug(ctx, "GitHub Repository File State after migration", map[string]any{
tflog.Debug(ctx, "GitHub Repository File State after v0 migration", map[string]any{
"rawState": rawState,
})
return rawState, nil
Expand Down
Loading