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

Refactor out branches attribute to new data source #1117

Merged
merged 12 commits into from
Sep 6, 2022
22 changes: 0 additions & 22 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,6 @@ func dataSourceGithubRepository() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"branches": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"protected": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"pages": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -242,12 +226,6 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er
d.Set("repo_id", repo.GetID())
d.Set("has_projects", repo.GetHasProjects())

branches, _, err := client.Repositories.ListBranches(context.TODO(), owner, repoName, nil)
if err != nil {
return err
}
d.Set("branches", flattenBranches(branches))

if repo.GetHasPages() {
pages, _, err := client.Repositories.GetPagesInfo(context.TODO(), owner, repoName)
if err != nil {
Expand Down
70 changes: 70 additions & 0 deletions github/data_source_github_repository_branches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package github

import (
"context"
"fmt"

"github.com/google/go-github/v43/github"
k24dizzle marked this conversation as resolved.
Show resolved Hide resolved
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGithubRepositoryBranches() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoryBranchesRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
},
"branches": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"protected": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}

func flattenBranches(branches []*github.Branch) []interface{} {
if branches == nil {
return []interface{}{}
}

branchList := make([]interface{}, 0, len(branches))
for _, branch := range branches {
branchMap := make(map[string]interface{})
branchMap["name"] = branch.GetName()
branchMap["protected"] = branch.GetProtected()
branchList = append(branchList, branchMap)
}

return branchList
}

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

branches, _, err := client.Repositories.ListBranches(context.TODO(), orgName, repoName, nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note, this data source will only display the first 30 branches of a repository, this is the default setting of the Github API endpoint
Screen Shot 2022-04-18 at 4 25 30 PM

This mimics the previous behavior introduced in #892: https://github.com/kzhaotest/branches_test1 (see in this repo although there are 50+ branches, only 30+ are shown in the README (tf located here)).

We can introduce pagination in a future PR, but for now just replicate the previous behavior

if err != nil {
return err
}

d.SetId(fmt.Sprintf("%s/%s", orgName, repoName))
d.Set("repository", repoName)
d.Set("branches", flattenBranches(branches))

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

import (
"fmt"
"testing"

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

func TestAccGithubBranchesDataSource(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func TestAccGithubBranchesDataSource(t *testing.T) {
func TestAccGithubRepositoryBranchesDataSource(t *testing.T) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ty :)

t.Run("manages branches of a new repository", func(t *testing.T) {
repoName := fmt.Sprintf("tf-acc-test-branches-%s", acctest.RandString(5))
config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
auto_init = true
}

data "github_repository_branches" "test" {
repository = github_repository.test.name
}
`, repoName)

const resourceName = "data.github_repository_branches.test"
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "branches.#", "1"),
resource.TestCheckResourceAttr(resourceName, "branches.0.name", "main"),
resource.TestCheckResourceAttr(resourceName, "branches.0.protected", "false"),
)

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)
})
})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func Provider() terraform.ResourceProvider {
"github_release": dataSourceGithubRelease(),
"github_repositories": dataSourceGithubRepositories(),
"github_repository": dataSourceGithubRepository(),
"github_repository_branches": dataSourceGithubRepositoryBranches(),
"github_repository_file": dataSourceGithubRepositoryFile(),
"github_repository_milestone": dataSourceGithubRepositoryMilestone(),
"github_repository_pull_request": dataSourceGithubRepositoryPullRequest(),
Expand Down
39 changes: 0 additions & 39 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,6 @@ func resourceGithubRepository() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"branches": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"protected": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"pages": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -454,12 +438,6 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro
d.Set("node_id", repo.GetNodeID())
d.Set("repo_id", repo.GetID())

branches, _, err := client.Repositories.ListBranches(ctx, owner, repoName, nil)
if err != nil {
return err
}
d.Set("branches", flattenBranches(branches))

if repo.GetHasPages() {
pages, _, err := client.Repositories.GetPagesInfo(ctx, owner, repoName)
if err != nil {
Expand Down Expand Up @@ -691,20 +669,3 @@ func flattenPages(pages *github.Pages) []interface{} {

return []interface{}{pagesMap}
}

func flattenBranches(branches []*github.Branch) []interface{} {
if branches == nil {
return []interface{}{}
}

branchList := make([]interface{}, 0, len(branches))

for _, branch := range branches {
branchMap := make(map[string]interface{})
branchMap["name"] = branch.Name
branchMap["protected"] = branch.Protected
branchList = append(branchList, branchMap)
}

return branchList
}
4 changes: 0 additions & 4 deletions website/docs/d/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,3 @@ The following arguments are supported:
* `node_id` - GraphQL global node id for use with v4 API

* `repo_id` - GitHub ID for the repository

* `branches` - The list of this repository's branches. Each element of `branches` has the following attributes:
* `name` - Name of the branch.
* `protected` - Whether the branch is protected.
28 changes: 28 additions & 0 deletions website/docs/d/repository_branches.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
layout: "github"
page_title: "GitHub: repository_branches"
description: |-
Get information on a Github repository's branches.
---

# github_repository_branches

Use this data source to retrieve information about branches in a repository.

## Example Usage

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

## Argument Reference

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

## Attributes Reference

* `branches` - The list of this repository's branches. Each element of `branches` has the following attributes:
* `name` - Name of the branch.
* `protected` - Whether the branch is protected.
5 changes: 0 additions & 5 deletions website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@ The following additional attributes are exported:
* `html_url` - The absolute URL (including scheme) of the rendered GitHub Pages site e.g. `https://username.github.io`.
* `status` - The GitHub Pages site's build status e.g. `building` or `built`.

* `branches` - The list of this repository's branches. Each element of `branches` has the following attributes:
* `name` - Name of the branch.
* `protected` - Whether the branch is protected.


## Import

Repositories can be imported using the `name`, e.g.
Expand Down
3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<li>
<a href="/docs/providers/github/d/repository.html">github_repository</a>
</li>
<li>
<a href="/docs/providers/github/d/repository_branches.html">github_repository_branches</a>
</li>
<li>
<a href="/docs/providers/github/d/repository_file.html">github_repository_file</a>
</li>
Expand Down