Skip to content

Commit

Permalink
data_source/github_repositories: add sort capabilities (#234)
Browse files Browse the repository at this point in the history
* data_source/github_repositories: add sort capabilities

* updates docs

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

Co-Authored-By: kt <[email protected]>

* Update github/data_source_github_repositories_test.go

Co-Authored-By: kt <[email protected]>

* reverts '%q' back to '"%s"'
--- PASS: TestAccGithubRepositoriesDataSource_basic (2.21s)
--- PASS: TestAccGithubRepositoriesDataSource_Sort (25.97s)
--- PASS: TestAccGithubRepositoriesDataSource_noMatch (1.60s)
  • Loading branch information
tracypholmes authored Jun 8, 2019
1 parent d6d3272 commit dffe362
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ website/node_modules
*.iml
*.test
*.iml
*.tfvars

website/vendor

Expand Down
30 changes: 19 additions & 11 deletions github/data_source_github_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
},
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
45 changes: 40 additions & 5 deletions github/data_source_github_repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"),
),
},
},
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions website/docs/d/repositories.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down

0 comments on commit dffe362

Please sign in to comment.