Skip to content

Commit 84d4e36

Browse files
committed
fixup! fix: Stop repo collaborators drifting on owner
1 parent 8c87c0b commit 84d4e36

3 files changed

Lines changed: 55 additions & 30 deletions

File tree

docs/resources/repository_collaborators.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,14 @@ resource "github_repository_collaborators" "some_repo_collaborators" {
8888
- `repository_id` (Number) ID of the repository.
8989

9090
<a id="nestedblock--ignore_team"></a>
91-
9291
### Nested Schema for `ignore_team`
9392

9493
Required:
9594

9695
- `team_id` (String) ID or slug of the team to ignore.
9796

98-
<a id="nestedblock--team"></a>
9997

98+
<a id="nestedblock--team"></a>
10099
### Nested Schema for `team`
101100

102101
Required:
@@ -107,8 +106,8 @@ Optional:
107106

108107
- `permission` (String) Permission to grant to the team. Must be one of `pull`, `triage`, `push`, `maintain`, `admin` or the name of an existing [custom repository role](https://docs.github.com/en/enterprise-cloud@latest/organizations/managing-peoples-access-to-your-organization-with-roles/managing-custom-repository-roles-for-an-organization) within the organization. Defaults to `push`.
109108

110-
<a id="nestedblock--user"></a>
111109

110+
<a id="nestedblock--user"></a>
112111
### Nested Schema for `user`
113112

114113
Required:

github/resource_github_repository_collaborators.go

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,32 @@ func resourceGithubRepositoryCollaborators() *schema.Resource {
133133
func resourceGithubRepositoryCollaboratorsDiff(ctx context.Context, d *schema.ResourceDiff, m any) error {
134134
tflog.Debug(ctx, "Diffing collaborators")
135135

136-
meta := m.(*Owner)
136+
meta, _ := m.(*Owner)
137137

138138
if d.HasChange("user") {
139-
users := d.Get("user").(*schema.Set).List()
139+
users, ok := d.Get("user").(*schema.Set)
140+
if !ok {
141+
return fmt.Errorf("error reading user config")
142+
}
143+
140144
seen := make(map[string]any)
145+
for _, u := range users.List() {
146+
user, ok := u.(map[string]any)
147+
if !ok {
148+
return fmt.Errorf("error reading user config")
149+
}
141150

142-
for _, u := range users {
143-
user := u.(map[string]any)
144-
username := strings.ToLower(user["username"].(string))
151+
usernameVal, ok := user["username"]
152+
if !ok {
153+
return fmt.Errorf("error reading user config")
154+
}
145155

156+
username, ok := usernameVal.(string)
157+
if !ok {
158+
return fmt.Errorf("error reading user config")
159+
}
160+
161+
username = strings.ToLower(username)
146162
if _, ok := seen[username]; ok {
147163
return fmt.Errorf("duplicate username %s found in user collaborators", username)
148164
}
@@ -188,30 +204,32 @@ func resourceGithubRepositoryCollaboratorsDiff(ctx context.Context, d *schema.Re
188204
// the owner is included in the list of users.
189205

190206
ownerConfigured := false
191-
owner := strings.ToLower(meta.name)
207+
owner := meta.name
192208

193-
usersVal := d.Get("user")
194-
if users, ok := usersVal.(*schema.Set); ok {
195-
for _, u := range users.List() {
196-
user, ok := u.(map[string]any)
197-
if !ok {
198-
continue
199-
}
209+
users, ok := d.Get("user").(*schema.Set)
210+
if !ok {
211+
return fmt.Errorf("error reading user config")
212+
}
200213

201-
usernameVal, ok := user["username"]
202-
if !ok {
203-
continue
204-
}
214+
for _, u := range users.List() {
215+
user, ok := u.(map[string]any)
216+
if !ok {
217+
return fmt.Errorf("error reading user config")
218+
}
205219

206-
username, ok := usernameVal.(string)
207-
if !ok {
208-
continue
209-
}
220+
usernameVal, ok := user["username"]
221+
if !ok {
222+
return fmt.Errorf("error reading user config")
223+
}
210224

211-
if strings.ToLower(username) == owner {
212-
ownerConfigured = true
213-
break
214-
}
225+
username, ok := usernameVal.(string)
226+
if !ok {
227+
return fmt.Errorf("error reading user config")
228+
}
229+
230+
if strings.EqualFold(username, owner) {
231+
ownerConfigured = true
232+
break
215233
}
216234
}
217235

@@ -276,6 +294,10 @@ func resourceGithubRepositoryCollaboratorsCreate(ctx context.Context, d *schema.
276294
if !ownerConfigured {
277295
inIgnoreUsers = append(inIgnoreUsers, strings.ToLower(owner))
278296
}
297+
298+
if !ownerConfigured {
299+
inIgnoreUsers = append(inIgnoreUsers, strings.ToLower(owner))
300+
}
279301
}
280302
}
281303

@@ -421,6 +443,10 @@ func resourceGithubRepositoryCollaboratorsUpdate(ctx context.Context, d *schema.
421443
if !ownerConfigured {
422444
inIgnoreUsers = append(inIgnoreUsers, strings.ToLower(owner))
423445
}
446+
447+
if err := d.Set("owner_configured", ownerConfigured); err != nil {
448+
return diag.FromErr(err)
449+
}
424450
}
425451
}
426452

github/resource_github_repository_collaborators_migration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func resourceGithubRepositoryCollaboratorsStateUpgradeV1(_ context.Context, rawS
207207
// is included in the list of users.
208208

209209
ownerConfigured := false
210-
owner := strings.ToLower(meta.name)
210+
owner := meta.name
211211

212212
if usersVal, ok := rawState["user"]; ok {
213213
if users, ok := usersVal.([]any); ok {
@@ -227,7 +227,7 @@ func resourceGithubRepositoryCollaboratorsStateUpgradeV1(_ context.Context, rawS
227227
continue
228228
}
229229

230-
if strings.ToLower(username) == owner {
230+
if strings.EqualFold(username, owner) {
231231
ownerConfigured = true
232232
break
233233
}

0 commit comments

Comments
 (0)