Skip to content

Commit

Permalink
Refactor out branches attribute to new data source (integrations#1117)
Browse files Browse the repository at this point in the history
* Refactor out branches attribute from resource_github_repository and data_source_github_repository

to data_source_github_repository_branches

* Add link to docs

* Update github/data_source_github_repository_branches.go

Co-authored-by: mareksapota-lyft <msapota@lyft.com>

* Add only_protected_branches in data source github_repository (integrations#1162)

This will allow to retrieve only the protected branches instead of the first 30.

Co-authored-by: Keegan Campbell <me@kfcampbell.com>

* Remove mistakenly committed binary (integrations#1223)

* Revert PR integrations#1162 (integrations#1224)

* marek's suggestion + use v47

Co-authored-by: mareksapota-lyft <msapota@lyft.com>
Co-authored-by: Bertrand Paquet <bertrand.paquet@gmail.com>
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
4 people authored and kazaker committed Dec 28, 2022
1 parent 3b23c0c commit 2a976ca
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 64 deletions.
16 changes: 0 additions & 16 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,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
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/v47/github"
"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)
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 TestAccGithubRepositoryBranchesDataSource(t *testing.T) {
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 @@ -148,6 +148,7 @@ func Provider() terraform.ResourceProvider {
"github_repositories": dataSourceGithubRepositories(),
"github_repositories_v2": dataSourceGithubRepositoriesV2(),
"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 @@ -147,22 +147,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 @@ -482,12 +466,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 @@ -720,20 +698,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 @@ -84,7 +84,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 @@ -161,11 +161,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 @@ -52,6 +52,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

0 comments on commit 2a976ca

Please sign in to comment.