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

GitHub org ignore archived repos #1833

Merged
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"),
Copy link
Contributor Author

@felixlut felixlut Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kfcampbell this test is based on the ones I found here, where it appears that TestCheckOutput() handles the conversions of bools and strings, i.e., false --> "false". That code uses terraform-plugin-sdk/v2, while the GitHub provider uses v1. Is there a reason for not using v2 here?

I think I read somewhere in the contribution docs that you guys don't want the developer community to mess with bumping dependencies ourselves, which is why I'm asking 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is due to lack of prioritization of #1780, not anything super intentional. I'm fine adding v2 as a dependency since we'll need to cut over to it anyways.

Copy link
Contributor Author

@felixlut felixlut Aug 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how do we move forward here? Should I wait until #1780 is merged, or import and use v2 in data_source_github_organization_test.go only (if that is even possible, haven't dealt much with golang dependencies and vendoring before)? I don't care about getting this in fast, so if it takes another couple of weeks for #1780, I'm fine to wait

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move forward with your second option. The approach you describe is indeed possible and I think it's unlikely #1780 is going to be merged in the near future.

Copy link
Contributor Author

@felixlut felixlut Aug 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played around a bit with option 2, but I think using both v1 and v2 of the sdk is not compatible, at least not without doing some real weird stuff.

First of you need to use a version of the sdk before v2.10.1 (which released in dec 2021). Using a later version requires a bump of terraform-exec, to a version above v0.16.0, which deprecated the tfinstall package that v1 of the sdk depends on

I just go stuck with the tests using the testAccProviders (defined in github/provider_test.go), which uses v1. They cannot be used for the v2 tests in github/data_source_github_organization_test.go.

I looked into using the terraform-plugin-testing package as well, since that is the recommended solution for testing, but that seems to require v2 as well. I'm not spending anymore time investigating it, I'll wait for #1780. Unless you have some idea of how to test this feature that wont require a bump to v2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh gross...thanks for doing the legwork on that! I'm 👍 on waiting for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're interested in picking up #1780 and fixing the build errors, please feel free! You should be able to create a new PR off of my branch if you desire.

Copy link
Contributor Author

@felixlut felixlut Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kfcampbell that thought did cross my mind, especially since the time I spent working around it could have been enough to just make it work properly. I learnt a lot about go dependencies at least 😅

I may take a look later

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, I'm glad you were able to take something positive out of it! Should you end up deciding to do so, I'd be happy to look at it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new release this actually works, I've tested it locally.

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