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// Command zoekt-mirror-gitlab 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// Command mirror. 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 "time"
36
37 "github.com/sourcegraph/zoekt/internal/gitindex"
38 gitlab "github.com/xanzy/go-gitlab"
39)
40
41func main() {
42 dest := flag.String("dest", "", "destination directory")
43 gitlabURL := flag.String("url", "https://gitlab.com/api/v4/", "Gitlab URL. If not set https://gitlab.com/api/v4/ will be used")
44 token := flag.String("token",
45 filepath.Join(os.Getenv("HOME"), ".gitlab-token"),
46 "file holding API token.")
47 isMember := flag.Bool("membership", false, "only mirror repos this user is a member of ")
48 isPublic := flag.Bool("public", false, "only mirror public repos")
49 deleteRepos := flag.Bool("delete", false, "delete missing repos")
50 excludeUserRepos := flag.Bool("exclude_user", false, "exclude user repos")
51 namePattern := flag.String("name", "", "only clone repos whose name matches the given regexp.")
52 excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.")
53 lastActivityAfter := flag.String("last_activity_after", "", "only mirror repos that have been active since this date (format: 2006-01-02).")
54 noArchived := flag.Bool("no_archived", false, "mirror only projects that are not archived")
55
56 flag.Parse()
57
58 if *dest == "" {
59 log.Fatal("must set --dest")
60 }
61
62 var host string
63 rootURL, err := url.Parse(*gitlabURL)
64 if err != nil {
65 log.Fatal(err)
66 }
67 host = rootURL.Host
68
69 destDir := filepath.Join(*dest, host)
70 if err := os.MkdirAll(destDir, 0o755); err != nil {
71 log.Fatal(err)
72 }
73
74 content, err := os.ReadFile(*token)
75 if err != nil {
76 log.Fatal(err)
77 }
78 apiToken := strings.TrimSpace(string(content))
79
80 client, err := gitlab.NewClient(apiToken, gitlab.WithBaseURL(*gitlabURL))
81 if err != nil {
82 log.Fatal(err)
83 }
84
85 opt := &gitlab.ListProjectsOptions{
86 ListOptions: gitlab.ListOptions{
87 PerPage: 100,
88 },
89 Sort: gitlab.String("asc"),
90 OrderBy: gitlab.String("id"),
91 Membership: isMember,
92 }
93 if *isPublic {
94 opt.Visibility = gitlab.Visibility(gitlab.PublicVisibility)
95 }
96
97 if *lastActivityAfter != "" {
98 targetDate, err := time.Parse("2006-01-02", *lastActivityAfter)
99 if err != nil {
100 log.Fatal(err)
101 }
102 opt.LastActivityAfter = gitlab.Time(targetDate)
103 }
104
105 if *noArchived {
106 opt.Archived = gitlab.Bool(false)
107 }
108
109 var gitlabProjects []*gitlab.Project
110 for {
111 projects, _, err := client.Projects.ListProjects(opt)
112 if err != nil {
113 log.Fatal(err)
114 }
115
116 for _, project := range projects {
117
118 // Skip projects without a default branch - these should be projects
119 // where the repository isn't enabled
120 if project.DefaultBranch == "" {
121 continue
122 }
123 if *excludeUserRepos && project.Namespace.Kind == "user" {
124 continue
125 }
126
127 gitlabProjects = append(gitlabProjects, project)
128 }
129
130 if len(projects) == 0 {
131 break
132 }
133
134 opt.IDAfter = &projects[len(projects)-1].ID
135 }
136
137 filter, err := gitindex.NewFilter(*namePattern, *excludePattern)
138 if err != nil {
139 log.Fatal(err)
140 }
141
142 {
143 trimmed := gitlabProjects[:0]
144 for _, p := range gitlabProjects {
145 if filter.Include(p.NameWithNamespace) {
146 trimmed = append(trimmed, p)
147 }
148 }
149 gitlabProjects = trimmed
150 }
151 fetchProjects(destDir, apiToken, gitlabProjects)
152
153 if *deleteRepos {
154 if err := deleteStaleProjects(*dest, filter, gitlabProjects); err != nil {
155 log.Fatalf("deleteStaleProjects: %v", err)
156 }
157 }
158}
159
160func deleteStaleProjects(destDir string, filter *gitindex.Filter, projects []*gitlab.Project) error {
161 u, err := url.Parse(projects[0].HTTPURLToRepo)
162 u.Path = ""
163 if err != nil {
164 return err
165 }
166
167 names := map[string]struct{}{}
168 for _, p := range projects {
169 u, err := url.Parse(p.HTTPURLToRepo)
170 if err != nil {
171 return err
172 }
173
174 names[filepath.Join(u.Host, u.Path)] = struct{}{}
175 }
176
177 if err := gitindex.DeleteRepos(destDir, u, names, filter); err != nil {
178 log.Fatalf("deleteRepos: %v", err)
179 }
180 return nil
181}
182
183func fetchProjects(destDir, token string, projects []*gitlab.Project) {
184 for _, p := range projects {
185 u, err := url.Parse(p.HTTPURLToRepo)
186 if err != nil {
187 log.Printf("Unable to parse project URL: %v", err)
188 continue
189 }
190 config := map[string]string{
191 "zoekt.web-url-type": "gitlab",
192 "zoekt.web-url": p.WebURL,
193 "zoekt.name": filepath.Join(u.Hostname(), p.PathWithNamespace),
194
195 "zoekt.gitlab-stars": strconv.Itoa(p.StarCount),
196 "zoekt.gitlab-forks": strconv.Itoa(p.ForksCount),
197
198 "zoekt.archived": marshalBool(p.Archived),
199 "zoekt.fork": marshalBool(p.ForkedFromProject != nil),
200 "zoekt.public": marshalBool(p.Visibility == gitlab.PublicVisibility),
201 }
202
203 cloneURL := p.HTTPURLToRepo
204 dest, err := gitindex.CloneRepo(destDir, p.PathWithNamespace, cloneURL, config)
205 if err != nil {
206 log.Printf("cloneRepos: %v", err)
207 continue
208 }
209 if dest != "" {
210 fmt.Println(dest)
211 }
212 }
213}
214
215func marshalBool(b bool) string {
216 if b {
217 return "1"
218 }
219 return "0"
220}