fork of https://github.com/sourcegraph/zoekt
1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13// This binary fetches all repos for a user from gitlab.
14//
15// It is recommended to use a gitlab personal access token:
16// https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html. This
17// token should be stored in a file and the --token option should be used.
18// In addition, the token should be present in the ~/.netrc of the user running
19// the mirror command. For example, the ~/.netrc may look like:
20//
21// machine gitlab.com
22// login oauth
23// password <personal access token>
24package main
25
26import (
27 "flag"
28 "fmt"
29 "log"
30 "net/url"
31 "os"
32 "path/filepath"
33 "strconv"
34 "strings"
35
36 "github.com/sourcegraph/zoekt/gitindex"
37 gitlab "github.com/xanzy/go-gitlab"
38)
39
40func main() {
41 dest := flag.String("dest", "", "destination directory")
42 gitlabURL := flag.String("url", "https://gitlab.com/api/v4/", "Gitlab URL. If not set https://gitlab.com/api/v4/ will be used")
43 token := flag.String("token",
44 filepath.Join(os.Getenv("HOME"), ".gitlab-token"),
45 "file holding API token.")
46 isMember := flag.Bool("membership", false, "only mirror repos this user is a member of ")
47 isPublic := flag.Bool("public", false, "only mirror public repos")
48 deleteRepos := flag.Bool("delete", false, "delete missing repos")
49 excludeUserRepos := flag.Bool("exclude_user", false, "exclude user repos")
50 namePattern := flag.String("name", "", "only clone repos whose name matches the given regexp.")
51 excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.")
52 flag.Parse()
53
54 if *dest == "" {
55 log.Fatal("must set --dest")
56 }
57
58 var host string
59 rootURL, err := url.Parse(*gitlabURL)
60 if err != nil {
61 log.Fatal(err)
62 }
63 host = rootURL.Host
64
65 destDir := filepath.Join(*dest, host)
66 if err := os.MkdirAll(destDir, 0o755); err != nil {
67 log.Fatal(err)
68 }
69
70 content, err := os.ReadFile(*token)
71 if err != nil {
72 log.Fatal(err)
73 }
74 apiToken := strings.TrimSpace(string(content))
75
76 client, err := gitlab.NewClient(apiToken, gitlab.WithBaseURL(*gitlabURL))
77 if err != nil {
78 log.Fatal(err)
79 }
80
81 opt := &gitlab.ListProjectsOptions{
82 ListOptions: gitlab.ListOptions{
83 PerPage: 100,
84 },
85 Sort: gitlab.String("asc"),
86 OrderBy: gitlab.String("id"),
87 Membership: isMember,
88 }
89 if *isPublic {
90 opt.Visibility = gitlab.Visibility(gitlab.PublicVisibility)
91 }
92
93 var gitlabProjects []*gitlab.Project
94 for {
95 projects, _, err := client.Projects.ListProjects(opt)
96 if err != nil {
97 log.Fatal(err)
98 }
99
100 for _, project := range projects {
101
102 // Skip projects without a default branch - these should be projects
103 // where the repository isn't enabled
104 if project.DefaultBranch == "" {
105 continue
106 }
107 if *excludeUserRepos && project.Namespace.Kind == "user" {
108 continue
109 }
110
111 gitlabProjects = append(gitlabProjects, project)
112 }
113
114 if len(projects) == 0 {
115 break
116 }
117
118 opt.IDAfter = &projects[len(projects)-1].ID
119 }
120
121 filter, err := gitindex.NewFilter(*namePattern, *excludePattern)
122 if err != nil {
123 log.Fatal(err)
124 }
125
126 {
127 trimmed := gitlabProjects[:0]
128 for _, p := range gitlabProjects {
129 if filter.Include(p.NameWithNamespace) {
130 trimmed = append(trimmed, p)
131 }
132 }
133 gitlabProjects = trimmed
134 }
135 fetchProjects(destDir, apiToken, gitlabProjects)
136
137 if *deleteRepos {
138 if err := deleteStaleProjects(*dest, filter, gitlabProjects); err != nil {
139 log.Fatalf("deleteStaleProjects: %v", err)
140 }
141 }
142}
143
144func deleteStaleProjects(destDir string, filter *gitindex.Filter, projects []*gitlab.Project) error {
145 u, err := url.Parse(projects[0].HTTPURLToRepo)
146 u.Path = ""
147 if err != nil {
148 return err
149 }
150
151 names := map[string]struct{}{}
152 for _, p := range projects {
153 u, err := url.Parse(p.HTTPURLToRepo)
154 if err != nil {
155 return err
156 }
157
158 names[filepath.Join(u.Host, u.Path)] = struct{}{}
159 }
160
161 if err := gitindex.DeleteRepos(destDir, u, names, filter); err != nil {
162 log.Fatalf("deleteRepos: %v", err)
163 }
164 return nil
165}
166
167func fetchProjects(destDir, token string, projects []*gitlab.Project) {
168 for _, p := range projects {
169 u, err := url.Parse(p.HTTPURLToRepo)
170 if err != nil {
171 log.Printf("Unable to parse project URL: %v", err)
172 continue
173 }
174 config := map[string]string{
175 "zoekt.web-url-type": "gitlab",
176 "zoekt.web-url": p.WebURL,
177 "zoekt.name": filepath.Join(u.Hostname(), p.PathWithNamespace),
178
179 "zoekt.gitlab-stars": strconv.Itoa(p.StarCount),
180 "zoekt.gitlab-forks": strconv.Itoa(p.ForksCount),
181
182 "zoekt.archived": marshalBool(p.Archived),
183 "zoekt.fork": marshalBool(p.ForkedFromProject != nil),
184 "zoekt.public": marshalBool(p.Visibility == gitlab.PublicVisibility),
185 }
186
187 cloneURL := p.HTTPURLToRepo
188 dest, err := gitindex.CloneRepo(destDir, p.PathWithNamespace, cloneURL, config)
189 if err != nil {
190 log.Printf("cloneRepos: %v", err)
191 continue
192 }
193 if dest != "" {
194 fmt.Println(dest)
195 }
196 }
197}
198
199func marshalBool(b bool) string {
200 if b {
201 return "1"
202 }
203 return "0"
204}