-
Notifications
You must be signed in to change notification settings - Fork 961
Expand file tree
/
Copy pathdata_source_github_repositories.go
More file actions
126 lines (109 loc) · 2.77 KB
/
data_source_github_repositories.go
File metadata and controls
126 lines (109 loc) · 2.77 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package github
import (
"context"
"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceGithubRepositories() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoriesRead,
Schema: map[string]*schema.Schema{
"query": {
Type: schema.TypeString,
Required: true,
},
"sort": {
Type: schema.TypeString,
Default: "updated",
Optional: true,
ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{"stars", "fork", "updated"}, false), "sort"),
},
"include_repo_id": {
Type: schema.TypeBool,
Default: false,
Optional: true,
},
"results_per_page": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
ValidateDiagFunc: toDiagFunc(validation.IntBetween(0, 1000), "results_per_page"),
},
"full_names": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
"names": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
"repo_ids": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Computed: true,
},
},
}
}
func dataSourceGithubRepositoriesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
includeRepoId := d.Get("include_repo_id").(bool)
resultsPerPage := d.Get("results_per_page").(int)
query := d.Get("query").(string)
opt := &github.SearchOptions{
Sort: d.Get("sort").(string),
ListOptions: github.ListOptions{
PerPage: resultsPerPage,
},
}
fullNames, names, repoIDs, err := searchGithubRepositories(client, query, opt)
if err != nil {
return err
}
d.SetId(query)
err = d.Set("full_names", fullNames)
if err != nil {
return err
}
err = d.Set("names", names)
if err != nil {
return err
}
if includeRepoId {
err = d.Set("repo_ids", repoIDs)
if err != nil {
return err
}
}
return nil
}
func searchGithubRepositories(client *github.Client, query string, opt *github.SearchOptions) ([]string, []string, []int64, error) {
fullNames := make([]string, 0)
names := make([]string, 0)
repoIDs := make([]int64, 0)
for {
results, resp, err := client.Search.Repositories(context.TODO(), query, opt)
if err != nil {
return fullNames, names, repoIDs, err
}
for _, repo := range results.Repositories {
fullNames = append(fullNames, repo.GetFullName())
names = append(names, repo.GetName())
repoIDs = append(repoIDs, repo.GetID())
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return fullNames, names, repoIDs, nil
}