Skip to content

Commit

Permalink
feat: allow filtering on permission in repo collaborator datasource (#…
Browse files Browse the repository at this point in the history
…2382)

* feat: allow filtering on permission in repo collaborator datasource

* ensure changing of ID isn't a breaking change
  • Loading branch information
felixlut authored Sep 9, 2024
1 parent f3792c8 commit af9cf3d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
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

0 comments on commit af9cf3d

Please sign in to comment.