-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
resource_github_repository_collaborator.go
103 lines (82 loc) · 2.55 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
package github
import (
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGithubRepositoryCollaborator() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryCollaboratorCreate,
Read: resourceGithubRepositoryCollaboratorRead,
// editing repository collaborators are not supported by github api so forcing new on any changes
Delete: resourceGithubRepositoryCollaboratorDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"repository": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"permission": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "push",
ValidateFunc: validateValueFunc([]string{"pull", "push", "admin"}),
},
},
}
}
func resourceGithubRepositoryCollaboratorCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
u := d.Get("username").(string)
r := d.Get("repository").(string)
p := d.Get("permission").(string)
_, err := client.Repositories.AddCollaborator(meta.(*Organization).name, r, u,
&github.RepositoryAddCollaboratorOptions{Permission: p})
if err != nil {
return err
}
d.SetId(buildTwoPartID(&r, &u))
return resourceGithubRepositoryCollaboratorRead(d, meta)
}
func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
r, u := parseTwoPartID(d.Id())
isCollaborator, _, err := client.Repositories.IsCollaborator(meta.(*Organization).name, r, u)
if !isCollaborator || err != nil {
d.SetId("")
return nil
}
collaborators, _, err := client.Repositories.ListCollaborators(meta.(*Organization).name, r,
&github.ListOptions{})
if err != nil {
return err
}
for _, c := range collaborators {
if *c.Login == u {
permName, err := getRepoPermission(c.Permissions)
if err != nil {
return err
}
d.Set("repository", r)
d.Set("username", u)
d.Set("permission", permName)
return nil
}
}
return nil
}
func resourceGithubRepositoryCollaboratorDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
u := d.Get("username").(string)
r := d.Get("repository").(string)
_, err := client.Repositories.RemoveCollaborator(meta.(*Organization).name, r, u)
return err
}