Skip to content

Commit

Permalink
Feature/get repos from a organization (integrations#750)
Browse files Browse the repository at this point in the history
* Get repos from a given org

* revert changes to default in accetance tests

* Update github/data_source_github_organization.go

Co-authored-by: Jeremy Udit <[email protected]>

* Update github/data_source_github_organization.go

Co-authored-by: Jeremy Udit <[email protected]>

* Update github/data_source_github_organization_test.go

Co-authored-by: Jeremy Udit <[email protected]>

* Update website/docs/d/organization.html.markdown

Co-authored-by: Jeremy Udit <[email protected]>

Co-authored-by: Jeremy Udit <[email protected]>
  • Loading branch information
kilwa0 and Jeremy Udit authored Apr 17, 2021
1 parent 352e032 commit 41a0601
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
36 changes: 34 additions & 2 deletions github/data_source_github_organization.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package github

import (
"github.com/google/go-github/v32/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGithubOrganization() *schema.Resource {
Expand Down Expand Up @@ -32,6 +32,13 @@ func dataSourceGithubOrganization() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"repositories": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand All @@ -50,11 +57,36 @@ func dataSourceGithubOrganizationRead(d *schema.ResourceData, meta interface{})

plan := organization.GetPlan()

opts := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 10, Page: 1},
}

var repoList []string
var allRepos []*github.Repository

for {
repos, resp, err := client.Repositories.ListByOrg(ctx, name, opts)
if err != nil {
return err
}
allRepos = append(allRepos, repos...)

opts.Page = resp.NextPage

if resp.NextPage == 0 {
break
}
}
for index := range allRepos {
repoList = append(repoList, allRepos[index].GetFullName())
}

d.SetId(strconv.FormatInt(organization.GetID(), 10))
d.Set("login", organization.GetLogin())
d.Set("name", organization.GetName())
d.Set("description", organization.GetDescription())
d.Set("plan", plan.Name)
d.Set("repositories", repoList)

return nil
}
3 changes: 2 additions & 1 deletion github/data_source_github_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ func TestAccGithubOrganizationDataSource(t *testing.T) {
`, testOrganization)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_organization.test", "login"),
resource.TestCheckResourceAttr("data.github_organization.test", "login", testOrganization),
resource.TestCheckResourceAttrSet("data.github_organization.test", "name"),
resource.TestCheckResourceAttrSet("data.github_organization.test", "description"),
resource.TestCheckResourceAttrSet("data.github_organization.test", "plan"),
resource.TestCheckResourceAttrSet("data.github_organization.test", "repositories.#"),
)

testCase := func(t *testing.T, mode string) {
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/organization.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ data "github_organization" "test" {
## Attributes Reference

* `plan` - The plan name for the organization account
* `repositories` - (`list`) A list with the repositories on the organization

0 comments on commit 41a0601

Please sign in to comment.