fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

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