Skip to content

Commit 4c557ef

Browse files
zeripatha1012112796techknowlogick
authored
Add github api token option to generate-license & generate-gitignore (#12700)
* Add github api token option to generate-license & generate-gitignore Without api toke, Will face rate limit sometimes. Signed-off-by: a1012112796 <[email protected]> * Use Basic authentication with tokens Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: a1012112796 <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent d745610 commit 4c557ef

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

build/generate-gitignores.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ import (
2121

2222
func main() {
2323
var (
24-
prefix = "gitea-gitignore"
25-
url = "https://api.github.com/repos/github/gitignore/tarball"
26-
destination = ""
24+
prefix = "gitea-gitignore"
25+
url = "https://api.github.com/repos/github/gitignore/tarball"
26+
githubApiToken = ""
27+
githubUsername = ""
28+
destination = ""
2729
)
2830

2931
flag.StringVar(&destination, "dest", "options/gitignore/", "destination for the gitignores")
32+
flag.StringVar(&githubUsername, "username", "", "github username")
33+
flag.StringVar(&githubApiToken, "token", "", "github api token")
3034
flag.Parse()
3135

3236
file, err := ioutil.TempFile(os.TempDir(), prefix)
@@ -37,12 +41,19 @@ func main() {
3741

3842
defer util.Remove(file.Name())
3943

40-
resp, err := http.Get(url)
41-
44+
req, err := http.NewRequest("GET", url, nil)
4245
if err != nil {
4346
log.Fatalf("Failed to download archive. %s", err)
4447
}
4548

49+
if len(githubApiToken) > 0 && len(githubUsername) > 0 {
50+
req.SetBasicAuth(githubUsername, githubApiToken)
51+
}
52+
53+
resp, err := http.DefaultClient.Do(req)
54+
if err != nil {
55+
log.Fatalf("Failed to download archive. %s", err)
56+
}
4657
defer resp.Body.Close()
4758

4859
if _, err := io.Copy(file, resp.Body); err != nil {

build/generate-licenses.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ import (
2121

2222
func main() {
2323
var (
24-
prefix = "gitea-licenses"
25-
url = "https://api.github.com/repos/spdx/license-list-data/tarball"
26-
destination = ""
24+
prefix = "gitea-licenses"
25+
url = "https://api.github.com/repos/spdx/license-list-data/tarball"
26+
githubApiToken = ""
27+
githubUsername = ""
28+
destination = ""
2729
)
2830

2931
flag.StringVar(&destination, "dest", "options/license/", "destination for the licenses")
32+
flag.StringVar(&githubUsername, "username", "", "github username")
33+
flag.StringVar(&githubApiToken, "token", "", "github api token")
3034
flag.Parse()
3135

3236
file, err := ioutil.TempFile(os.TempDir(), prefix)
@@ -37,8 +41,16 @@ func main() {
3741

3842
defer util.Remove(file.Name())
3943

40-
resp, err := http.Get(url)
44+
req, err := http.NewRequest("GET", url, nil)
45+
if err != nil {
46+
log.Fatalf("Failed to download archive. %s", err)
47+
}
48+
49+
if len(githubApiToken) > 0 && len(githubUsername) > 0 {
50+
req.SetBasicAuth(githubUsername, githubApiToken)
51+
}
4152

53+
resp, err := http.DefaultClient.Do(req)
4254
if err != nil {
4355
log.Fatalf("Failed to download archive. %s", err)
4456
}

0 commit comments

Comments
 (0)