This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
/
tool.go
208 lines (176 loc) · 4.97 KB
/
tool.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"context"
"fmt"
"github.com/google/go-github/v42/github"
"golang.org/x/oauth2"
"os"
"strings"
"text/tabwriter"
)
type TopicTool struct {
client *github.Client
}
func NewTopicTool(token string) *TopicTool {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return &TopicTool{
client: client,
}
}
func (t *TopicTool) ReplaceTopics(query string, topics []string) error {
result, err := t.searchRepositories(query)
if err != nil {
return err
}
if len(result.Repositories) == 0 {
return fmt.Errorf("no repositories found matching query %s", query)
}
previewRepositories(result.Repositories)
fmt.Println("")
fmt.Printf("\n Replace all labels with [%s] in %d repositories? [y/n/q]:\n", topicsToStr(topics), len(result.Repositories))
next, err := AskForBool(os.Stdin, true, false)
if err != nil {
return err
}
if !next {
return ErrAbortInput
}
return t.replaceTopics(result.Repositories, topics)
}
func (t *TopicTool) replaceTopics(repositories []*github.Repository, topics []string) error {
for _, repo := range repositories {
err := t.setRepositoryTopics(repo, topics)
if err != nil {
return err
}
}
return nil
}
func (t *TopicTool) AddTopics(query string, topics []string) error {
result, err := t.searchRepositories(query)
if err != nil {
return err
}
if len(result.Repositories) == 0 {
return fmt.Errorf("no repositories found matching query %s", query)
}
previewRepositories(result.Repositories)
fmt.Println("")
fmt.Printf("\n Add labels [%s] to %d repositories? [y/n/q]:\n", topicsToStr(topics), len(result.Repositories))
next, err := AskForBool(os.Stdin, true, false)
if err != nil {
print(err)
}
if !next {
return ErrAbortInput
}
return t.addTopics(result.Repositories, topics)
}
func (t *TopicTool) addTopics(repositories []*github.Repository, topicsToAppend []string) error {
for _, repo := range repositories {
repo.Topics = append(repo.Topics, topicsToAppend...)
err := t.setRepositoryTopics(repo, repo.Topics)
if err != nil {
return err
}
}
return nil
}
func (t *TopicTool) RemoveTopics(query string, topics []string) error {
result, err := t.searchRepositories(query)
if err != nil {
return err
}
if len(result.Repositories) == 0 {
return fmt.Errorf("no repositories found matching query %s", query)
}
previewRepositories(result.Repositories)
fmt.Println("")
fmt.Printf("\n Remove labels [%s] from %d repositories? [y/n/q]:\n", topicsToStr(topics), len(result.Repositories))
next, err := AskForBool(os.Stdin, true, false)
if err != nil {
print(err)
}
if !next {
return ErrAbortInput
}
return t.removeTopics(result.Repositories, topics)
}
func (t *TopicTool) removeTopics(repositories []*github.Repository, topicsToRemove []string) error {
for _, repo := range repositories {
repo.Topics = removeFromTopics(repo.Topics, topicsToRemove)
err := t.setRepositoryTopics(repo, repo.Topics)
if err != nil {
return err
}
}
return nil
}
func (t *TopicTool) setRepositoryTopics(repository *github.Repository, topics []string) error {
topics = removeDuplicateTopics(topics)
_, _, err := t.client.Repositories.ReplaceAllTopics(context.Background(), *repository.Owner.Login, *repository.Name, topics)
if err != nil {
return err
}
return nil
}
// searchRepositories searches repositories via various criteria.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/search/#search-repositories
func (t *TopicTool) searchRepositories(query string) (*github.RepositoriesSearchResult, error) {
result, _, err := t.client.Search.Repositories(context.Background(), query, &github.SearchOptions{
ListOptions: github.ListOptions{
Page: 0,
PerPage: 100,
},
})
if err != nil {
return &github.RepositoriesSearchResult{}, err
}
return result, err
}
func removeFromTopics(topics []string, toRemove []string) []string {
// todo(cbrgm): we can use a set here because order does not matter
set := make(map[string]bool)
for _, s := range topics {
set[s] = true
}
for _, s := range toRemove {
delete(set, s)
}
res := []string{}
for k, _ := range set {
res = append(res, k)
}
return res
}
func removeDuplicateTopics(topics []string) []string {
allKeys := make(map[string]bool)
list := []string{}
for _, item := range topics {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
func topicsToStr(topics []string) string {
res := strings.Join(topics, ",")
return strings.TrimRight(res, ",")
}
func previewRepositories(repos []*github.Repository) {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 10, 10, 0, '\t', 0)
defer w.Flush()
fmt.Fprintf(w, "\n %s\t%s\t", "Repository Name", "Topics")
fmt.Fprintf(w, "\n %s\t%s\t", "---", "---")
for _, repo := range repos {
fmt.Fprintf(w, "\n %s\t%s\t", fmt.Sprintf("%s/%s", *repo.Owner.Login, *repo.Name), topicsToStr(repo.Topics))
}
}