This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresource_github_repository_collaborator.go
208 lines (175 loc) · 5.35 KB
/
resource_github_repository_collaborator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package github
import (
"context"
"fmt"
"log"
"strings"
"github.com/google/go-github/v28/github"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGithubRepositoryCollaborator() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryCollaboratorCreate,
Read: resourceGithubRepositoryCollaboratorRead,
Delete: resourceGithubRepositoryCollaboratorDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
// editing repository collaborators are not supported by github api so forcing new on any changes
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: caseInsensitive(),
},
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"permission": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "push",
ValidateFunc: validateValueFunc([]string{"pull", "triage", "push", "maintain", "admin"}),
},
"invitation_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGithubRepositoryCollaboratorCreate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Organization).client
orgName := meta.(*Organization).name
username := d.Get("username").(string)
repoName := d.Get("repository").(string)
ctx := context.Background()
log.Printf("[DEBUG] Creating repository collaborator: %s (%s/%s)",
username, orgName, repoName)
_, err = client.Repositories.AddCollaborator(ctx,
orgName,
repoName,
username,
&github.RepositoryAddCollaboratorOptions{
Permission: d.Get("permission").(string),
})
if err != nil {
return err
}
d.SetId(buildTwoPartID(&repoName, &username))
return resourceGithubRepositoryCollaboratorRead(d, meta)
}
func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Organization).client
orgName := meta.(*Organization).name
repoName, username, err := parseTwoPartID(d.Id())
if err != nil {
return err
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
// First, check if the user has been invited but has not yet accepted
invitation, err := findRepoInvitation(client, ctx, orgName, repoName, username)
if err != nil {
return err
} else if invitation != nil {
username = *invitation.Invitee.Login
log.Printf("[DEBUG] Found invitation for %q", username)
permissionName, err := getInvitationPermission(invitation)
if err != nil {
return err
}
d.Set("repository", repoName)
d.Set("username", username)
d.Set("permission", permissionName)
d.Set("invitation_id", fmt.Sprintf("%d", invitation.GetID()))
return nil
}
// Next, check if the user has accepted the invite and is a full collaborator
opt := &github.ListCollaboratorsOptions{ListOptions: github.ListOptions{
PerPage: maxPerPage,
}}
for {
collaborators, resp, err := client.Repositories.ListCollaborators(ctx,
orgName, repoName, opt)
if err != nil {
return err
}
log.Printf("[DEBUG] Found %d collaborators, checking if any matches %q", len(collaborators), username)
for _, c := range collaborators {
if strings.EqualFold(*c.Login, username) {
log.Printf("[DEBUG] Matching collaborator found for %q", username)
permissionName, err := getRepoPermission(c.Permissions)
if err != nil {
return err
}
d.Set("repository", repoName)
d.Set("username", c.Login)
d.Set("permission", permissionName)
return nil
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
// The user is neither invited nor a collaborator
log.Printf("[WARN] Removing repository collaborator %s (%s/%s) from state because it no longer exists in GitHub",
username, orgName, repoName)
d.SetId("")
return nil
}
func resourceGithubRepositoryCollaboratorDelete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Organization).client
orgName := meta.(*Organization).name
username := d.Get("username").(string)
repoName := d.Get("repository").(string)
ctx := context.WithValue(context.Background(), ctxId, d.Id())
// Delete any pending invitations
invitation, err := findRepoInvitation(client, ctx, orgName, repoName, username)
if err != nil {
return err
} else if invitation != nil {
_, err = client.Repositories.DeleteInvitation(ctx, orgName, repoName, *invitation.ID)
return err
}
log.Printf("[DEBUG] Deleting repository collaborator: %s (%s/%s)",
username, orgName, repoName)
_, err = client.Repositories.RemoveCollaborator(ctx, orgName, repoName, username)
return err
}
func findRepoInvitation(client *github.Client, ctx context.Context, owner, repo, collaborator string) (*github.RepositoryInvitation, error) {
opt := &github.ListOptions{PerPage: maxPerPage}
for {
invitations, resp, err := client.Repositories.ListInvitations(ctx, owner, repo, opt)
if err != nil {
return nil, err
}
for _, i := range invitations {
if strings.EqualFold(*i.Invitee.Login, collaborator) {
return i, nil
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return nil, nil
}