Skip to content

Commit 251c33a

Browse files
committed
Uncouple Create and Update
Signed-off-by: Timo Sand <[email protected]>
1 parent cc6f24f commit 251c33a

1 file changed

Lines changed: 67 additions & 22 deletions

File tree

github/resource_github_team_settings.go

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"strconv"
77

8+
"github.com/hashicorp/terraform-plugin-log/tflog"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -73,27 +74,71 @@ func resourceGithubTeamSettings() *schema.Resource {
7374
}
7475
}
7576

76-
func resourceGithubTeamSettingsCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
77-
if err := checkOrganization(meta); err != nil {
77+
func resourceGithubTeamSettingsCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
78+
meta := m.(*Owner)
79+
if err := checkOrganization(m); err != nil {
7880
return diag.FromErr(err)
7981
}
82+
graphql := meta.v4client
8083

8184
teamIDString := d.Get("team_id").(string)
8285

86+
tflog.Debug(ctx, "Resolving team_id to Team node_id and slug", map[string]any{
87+
"team_id": teamIDString,
88+
})
8389
// Given a string that is either a team id or team slug, return the
8490
// get the basic details of the team including node_id and slug
85-
nodeId, slug, err := resolveTeamIDs(teamIDString, meta.(*Owner), ctx)
91+
nodeId, slug, err := resolveTeamIDs(teamIDString, meta, ctx)
8692
if err != nil {
8793
return diag.FromErr(err)
8894
}
95+
tflog.Trace(ctx, "Resolved team_id to Team node_id and slug", map[string]any{
96+
"node_id": nodeId,
97+
"slug": slug,
98+
})
8999
d.SetId(nodeId)
90100
if err = d.Set("team_slug", slug); err != nil {
91101
return diag.FromErr(err)
92102
}
93103
if err = d.Set("team_uid", nodeId); err != nil {
94104
return diag.FromErr(err)
95105
}
96-
return resourceGithubTeamSettingsUpdate(ctx, d, meta)
106+
107+
reviewRequestDelegation := d.Get("review_request_delegation").([]any)
108+
109+
var mutation struct {
110+
UpdateTeamReviewAssignment struct {
111+
ClientMutationId githubv4.ID `graphql:"clientMutationId"`
112+
} `graphql:"updateTeamReviewAssignment(input:$input)"`
113+
}
114+
115+
if len(reviewRequestDelegation) == 0 {
116+
tflog.Debug(ctx, "No review request delegation settings provided, disabling review request delegation", map[string]any{
117+
"team_id": d.Id(),
118+
"team_slug": slug,
119+
})
120+
121+
err := graphql.Mutate(ctx, &mutation, defaultTeamReviewAssignmentSettings(d.Id()), nil)
122+
if err != nil {
123+
return diag.FromErr(err)
124+
}
125+
} else {
126+
settings := reviewRequestDelegation[0].(map[string]any)
127+
128+
teamReviewAlgorithm := githubv4.TeamReviewAssignmentAlgorithm(settings["algorithm"].(string))
129+
updateTeamReviewAssignmentInput := githubv4.UpdateTeamReviewAssignmentInput{
130+
ID: d.Id(),
131+
Enabled: githubv4.Boolean(true),
132+
Algorithm: &teamReviewAlgorithm,
133+
TeamMemberCount: githubv4.NewInt(githubv4.Int(settings["member_count"].(int))),
134+
NotifyTeam: githubv4.NewBoolean(githubv4.Boolean(settings["notify"].(bool))),
135+
}
136+
err := graphql.Mutate(ctx, &mutation, updateTeamReviewAssignmentInput, nil)
137+
if err != nil {
138+
return diag.FromErr(err)
139+
}
140+
}
141+
return nil
97142
}
98143

99144
func resourceGithubTeamSettingsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
@@ -134,29 +179,30 @@ func resourceGithubTeamSettingsRead(ctx context.Context, d *schema.ResourceData,
134179
return nil
135180
}
136181

137-
func resourceGithubTeamSettingsUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
138-
if d.HasChange("review_request_delegation") || d.IsNewResource() {
139-
graphql := meta.(*Owner).v4client
140-
if setting := d.Get("review_request_delegation").([]any); len(setting) == 0 {
141-
var mutation struct {
142-
UpdateTeamReviewAssignment struct {
143-
ClientMutationId githubv4.ID `graphql:"clientMutationId"`
144-
} `graphql:"updateTeamReviewAssignment(input:$input)"`
145-
}
182+
func resourceGithubTeamSettingsUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
183+
if d.HasChange("review_request_delegation") {
184+
meta := m.(*Owner)
185+
graphql := meta.v4client
186+
reviewRequestDelegation := d.Get("review_request_delegation").([]any)
187+
188+
var mutation struct {
189+
UpdateTeamReviewAssignment struct {
190+
ClientMutationId githubv4.ID `graphql:"clientMutationId"`
191+
} `graphql:"updateTeamReviewAssignment(input:$input)"`
192+
}
193+
194+
if len(reviewRequestDelegation) == 0 {
195+
tflog.Debug(ctx, "No review request delegation settings provided, disabling review request delegation", map[string]any{
196+
"team_id": d.Id(),
197+
"team_slug": d.Get("team_slug").(string),
198+
})
146199

147200
err := graphql.Mutate(ctx, &mutation, defaultTeamReviewAssignmentSettings(d.Id()), nil)
148201
if err != nil {
149202
return diag.FromErr(err)
150203
}
151-
return nil
152204
} else {
153-
settings := d.Get("review_request_delegation").([]any)[0].(map[string]any)
154-
155-
var mutation struct {
156-
UpdateTeamReviewAssignment struct {
157-
ClientMutationId githubv4.ID `graphql:"clientMutationId"`
158-
} `graphql:"updateTeamReviewAssignment(input:$input)"`
159-
}
205+
settings := reviewRequestDelegation[0].(map[string]any)
160206

161207
teamReviewAlgorithm := githubv4.TeamReviewAssignmentAlgorithm(settings["algorithm"].(string))
162208
updateTeamReviewAssignmentInput := githubv4.UpdateTeamReviewAssignmentInput{
@@ -170,7 +216,6 @@ func resourceGithubTeamSettingsUpdate(ctx context.Context, d *schema.ResourceDat
170216
if err != nil {
171217
return diag.FromErr(err)
172218
}
173-
return nil
174219
}
175220
}
176221

0 commit comments

Comments
 (0)