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

Configure Feed

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

zoekt-mirror-github: allow cloning without github token (#937)

Make the flag "token" optional. Exit if the user provided token does not
exist (as before). Only, fallback to the token from the default location
if the user did not provide a token. Continue with an unauthenticated
GitHub client (`nil` OAuth client) if the fallback did not work (does
not exist, cannot be read, ...).

author
teleivo
committer
GitHub
date (Apr 10, 2025, 7:58 AM -0700) commit a4983601 parent 4aa57929
+32 -29
+32 -29
cmd/zoekt-mirror-github/main.go
··· 23 23 "flag" 24 24 "fmt" 25 25 "log" 26 + "net/http" 26 27 "net/url" 27 28 "os" 28 29 "path/filepath" ··· 57 58 githubURL := flag.String("url", "", "GitHub Enterprise url. If not set github.com will be used as the host.") 58 59 org := flag.String("org", "", "organization to mirror") 59 60 user := flag.String("user", "", "user to mirror") 60 - token := flag.String("token", 61 - filepath.Join(os.Getenv("HOME"), ".github-token"), 62 - "file holding API token.") 61 + token := flag.String("token", "", "file holding API token. If not set defaults to $HOME/.github-token if present, else uses unauthenticated GitHub client.") 63 62 forks := flag.Bool("forks", false, "also mirror forks.") 64 63 deleteRepos := flag.Bool("delete", false, "delete missing repos") 65 64 namePattern := flag.String("name", "", "only clone repos whose name matches the given regexp.") ··· 80 79 } 81 80 82 81 var host string 83 - var apiBaseURL string 84 82 var client *github.Client 83 + tc := newOAuthClient(token) 85 84 if *githubURL != "" { 86 85 rootURL, err := url.Parse(*githubURL) 87 86 if err != nil { ··· 92 91 if err != nil { 93 92 log.Fatal(err) 94 93 } 95 - apiBaseURL = rootURL.ResolveReference(apiPath).String() 96 - client, err = github.NewEnterpriseClient(apiBaseURL, apiBaseURL, nil) 94 + apiBaseURL := rootURL.ResolveReference(apiPath).String() 95 + client, err = github.NewEnterpriseClient(apiBaseURL, apiBaseURL, tc) 97 96 if err != nil { 98 97 log.Fatal(err) 99 98 } 100 99 } else { 101 100 host = "github.com" 102 - apiBaseURL = "https://github.com/" 103 - client = github.NewClient(nil) 101 + client = github.NewClient(tc) 104 102 } 105 103 destDir := filepath.Join(*dest, host) 106 104 if err := os.MkdirAll(destDir, 0o755); err != nil { 107 105 log.Fatal(err) 108 106 } 109 107 110 - if *token != "" { 111 - content, err := os.ReadFile(*token) 112 - if err != nil { 113 - log.Fatal(err) 114 - } 115 - 116 - ts := oauth2.StaticTokenSource( 117 - &oauth2.Token{ 118 - AccessToken: strings.TrimSpace(string(content)), 119 - }) 120 - tc := oauth2.NewClient(context.Background(), ts) 121 - if *githubURL != "" { 122 - client, err = github.NewEnterpriseClient(apiBaseURL, apiBaseURL, tc) 123 - if err != nil { 124 - log.Fatal(err) 125 - } 126 - } else { 127 - client = github.NewClient(tc) 128 - } 129 - } 130 - 131 108 reposFilters := reposFilters{ 132 109 topics: topics, 133 110 excludeTopics: excludeTopics, ··· 182 159 log.Fatalf("deleteStaleRepos: %v", err) 183 160 } 184 161 } 162 + } 163 + 164 + func newOAuthClient(token *string) *http.Client { 165 + var content []byte 166 + var err error 167 + 168 + if *token != "" { // user explicitly provided a token which must exist 169 + content, err = os.ReadFile(*token) 170 + if err != nil { 171 + log.Fatal(err) 172 + } 173 + } else { 174 + defaultToken := filepath.Join(os.Getenv("HOME"), ".github-token") 175 + content, err = os.ReadFile(defaultToken) 176 + if err != nil && os.IsNotExist(err) { // use unauthenticated client 177 + return nil 178 + } else if err != nil { 179 + log.Fatal(err) 180 + } 181 + } 182 + 183 + ts := oauth2.StaticTokenSource( 184 + &oauth2.Token{ 185 + AccessToken: strings.TrimSpace(string(content)), 186 + }) 187 + return oauth2.NewClient(context.Background(), ts) 185 188 } 186 189 187 190 func deleteStaleRepos(destDir string, filter *gitindex.Filter, repos []*github.Repository, user string) error {