Skip to content

Commit

Permalink
GitHub org ignore archived repos (#1833)
Browse files Browse the repository at this point in the history
* add ignore_archived_repos argument to github_organization datasource

* add test for checking that the archived repo is ignored

* add documentation for ignore_archived_repos

* fix ordering of imports

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
felixlut and kfcampbell authored Mar 4, 2024
1 parent 97d5113 commit 4ca9b21
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
14 changes: 13 additions & 1 deletion github/data_source_github_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func dataSourceGithubOrganization() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"ignore_archived_repos": {
Type: schema.TypeBool,
Default: false,
Optional: true,
},
"orgname": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -176,8 +181,15 @@ func dataSourceGithubOrganizationRead(d *schema.ResourceData, meta interface{})
break
}
}

ignoreArchiveRepos := d.Get("ignore_archived_repos").(bool)
for index := range allRepos {
repoList = append(repoList, allRepos[index].GetFullName())
repo := allRepos[index]
if ignoreArchiveRepos && repo.GetArchived() {
continue
}

repoList = append(repoList, repo.GetFullName())
}

var query struct {
Expand Down
66 changes: 66 additions & 0 deletions github/data_source_github_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

Expand Down Expand Up @@ -72,4 +73,69 @@ func TestAccGithubOrganizationDataSource(t *testing.T) {
})

})

t.Run("queries for an organization with archived repos", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
resource "github_repository" "archived" {
name = "tf-acc-archived-%s"
archived = true
}
data "github_organization" "skip_archived" {
name = "%s"
ignore_archived_repos = true
depends_on = [
github_repository.archived,
]
}
data "github_organization" "all_repos" {
name = "%s"
ignore_archived_repos = false
depends_on = [
github_repository.archived,
]
}
output "should_be_false" {
value = contains(data.github_organization.skip_archived.repositories, github_repository.archived.full_name)
}
output "should_be_true" {
value = contains(data.github_organization.all_repos.repositories, github_repository.archived.full_name)
}
`, randomID, testOrganization, testOrganization)

check := resource.ComposeTestCheckFunc(
resource.TestCheckOutput("should_be_false", "false"),
resource.TestCheckOutput("should_be_true", "true"),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})

}
4 changes: 4 additions & 0 deletions website/docs/d/organization.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ data "github_organization" "example" {
}
```

## Argument Reference

* `ignore_archived_repos` - Whether or not to include archived repos in the `repositories` list

## Attributes Reference

* `id` - The ID of the organization
Expand Down

0 comments on commit 4ca9b21

Please sign in to comment.