forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_repository_file_migration.go
More file actions
123 lines (112 loc) · 2.97 KB
/
resource_github_repository_file_migration.go
File metadata and controls
123 lines (112 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package github
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubRepositoryFileV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"file": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"content": {
Type: schema.TypeString,
Required: true,
},
"branch": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ref": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"commit_sha": {
Type: schema.TypeString,
Computed: true,
},
"commit_message": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"commit_author": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"commit_email"},
},
"commit_email": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"commit_author"},
},
"sha": {
Type: schema.TypeString,
Computed: true,
},
"overwrite_on_create": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"autocreate_branch": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"autocreate_branch_source_branch": {
Type: schema.TypeString,
Default: "main",
Optional: true,
RequiredWith: []string{"autocreate_branch"},
},
"autocreate_branch_source_sha": {
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{"autocreate_branch"},
},
},
}
}
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{
"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")
}
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return nil, fmt.Errorf("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()
}
newResourceID, err := buildID(rawState["repository"].(string), rawState["file"].(string), rawState["branch"].(string))
if err != nil {
return nil, fmt.Errorf("failed to build ID: %w", err)
}
rawState["id"] = newResourceID
tflog.Debug(ctx, "GitHub Repository File State after migration", map[string]any{
"rawState": rawState,
})
return rawState, nil
}