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

Add branches attribute to github_repository #892

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ 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 @@ -213,6 +229,12 @@ 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
23 changes: 23 additions & 0 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ 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 @@ -660,3 +666,20 @@ 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: 4 additions & 0 deletions website/docs/d/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ 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.
5 changes: 5 additions & 0 deletions website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ 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