Skip to content

Commit af9cf3d

Browse files
authored
feat: allow filtering on permission in repo collaborator datasource (#2382)
* feat: allow filtering on permission in repo collaborator datasource * ensure changing of ID isn't a breaking change
1 parent f3792c8 commit af9cf3d

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

github/data_source_github_collaborators.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ func dataSourceGithubCollaborators() *schema.Resource {
3232
Optional: true,
3333
Default: "all",
3434
},
35+
"permission": {
36+
Type: schema.TypeString,
37+
ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{
38+
"pull",
39+
"triage",
40+
"push",
41+
"maintain",
42+
"admin",
43+
}, false), "permission"),
44+
Optional: true,
45+
Default: "",
46+
},
3547
"collaborator": {
3648
Type: schema.TypeList,
3749
Computed: true,
@@ -116,15 +128,21 @@ func dataSourceGithubCollaboratorsRead(d *schema.ResourceData, meta interface{})
116128
owner := d.Get("owner").(string)
117129
repo := d.Get("repository").(string)
118130
affiliation := d.Get("affiliation").(string)
131+
permission := d.Get("permission").(string)
119132

120133
options := &github.ListCollaboratorsOptions{
121134
Affiliation: affiliation,
135+
Permission: permission,
122136
ListOptions: github.ListOptions{
123137
PerPage: maxPerPage,
124138
},
125139
}
126140

127-
d.SetId(fmt.Sprintf("%s/%s/%s", owner, repo, affiliation))
141+
if len(permission) == 0 {
142+
d.SetId(fmt.Sprintf("%s/%s/%s", owner, repo, affiliation))
143+
} else {
144+
d.SetId(fmt.Sprintf("%s/%s/%s/%s", owner, repo, affiliation, permission))
145+
}
128146
err := d.Set("owner", owner)
129147
if err != nil {
130148
return err
@@ -137,6 +155,10 @@ func dataSourceGithubCollaboratorsRead(d *schema.ResourceData, meta interface{})
137155
if err != nil {
138156
return err
139157
}
158+
err = d.Set("permission", permission)
159+
if err != nil {
160+
return err
161+
}
140162

141163
totalCollaborators := make([]interface{}, 0)
142164
for {

github/data_source_github_collaborators_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ func TestAccGithubCollaboratorsDataSource_basic(t *testing.T) {
3333
})
3434
}
3535

36+
func TestAccGithubCollaboratorsDataSource_withPermission(t *testing.T) {
37+
if err := testAccCheckOrganization(); err != nil {
38+
t.Skipf("Skipping because %s.", err.Error())
39+
}
40+
41+
dsn := "data.github_collaborators.test"
42+
repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5))
43+
44+
resource.ParallelTest(t, resource.TestCase{
45+
PreCheck: func() {
46+
testAccPreCheck(t)
47+
},
48+
Providers: testAccProviders,
49+
Steps: []resource.TestStep{
50+
{
51+
Config: testAccCheckGithubCollaboratorsDataSourcePermissionConfig(repoName),
52+
Check: resource.ComposeTestCheckFunc(
53+
resource.TestCheckResourceAttrSet(dsn, "collaborator.#"),
54+
resource.TestCheckResourceAttr(dsn, "affiliation", "all"),
55+
resource.TestCheckResourceAttr(dsn, "permission", "admin"),
56+
),
57+
},
58+
},
59+
})
60+
}
61+
3662
func testAccCheckGithubCollaboratorsDataSourceConfig(repo string) string {
3763
return fmt.Sprintf(`
3864
resource "github_repository" "test" {
@@ -45,3 +71,16 @@ data "github_collaborators" "test" {
4571
}
4672
`, repo, testOwner)
4773
}
74+
func testAccCheckGithubCollaboratorsDataSourcePermissionConfig(repo string) string {
75+
return fmt.Sprintf(`
76+
resource "github_repository" "test" {
77+
name = "%s"
78+
}
79+
80+
data "github_collaborators" "test" {
81+
owner = "%s"
82+
repository = "${github_repository.test.name}"
83+
permission = "admin"
84+
}
85+
`, repo, testOwner)
86+
}

website/docs/d/collaborators.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ data "github_collaborators" "test" {
2626

2727
* `affiliation` - (Optional) Filter collaborators returned by their affiliation. Can be one of: `outside`, `direct`, `all`. Defaults to `all`.
2828

29+
* `permission` - (Optional) Filter collaborators returned by their permission. Can be one of: `pull`, `triage`, `push`, `maintain`, `admin`. Defaults to not doing any filtering on permission.
30+
2931
## Attributes Reference
3032

3133
* `collaborator` - An Array of GitHub collaborators. Each `collaborator` block consists of the fields documented below.

0 commit comments

Comments
 (0)