forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor out branches attribute to new data source (integrations#1117)
* 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
1 parent
7c22b23
commit 7635707
Showing
9 changed files
with
159 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters