-
Notifications
You must be signed in to change notification settings - Fork 772
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9f1c430
Refactor out branches attribute from resource_github_repository and d…
k24dizzle 468cfc3
Add link to docs
k24dizzle 00aa131
Update github/data_source_github_repository_branches.go
k24dizzle 1bd34d6
Add only_protected_branches in data source github_repository (#1162)
bpaquet 5f1a989
Remove mistakenly committed binary (#1223)
kfcampbell ce4c046
Revert PR #1162 (#1224)
kfcampbell 026fe7f
Merge branch 'main' into branches
k24dizzle 5534c8a
Merge branch 'main' into branches
kfcampbell f857f8f
Merge branch 'main' into branches
kfcampbell 4bb8eec
Merge branch 'integrations:main' into branches
k24dizzle 2b683f6
marek's suggestion + use v47
k24dizzle d3aaa76
Merge branch 'main' into branches
kfcampbell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
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