-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix multiple bugs in handling of branch_types.enabled #205
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,10 @@ type BranchModel struct { | |
} | ||
|
||
type BranchType struct { | ||
Enabled bool `json:"enabled,omitempty"` | ||
// TRICKY: This is a pointer to a bool because the API omits the field if | ||
// it is true. json.Unmarshal treats a missing field as false, so we need to | ||
// handle this case explicitly. | ||
Enabled *bool `json:"enabled"` | ||
Kind string `json:"kind,omitempty"` | ||
Prefix string `json:"prefix,omitempty"` | ||
} | ||
|
@@ -181,7 +184,7 @@ func resourceBranchingModelsRead(ctx context.Context, d *schema.ResourceData, m | |
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
branchingModelsReq, _ := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/branching-model", owner, repo)) | ||
branchingModelsReq, _ := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/branching-model/settings", owner, repo)) | ||
|
||
if branchingModelsReq.StatusCode == http.StatusNotFound { | ||
log.Printf("[WARN] Branching Model (%s) not found, removing from state", d.Id()) | ||
|
@@ -206,6 +209,14 @@ func resourceBranchingModelsRead(ctx context.Context, d *schema.ResourceData, m | |
return diag.FromErr(decodeerr) | ||
} | ||
|
||
// Set default value for Enabled if it is nil | ||
for _, branchType := range branchingModel.BranchTypes { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the comment above (on |
||
if branchType.Enabled == nil { | ||
defaultTrue := true | ||
branchType.Enabled = &defaultTrue | ||
} | ||
} | ||
|
||
log.Printf("[DEBUG] Branching Model Response Decoded: %#v", branchingModel) | ||
|
||
d.Set("owner", owner) | ||
|
@@ -302,11 +313,6 @@ func flattenBranchModel(rp *BranchModel, typ string) []interface{} { | |
"name": rp.Name, | ||
} | ||
|
||
// if production branch is disabled it wont show up in response and will show up without the proerty if enabled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This workaround should no longer be needed now that we're using the |
||
if typ == "production" { | ||
m["enabled"] = true | ||
} | ||
|
||
return []interface{}{m} | ||
} | ||
|
||
|
@@ -333,7 +339,7 @@ func expandBranchTypes(tfList *schema.Set) []*BranchType { | |
} | ||
|
||
if v, ok := tfMap["enabled"].(bool); ok { | ||
bt.Enabled = v | ||
bt.Enabled = &v | ||
} | ||
|
||
branchTypes = append(branchTypes, bt) | ||
|
@@ -359,7 +365,7 @@ func flattenBranchTypes(branchTypes []*BranchType) []interface{} { | |
branchType := map[string]interface{}{ | ||
"kind": btRaw.Kind, | ||
"prefix": btRaw.Prefix, | ||
"enabled": true, | ||
"enabled": btRaw.Enabled, | ||
} | ||
|
||
tfList = append(tfList, branchType) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old code used the wrong API. The two APIs are similar, but the old one omits disabled branches in
branch_types
, and it omitsproduction
if disabled. Usingsettings
returns everything. Note that the code was already usingsettings
forPUT
operations. This change alignsGET
andPUT
to use the same URL.