Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow filtering on permission in repo collaborator datasource #2382

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion github/data_source_github_collaborators.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ func dataSourceGithubCollaborators() *schema.Resource {
Optional: true,
Default: "all",
},
"permission": {
Type: schema.TypeString,
ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{
"pull",
"triage",
"push",
"maintain",
"admin",
}, false), "permission"),
Optional: true,
Default: "",
},
"collaborator": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -116,15 +128,21 @@ func dataSourceGithubCollaboratorsRead(d *schema.ResourceData, meta interface{})
owner := d.Get("owner").(string)
repo := d.Get("repository").(string)
affiliation := d.Get("affiliation").(string)
permission := d.Get("permission").(string)

options := &github.ListCollaboratorsOptions{
Affiliation: affiliation,
Permission: permission,
ListOptions: github.ListOptions{
PerPage: maxPerPage,
},
}

d.SetId(fmt.Sprintf("%s/%s/%s", owner, repo, affiliation))
if len(permission) == 0 {
d.SetId(fmt.Sprintf("%s/%s/%s", owner, repo, affiliation))
} else {
d.SetId(fmt.Sprintf("%s/%s/%s/%s", owner, repo, affiliation, permission))
}
err := d.Set("owner", owner)
if err != nil {
return err
Expand All @@ -137,6 +155,10 @@ func dataSourceGithubCollaboratorsRead(d *schema.ResourceData, meta interface{})
if err != nil {
return err
}
err = d.Set("permission", permission)
if err != nil {
return err
}

totalCollaborators := make([]interface{}, 0)
for {
Expand Down
39 changes: 39 additions & 0 deletions github/data_source_github_collaborators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ func TestAccGithubCollaboratorsDataSource_basic(t *testing.T) {
})
}

func TestAccGithubCollaboratorsDataSource_withPermission(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

dsn := "data.github_collaborators.test"
repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubCollaboratorsDataSourcePermissionConfig(repoName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dsn, "collaborator.#"),
resource.TestCheckResourceAttr(dsn, "affiliation", "all"),
resource.TestCheckResourceAttr(dsn, "permission", "admin"),
),
},
},
})
}

func testAccCheckGithubCollaboratorsDataSourceConfig(repo string) string {
return fmt.Sprintf(`
resource "github_repository" "test" {
Expand All @@ -45,3 +71,16 @@ data "github_collaborators" "test" {
}
`, repo, testOwner)
}
func testAccCheckGithubCollaboratorsDataSourcePermissionConfig(repo string) string {
return fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
}

data "github_collaborators" "test" {
owner = "%s"
repository = "${github_repository.test.name}"
permission = "admin"
}
`, repo, testOwner)
}
2 changes: 2 additions & 0 deletions website/docs/d/collaborators.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ data "github_collaborators" "test" {

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

* `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.

## Attributes Reference

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