Skip to content

Commit 0bc0f50

Browse files
authored
Merge pull request #94 from alexalbr/issue/52
fix branch restriction error on 404 response
2 parents 89e5506 + 5a5fbeb commit 0bc0f50

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

bitbucket/resource_branch_restriction.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,16 @@ func resourceBranchRestrictionsRead(ctx context.Context, d *schema.ResourceData,
212212
brRes, res, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsIdGet(c.AuthContext, url.PathEscape(d.Id()),
213213
d.Get("repository").(string), d.Get("owner").(string))
214214

215-
if err != nil {
216-
return diag.FromErr(err)
217-
}
218-
219-
if res.StatusCode == http.StatusNotFound {
215+
if res != nil && res.StatusCode == http.StatusNotFound {
220216
log.Printf("[WARN] Branch Restrictions (%s) not found, removing from state", d.Id())
221217
d.SetId("")
222218
return nil
223219
}
224220

221+
if err != nil {
222+
return diag.FromErr(err)
223+
}
224+
225225
d.SetId(string(fmt.Sprintf("%v", brRes.Id)))
226226
d.Set("kind", brRes.Kind)
227227
d.Set("pattern", brRes.Pattern)
@@ -254,9 +254,14 @@ func resourceBranchRestrictionsDelete(ctx context.Context, d *schema.ResourceDat
254254
c := m.(Clients).genClient
255255
brApi := c.ApiClient.BranchRestrictionsApi
256256

257-
_, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsIdDelete(c.AuthContext, url.PathEscape(d.Id()),
257+
res, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsIdDelete(c.AuthContext, url.PathEscape(d.Id()),
258258
d.Get("repository").(string), d.Get("owner").(string))
259259

260+
if res != nil && res.StatusCode == http.StatusNotFound {
261+
log.Printf("[WARN] Branch Restrictions (%s) not found, removing from state", d.Id())
262+
return nil
263+
}
264+
260265
if err != nil {
261266
return diag.FromErr(err)
262267
}

0 commit comments

Comments
 (0)