diff --git a/.gitignore b/.gitignore index 5982d2c642..337726fdc7 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ website/node_modules *.iml *.test *.iml +*.tfvars website/vendor diff --git a/github/data_source_github_repositories.go b/github/data_source_github_repositories.go index af5d69720e..f8ed1b8473 100644 --- a/github/data_source_github_repositories.go +++ b/github/data_source_github_repositories.go @@ -6,6 +6,7 @@ import ( "github.com/google/go-github/v25/github" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" ) func dataSourceGithubRepositories() *schema.Resource { @@ -17,15 +18,21 @@ func dataSourceGithubRepositories() *schema.Resource { Type: schema.TypeString, Required: true, }, + "sort": { + Type: schema.TypeString, + Default: "updated", + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"stars", "fork", "updated"}, false), + }, "full_names": { - Type: schema.TypeSet, + Type: schema.TypeList, Elem: &schema.Schema{ Type: schema.TypeString, }, Computed: true, }, "names": { - Type: schema.TypeSet, + Type: schema.TypeList, Elem: &schema.Schema{ Type: schema.TypeString, }, @@ -39,9 +46,15 @@ func dataSourceGithubRepositoriesRead(d *schema.ResourceData, meta interface{}) client := meta.(*Organization).client query := d.Get("query").(string) + opt := &github.SearchOptions{ + Sort: d.Get("sort").(string), + ListOptions: github.ListOptions{ + PerPage: 100, + }, + } log.Printf("[DEBUG] Searching for GitHub repositories: %q", query) - fullNames, names, err := searchGithubRepositories(client, query) + fullNames, names, err := searchGithubRepositories(client, query, opt) if err != nil { return err } @@ -53,15 +66,10 @@ func dataSourceGithubRepositoriesRead(d *schema.ResourceData, meta interface{}) return nil } -func searchGithubRepositories(client *github.Client, query string) ([]string, []string, error) { - fullNames := make([]string, 0, 0) - names := make([]string, 0, 0) +func searchGithubRepositories(client *github.Client, query string, opt *github.SearchOptions) ([]string, []string, error) { + fullNames := make([]string, 0) - opt := &github.SearchOptions{ - ListOptions: github.ListOptions{ - PerPage: 100, - }, - } + names := make([]string, 0) for { results, resp, err := client.Search.Repositories(context.TODO(), query, opt) diff --git a/github/data_source_github_repositories_test.go b/github/data_source_github_repositories_test.go index 7561591d50..7c6c30aef9 100644 --- a/github/data_source_github_repositories_test.go +++ b/github/data_source_github_repositories_test.go @@ -2,13 +2,14 @@ package github import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform/helper/resource" ) func TestAccGithubRepositoriesDataSource_basic(t *testing.T) { - query := "org:hashicorp terraform" + query := "org:hashicorp repository:terraform" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -18,10 +19,35 @@ func TestAccGithubRepositoriesDataSource_basic(t *testing.T) { { 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"), + resource.TestMatchResourceAttr("data.github_repositories.test", "full_names.0", regexp.MustCompile(`^hashicorp`)), + resource.TestMatchResourceAttr("data.github_repositories.test", "names.0", regexp.MustCompile(`^terraform`)), + resource.TestCheckResourceAttr("data.github_repositories.test", "sort", "updated"), + ), + }, + }, + }) +} +func TestAccGithubRepositoriesDataSource_Sort(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGithubRepositoriesDataSourceConfigWithSort("org:hashicorp repository:terraform", "updated"), + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr("data.github_repositories.test", "full_names.0", regexp.MustCompile(`^hashicorp`)), + resource.TestMatchResourceAttr("data.github_repositories.test", "names.0", regexp.MustCompile(`^terraform`)), + resource.TestCheckResourceAttr("data.github_repositories.test", "sort", "updated"), + ), + }, + { + Config: testAccCheckGithubRepositoriesDataSourceConfigWithSort("org:hashicorp language:go", "stars"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.github_repositories.test", "full_names.0", "hashicorp/terraform"), + resource.TestCheckResourceAttr("data.github_repositories.test", "names.0", "terraform"), + resource.TestCheckResourceAttr("data.github_repositories.test", "sort", "stars"), ), }, }, @@ -54,3 +80,12 @@ data "github_repositories" "test" { } `, query) } + +func testAccCheckGithubRepositoriesDataSourceConfigWithSort(query, sort string) string { + return fmt.Sprintf(` +data "github_repositories" "test" { + query = "%s" + sort = "%s" +} +`, query, sort) +} diff --git a/website/docs/d/repositories.html.markdown b/website/docs/d/repositories.html.markdown index 38badf7e37..7dfa825a92 100644 --- a/website/docs/d/repositories.html.markdown +++ b/website/docs/d/repositories.html.markdown @@ -27,6 +27,8 @@ The following arguments are supported: * `query` - (Required) Search query. See [documentation for the search syntax](https://help.github.com/articles/understanding-the-search-syntax/). +* `sort` - (Optional) Sorts the repositories returned by the specified attribute. Valid values include `stars`, `fork`, and `updated`. Defaults to `updated`. + ## Attributes Reference * `full_names` - A list of full names of found repositories (e.g. `hashicorp/terraform`)