From 740899b060a2e1c5b01daa497a4e0f5b19a02686 Mon Sep 17 00:00:00 2001 From: Mikhail Bulash Date: Fri, 27 Aug 2021 16:11:59 +0200 Subject: [PATCH] Add `branches` attribute to `github_repository` --- github/data_source_github_repository.go | 22 ++++++++++++++++++++++ github/resource_github_repository.go | 23 +++++++++++++++++++++++ website/docs/d/repository.html.markdown | 4 ++++ website/docs/r/repository.html.markdown | 5 +++++ 4 files changed, 54 insertions(+) diff --git a/github/data_source_github_repository.go b/github/data_source_github_repository.go index 26e097c013..a51ea1d630 100644 --- a/github/data_source_github_repository.go +++ b/github/data_source_github_repository.go @@ -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, @@ -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 { diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 9eb1b502c3..4ba7cf5e1f 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -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 { @@ -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 +} diff --git a/website/docs/d/repository.html.markdown b/website/docs/d/repository.html.markdown index 546f3ae6fc..b5b762cd76 100644 --- a/website/docs/d/repository.html.markdown +++ b/website/docs/d/repository.html.markdown @@ -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. diff --git a/website/docs/r/repository.html.markdown b/website/docs/r/repository.html.markdown index 60dccfb4f8..4140690815 100644 --- a/website/docs/r/repository.html.markdown +++ b/website/docs/r/repository.html.markdown @@ -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.