forked from integrations/terraform-provider-github
/
data_source_github_repositories.go
84 lines (69 loc) · 1.66 KB
/
data_source_github_repositories.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
}