-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from terraform-providers/f-repositories-ds
New Data Source: github_repositories
- Loading branch information
Showing
5 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters