Skip to content

Commit

Permalink
Add branches attribute to github_repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Bulash committed Aug 27, 2021
1 parent 97a100b commit a5fdcfa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
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
25 changes: 25 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,22 @@ func flattenPages(pages *github.Pages) []interface{} {

return []interface{}{pagesMap}
}

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

println(branches)

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

0 comments on commit a5fdcfa

Please sign in to comment.