-
Notifications
You must be signed in to change notification settings - Fork 772
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
)?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)