Skip to content

Commit

Permalink
Merge pull request #129 from terraform-providers/f-repositories-ds
Browse files Browse the repository at this point in the history
New Data Source: github_repositories
  • Loading branch information
radeksimko authored Aug 13, 2018
2 parents 06d54d2 + a7428bc commit 955cd25
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 4 deletions.
84 changes: 84 additions & 0 deletions github/data_source_github_repositories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package github

import (
"context"
"log"

"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceGithubRepositories() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoriesRead,

Schema: map[string]*schema.Schema{
"query": {
Type: schema.TypeString,
Required: true,
},
"full_names": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
"names": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
},
}
}

func dataSourceGithubRepositoriesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

query := d.Get("query").(string)

log.Printf("[DEBUG] Searching for GitHub repositories: %q", query)
fullNames, names, err := searchGithubRepositories(client, query)
if err != nil {
return err
}

d.SetId(query)
d.Set("full_names", fullNames)
d.Set("names", names)

return nil
}

func searchGithubRepositories(client *github.Client, query string) ([]string, []string, error) {
fullNames := make([]string, 0, 0)
names := make([]string, 0, 0)

opt := &github.SearchOptions{
ListOptions: github.ListOptions{
PerPage: 100,
},
}

for {
results, resp, err := client.Search.Repositories(context.TODO(), query, opt)
if err != nil {
return fullNames, names, err
}

for _, repo := range results.Repositories {
fullNames = append(fullNames, repo.GetFullName())
names = append(names, repo.GetName())
}

if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}

return fullNames, names, nil
}
56 changes: 56 additions & 0 deletions github/data_source_github_repositories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package github

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccGithubRepositoriesDataSource_basic(t *testing.T) {
query := "org:hashicorp terraform"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoriesDataSourceConfig(query),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_repositories.test", "full_names.#"),
resource.TestCheckResourceAttr("data.github_repositories.test", "full_names.3450805659", "hashicorp/terraform"),
resource.TestCheckResourceAttrSet("data.github_repositories.test", "names.#"),
resource.TestCheckResourceAttr("data.github_repositories.test", "names.535570215", "terraform"),
),
},
},
})
}

func TestAccGithubRepositoriesDataSource_noMatch(t *testing.T) {
query := "klsafj_23434_doesnt_exist"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoriesDataSourceConfig(query),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.github_repositories.test", "full_names.#", "0"),
resource.TestCheckResourceAttr("data.github_repositories.test", "names.#", "0"),
),
},
},
})
}

func testAccCheckGithubRepositoriesDataSourceConfig(query string) string {
return fmt.Sprintf(`
data "github_repositories" "test" {
query = "%s"
}
`, query)
}
9 changes: 5 additions & 4 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_repository": dataSourceGithubRepository(),
"github_team": dataSourceGithubTeam(),
"github_user": dataSourceGithubUser(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_repository": dataSourceGithubRepository(),
"github_repositories": dataSourceGithubRepositories(),
"github_team": dataSourceGithubTeam(),
"github_user": dataSourceGithubUser(),
},
}

Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/repositories.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: "github"
page_title: "GitHub: github_repositories"
sidebar_current: "docs-github-datasource-repositories"
description: |-
Search for GitHub repositories
---

# github_repositories

-> **Note:** The data source will return a maximum of `1000` repositories
[as documented in official API docs](https://developer.github.com/v3/search/#about-the-search-api).

Use this data source to retrieve a list of GitHub repositories using a search query.

## Example Usage

```hcl
data "github_repositories" "example" {
query = "org:hashicorp language:Go"
}
```

## Argument Reference

The following arguments are supported:

* `query` - (Required) Search query. See [documentation for the search syntax](https://help.github.com/articles/understanding-the-search-syntax/).

## Attributes Reference

* `full_names` - A list of full names of found repositories (e.g. `hashicorp/terraform`)
* `names` - A list of found repository names (e.g. `terraform`)
3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<li<%= sidebar_current("docs-github-datasource-ip-ranges") %>>
<a href="/docs/providers/github/d/ip_ranges.html">github_ip_ranges</a>
</li>
<li<%= sidebar_current("docs-github-datasource-repositories") %>>
<a href="/docs/providers/github/d/repositories.html">github_repositories</a>
</li>
<li<%= sidebar_current("docs-github-datasource-repository") %>>
<a href="/docs/providers/github/d/repository.html">github_repository</a>
</li>
Expand Down

0 comments on commit 955cd25

Please sign in to comment.