@@ -120,7 +120,13 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
120
120
f := expandFile (d )
121
121
122
122
// Check whether the file exists.
123
- _ , err := ghfileutils .GetFile (context .Background (), c .githubClient , f .repositoryOwner , f .repositoryName , f .branch , f .path )
123
+ fileContent , err := ghfileutils .GetFile (context .Background (),
124
+ c .githubClient ,
125
+ f .repositoryOwner ,
126
+ f .repositoryName ,
127
+ f .branch ,
128
+ f .path ,
129
+ )
124
130
if err != nil {
125
131
if err == ghfileutils .ErrNotFound {
126
132
return nil
@@ -129,24 +135,21 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
129
135
}
130
136
131
137
// Get the tree that corresponds to the target branch.
132
- s , err := branch .GetSHAForBranch (context .Background (), c . githubClient , f . repositoryOwner , f . repositoryName , f . branch )
133
- if err != nil {
134
- return err
135
- }
136
- oldTree , _ , err := c . githubClient . Git . GetTree ( context . Background (), f . repositoryOwner , f . repositoryName , s , true )
138
+ s , err := branch .GetSHAForBranch (context .Background (),
139
+ c . githubClient ,
140
+ f . repositoryOwner ,
141
+ f . repositoryName ,
142
+ f . branch )
137
143
if err != nil {
138
144
return err
139
145
}
140
146
141
- // Remove the target file from the list of entries for the new tree.
142
- // NOTE: Entries of type "tree" must be removed as well, otherwise deletion won't take place.
143
- newTree := make ([]* github.TreeEntry , 0 , len (oldTree .Entries ))
144
- for _ , entry := range oldTree .Entries {
145
- if * entry .Type != "tree" && * entry .Path != f .path {
146
- newTree = append (newTree , entry )
147
- }
148
- }
149
-
147
+ newTree := []* github.TreeEntry {{
148
+ SHA : nil , // delete the file
149
+ Path : fileContent .Path ,
150
+ Mode : github .String ("100644" ),
151
+ Type : github .String ("blob" ),
152
+ }}
150
153
// Create a commit based on the new tree.
151
154
if err := commit .CreateCommit (context .Background (), c .githubClient , & commit.CommitOptions {
152
155
RepoOwner : f .repositoryOwner ,
@@ -157,12 +160,13 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
157
160
Username : c .githubUsername ,
158
161
Email : c .githubEmail ,
159
162
Changes : newTree ,
160
- BaseTreeOverride : github . String ( "" ) ,
163
+ BaseTreeOverride : & s ,
161
164
PullRequestSourceBranchName : fmt .Sprintf ("terraform-provider-githubfile-%d" , time .Now ().UnixNano ()),
162
165
PullRequestBody : "" ,
163
166
MaxRetries : 3 ,
164
167
RetryBackoff : 5 * time .Second ,
165
- }); err != nil {
168
+ },
169
+ ); err != nil {
166
170
return fmt .Errorf ("failed to create commit: %v" , err )
167
171
}
168
172
return nil
@@ -179,7 +183,12 @@ func resourceFileRead(d *schema.ResourceData, m interface{}) error {
179
183
c := m .(* providerConfiguration )
180
184
f := expandFile (d )
181
185
182
- h , err := ghfileutils .GetFile (context .Background (), c .githubClient , f .repositoryOwner , f .repositoryName , f .branch , f .path )
186
+ h , err := ghfileutils .GetFile (context .Background (),
187
+ c .githubClient ,
188
+ f .repositoryOwner ,
189
+ f .repositoryName ,
190
+ f .branch ,
191
+ f .path )
183
192
if err == ghfileutils .ErrNotFound {
184
193
d .SetId ("" )
185
194
return nil
0 commit comments