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

New Data Source: github_repositories #129

Merged
merged 1 commit into from
Aug 13, 2018
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
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd question whether we want to also expose these as properties on the data source (e.g. org / language)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I don't think there's any strong enough use case that would justify creating (and maintaining) a custom parser for a fairly non-trivial query syntax, or did you have any particular one in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, I was just thinking out loud :)

}
```

## 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