-
Notifications
You must be signed in to change notification settings - Fork 114
/
main.go
210 lines (177 loc) · 5.07 KB
/
main.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
// 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.
// This binary fetches all repos for a user from gitlab.
//
// It is recommended to use a gitlab personal access token:
// https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html. This
// token should be stored in a file and the --token option should be used.
// In addition, the token should be present in the ~/.netrc of the user running
// the mirror command. For example, the ~/.netrc may look like:
//
// machine gitlab.com
// login oauth
// password <personal access token>
//
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/google/zoekt/gitindex"
gitlab "github.com/xanzy/go-gitlab"
)
func main() {
dest := flag.String("dest", "", "destination directory")
gitlabURL := flag.String("url", "https://gitlab.com/api/v4/", "Gitlab URL. If not set https://gitlab.com/api/v4/ will be used")
token := flag.String("token",
filepath.Join(os.Getenv("HOME"), ".gitlab-token"),
"file holding API token.")
isMember := flag.Bool("membership", false, "only mirror repos this user is a member of ")
deleteRepos := flag.Bool("delete", false, "delete missing repos")
namePattern := flag.String("name", "", "only clone repos whose name matches the given regexp.")
excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.")
flag.Parse()
if *dest == "" {
log.Fatal("must set --dest")
}
var host string
rootURL, err := url.Parse(*gitlabURL)
if err != nil {
log.Fatal(err)
}
host = rootURL.Host
destDir := filepath.Join(*dest, host)
if err := os.MkdirAll(destDir, 0755); err != nil {
log.Fatal(err)
}
content, err := ioutil.ReadFile(*token)
if err != nil {
log.Fatal(err)
}
apiToken := strings.TrimSpace(string(content))
client := gitlab.NewClient(nil, apiToken)
client.SetBaseURL(*gitlabURL)
opt := &gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 10,
Page: 1,
},
Membership: isMember,
}
var gitlabProjects []*gitlab.Project
for {
projects, resp, err := client.Projects.ListProjects(opt)
if err != nil {
log.Fatal(err)
}
for _, project := range projects {
// Skip projects without a default branch - these should be projects
// where the repository isn't enabled
if project.DefaultBranch == "" {
continue
}
gitlabProjects = append(gitlabProjects, project)
}
if resp.CurrentPage >= resp.TotalPages {
break
}
opt.Page = resp.NextPage
}
filter, err := gitindex.NewFilter(*namePattern, *excludePattern)
if err != nil {
log.Fatal(err)
}
{
trimmed := gitlabProjects[:0]
for _, p := range gitlabProjects {
if filter.Include(p.NameWithNamespace) {
trimmed = append(trimmed, p)
}
}
gitlabProjects = trimmed
}
fetchProjects(destDir, apiToken, gitlabProjects)
if *deleteRepos {
if err := deleteStaleProjects(*dest, filter, gitlabProjects); err != nil {
log.Fatalf("deleteStaleProjects: %v", err)
}
}
}
func deleteStaleProjects(destDir string, filter *gitindex.Filter, projects []*gitlab.Project) error {
u, err := url.Parse(projects[0].HTTPURLToRepo)
u.Path = ""
if err != nil {
return err
}
paths, err := gitindex.ListRepos(destDir, u)
if err != nil {
return err
}
names := map[string]bool{}
for _, p := range projects {
u, err := url.Parse(p.HTTPURLToRepo)
if err != nil {
return err
}
names[filepath.Join(u.Host, u.Path)] = true
}
var toDelete []string
for _, p := range paths {
if filter.Include(p) && !names[p] {
toDelete = append(toDelete, p)
}
}
if len(toDelete) > 0 {
log.Printf("deleting repos %v", toDelete)
}
var errs []string
for _, d := range toDelete {
if err := os.RemoveAll(filepath.Join(destDir, d)); err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return fmt.Errorf("errors: %v", errs)
}
return nil
}
func fetchProjects(destDir, token string, projects []*gitlab.Project) {
for _, p := range projects {
u, err := url.Parse(p.HTTPURLToRepo)
if err != nil {
log.Printf("Unable to parse project URL: %v", err)
continue
}
config := map[string]string{
"zoekt.web-url-type": "gitlab",
"zoekt.web-url": p.WebURL,
"zoekt.name": filepath.Join(u.Hostname(), p.PathWithNamespace),
"zoekt.gitlab-stars": strconv.Itoa(p.StarCount),
"zoekt.gitlab-forks": strconv.Itoa(p.ForksCount),
}
cloneURL := p.HTTPURLToRepo
dest, err := gitindex.CloneRepo(destDir, p.PathWithNamespace, cloneURL, config)
if err != nil {
log.Printf("cloneRepos: %v", err)
continue
}
if dest != "" {
fmt.Println(dest)
}
}
}