Skip to content

Commit

Permalink
Add data source: github_repository_environments
Browse files Browse the repository at this point in the history
  • Loading branch information
bpaquet committed May 19, 2023
1 parent 49b2376 commit d236e29
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
82 changes: 82 additions & 0 deletions github/data_source_github_repository_environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package github

import (
"context"

"github.com/google/go-github/v52/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGithubRepositoryEnvironments() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoryEnvironmentsRead,

Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
},
"environments": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"node_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGithubRepositoryEnvironmentsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)

results := make([]map[string]interface{}, 0)

var listOptions *github.EnvironmentListOptions
for {
environments, resp, err := client.Repositories.ListEnvironments(context.Background(), orgName, repoName, listOptions)
if err != nil {
return err
}

results = append(results, flattenEnvironments(environments)...)

if resp.NextPage == 0 {
break
}

listOptions.Page = resp.NextPage
}

d.SetId(repoName)
d.Set("environments", results)

return nil
}

func flattenEnvironments(environments *github.EnvResponse) []map[string]interface{} {
results := make([]map[string]interface{}, 0)
if environments == nil {
return results
}

for _, environment := range environments.Environments {
environmentMap := make(map[string]interface{})
environmentMap["name"] = environment.GetName()
environmentMap["node_id"] = environment.GetNodeID()
results = append(results, environmentMap)
}

return results
}
68 changes: 68 additions & 0 deletions github/data_source_github_repository_environments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package github

import (
"fmt"
"testing"

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

func TestAccGithubRepositoryEnvironmentsDataSource(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("queries environments", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}
resource "github_repository_environment" "env1" {
repository = github_repository.test.name
environment = "env_x"
}
`, randomID)

config2 := config + `
data "github_repository_environments" "all" {
repository = github_repository.test.name
}
`
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.github_repository_environments.all", "environments.#", "1"),
resource.TestCheckResourceAttr("data.github_repository_environments.all", "environments.0.name", "env_x"),
resource.TestCheckResourceAttrSet("data.github_repository_environments.all", "environments.0.node_id"),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
},
{
Config: config2,
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)
})

})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func Provider() terraform.ResourceProvider {
"github_repositories": dataSourceGithubRepositories(),
"github_repository": dataSourceGithubRepository(),
"github_repository_branches": dataSourceGithubRepositoryBranches(),
"github_repository_environments": dataSourceGithubRepositoryEnvironments(),
"github_repository_deploy_keys": dataSourceGithubRepositoryDeployKeys(),
"github_repository_file": dataSourceGithubRepositoryFile(),
"github_repository_milestone": dataSourceGithubRepositoryMilestone(),
Expand Down
28 changes: 28 additions & 0 deletions website/docs/d/repository_environments.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
layout: "github"
page_title: "GitHub: repository_environments"
description: |-
Get information on a GitHub repository's environments.
---

# github_repository_environments

Use this data source to retrieve information about environments for a repository.

## Example Usage

```hcl
data "github_repository_environments" "example" {
repository = "example-repository"
}
```

## Argument Reference

* `repository` - (Required) Name of the repository to retrieve the environments from.

## Attributes Reference

* `environments` - The list of this repository's environments. Each element of `environments` has the following attributes:
* `name` - Environment name.
* `node_id` - Environment node id.

0 comments on commit d236e29

Please sign in to comment.