-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathconfig.go
294 lines (268 loc) · 7.46 KB
/
config.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
)
type ConfigEntry struct {
GithubUser string
GithubOrg string
BitBucketServerProject string
GitHubURL string
GitilesURL string
CGitURL string
BitBucketServerURL string
DisableTLS bool
CredentialPath string
ProjectType string
Name string
Exclude string
GitLabURL string
OnlyPublic bool
GerritApiURL string
Topics []string
ExcludeTopics []string
Active bool
NoArchived bool
GerritFetchMetaConfig bool
GerritRepoNameFormat string
ExcludeUserRepos bool
}
func randomize(entries []ConfigEntry) []ConfigEntry {
perm := rand.Perm(len(entries))
var shuffled []ConfigEntry
for _, i := range perm {
shuffled = append(shuffled, entries[i])
}
return shuffled
}
func isHTTP(u string) bool {
asURL, err := url.Parse(u)
return err == nil && (asURL.Scheme == "http" || asURL.Scheme == "https")
}
func readConfigURL(u string) ([]ConfigEntry, error) {
var body []byte
var readErr error
if isHTTP(u) {
rep, err := http.Get(u)
if err != nil {
return nil, err
}
defer rep.Body.Close()
body, readErr = io.ReadAll(rep.Body)
} else {
body, readErr = os.ReadFile(u)
}
if readErr != nil {
return nil, readErr
}
var result []ConfigEntry
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return result, nil
}
func watchFile(path string) (<-chan struct{}, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
if err := watcher.Add(filepath.Dir(path)); err != nil {
return nil, err
}
out := make(chan struct{}, 1)
go func() {
var last time.Time
for {
select {
case <-watcher.Events:
fi, err := os.Stat(path)
if err == nil && fi.ModTime() != last {
out <- struct{}{}
last = fi.ModTime()
}
case err := <-watcher.Errors:
if err != nil {
log.Printf("watcher error: %v", err)
}
}
}
}()
return out, nil
}
func periodicMirrorFile(repoDir string, opts *Options, pendingRepos chan<- string) {
ticker := time.NewTicker(opts.mirrorInterval)
var watcher <-chan struct{}
if !isHTTP(opts.mirrorConfigFile) {
var err error
watcher, err = watchFile(opts.mirrorConfigFile)
if err != nil {
log.Printf("watchFile(%q): %v", opts.mirrorConfigFile, err)
}
}
var lastCfg []ConfigEntry
for {
cfg, err := readConfigURL(opts.mirrorConfigFile)
if err != nil {
log.Printf("readConfig(%s): %v", opts.mirrorConfigFile, err)
} else {
lastCfg = cfg
}
executeMirror(lastCfg, repoDir, pendingRepos)
select {
case <-watcher:
log.Printf("mirror config %s changed", opts.mirrorConfigFile)
case <-ticker.C:
}
}
}
func executeMirror(cfg []ConfigEntry, repoDir string, pendingRepos chan<- string) {
// Randomize the ordering in which we query
// things. This is to ensure that quota limits don't
// always hit the last one in the list.
cfg = randomize(cfg)
for _, c := range cfg {
var cmd *exec.Cmd
if c.GitHubURL != "" || c.GithubUser != "" || c.GithubOrg != "" {
cmd = exec.Command("zoekt-mirror-github",
"-dest", repoDir, "-delete")
if c.GitHubURL != "" {
cmd.Args = append(cmd.Args, "-url", c.GitHubURL)
}
if c.GithubUser != "" {
cmd.Args = append(cmd.Args, "-user", c.GithubUser)
} else if c.GithubOrg != "" {
cmd.Args = append(cmd.Args, "-org", c.GithubOrg)
}
if c.Name != "" {
cmd.Args = append(cmd.Args, "-name", c.Name)
}
if c.Exclude != "" {
cmd.Args = append(cmd.Args, "-exclude", c.Exclude)
}
if c.CredentialPath != "" {
cmd.Args = append(cmd.Args, "-token", c.CredentialPath)
}
for _, topic := range c.Topics {
cmd.Args = append(cmd.Args, "-topic", topic)
}
for _, topic := range c.ExcludeTopics {
cmd.Args = append(cmd.Args, "-exclude_topic", topic)
}
if c.NoArchived {
cmd.Args = append(cmd.Args, "-no_archived")
}
} else if c.GitilesURL != "" {
cmd = exec.Command("zoekt-mirror-gitiles",
"-dest", repoDir, "-name", c.Name)
if c.Exclude != "" {
cmd.Args = append(cmd.Args, "-exclude", c.Exclude)
}
cmd.Args = append(cmd.Args, c.GitilesURL)
} else if c.CGitURL != "" {
cmd = exec.Command("zoekt-mirror-gitiles",
"-type", "cgit",
"-dest", repoDir, "-name", c.Name)
if c.Exclude != "" {
cmd.Args = append(cmd.Args, "-exclude", c.Exclude)
}
cmd.Args = append(cmd.Args, c.CGitURL)
} else if c.BitBucketServerURL != "" {
cmd = exec.Command("zoekt-mirror-bitbucket-server",
"-dest", repoDir, "-url", c.BitBucketServerURL, "-delete")
if c.BitBucketServerProject != "" {
cmd.Args = append(cmd.Args, "-project", c.BitBucketServerProject)
}
if c.DisableTLS {
cmd.Args = append(cmd.Args, "-disable-tls")
}
if c.ProjectType != "" {
cmd.Args = append(cmd.Args, "-type", c.ProjectType)
}
if c.Name != "" {
cmd.Args = append(cmd.Args, "-name", c.Name)
}
if c.Exclude != "" {
cmd.Args = append(cmd.Args, "-exclude", c.Exclude)
}
if c.CredentialPath != "" {
cmd.Args = append(cmd.Args, "-credentials", c.CredentialPath)
}
} else if c.GitLabURL != "" {
cmd = exec.Command("zoekt-mirror-gitlab",
"-dest", repoDir, "-url", c.GitLabURL)
if c.Name != "" {
cmd.Args = append(cmd.Args, "-name", c.Name)
}
if c.Exclude != "" {
cmd.Args = append(cmd.Args, "-exclude", c.Exclude)
}
if c.OnlyPublic {
cmd.Args = append(cmd.Args, "-public")
}
if c.ExcludeUserRepos {
cmd.Args = append(cmd.Args, "-exclude_user")
}
if c.CredentialPath != "" {
cmd.Args = append(cmd.Args, "-token", c.CredentialPath)
}
if c.NoArchived {
cmd.Args = append(cmd.Args, "-no_archived")
}
} else if c.GerritApiURL != "" {
cmd = exec.Command("zoekt-mirror-gerrit",
"-dest", repoDir, "-delete")
if c.CredentialPath != "" {
cmd.Args = append(cmd.Args, "-http-credentials", c.CredentialPath)
}
if c.Name != "" {
cmd.Args = append(cmd.Args, "-name", c.Name)
}
if c.Exclude != "" {
cmd.Args = append(cmd.Args, "-exclude", c.Exclude)
}
if c.Active {
cmd.Args = append(cmd.Args, "-active")
}
if c.GerritFetchMetaConfig {
cmd.Args = append(cmd.Args, "-fetch-meta-config")
}
if c.GerritRepoNameFormat != "" {
cmd.Args = append(cmd.Args, "-repo-name-format", c.GerritRepoNameFormat)
}
cmd.Args = append(cmd.Args, c.GerritApiURL)
} else {
log.Printf("executeMirror: ignoring config, because it does not contain any valid repository definition: %v", c)
continue
}
stdout, _ := loggedRun(cmd)
for _, fn := range bytes.Split(stdout, []byte{'\n'}) {
if len(fn) == 0 {
continue
}
pendingRepos <- string(fn)
}
}
}