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 <1012112796@qq.com>

* Use Basic authentication with tokens

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: a1012112796 <1012112796@qq.com>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
zeripath 2020-09-04 15:02:37 +01:00 committed by GitHub
parent d7456106bf
commit 4c557eff5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 9 deletions

View file

@ -21,12 +21,16 @@ import (
func main() { func main() {
var ( var (
prefix = "gitea-gitignore" prefix = "gitea-gitignore"
url = "https://api.github.com/repos/github/gitignore/tarball" url = "https://api.github.com/repos/github/gitignore/tarball"
destination = "" githubApiToken = ""
githubUsername = ""
destination = ""
) )
flag.StringVar(&destination, "dest", "options/gitignore/", "destination for the gitignores") flag.StringVar(&destination, "dest", "options/gitignore/", "destination for the gitignores")
flag.StringVar(&githubUsername, "username", "", "github username")
flag.StringVar(&githubApiToken, "token", "", "github api token")
flag.Parse() flag.Parse()
file, err := ioutil.TempFile(os.TempDir(), prefix) file, err := ioutil.TempFile(os.TempDir(), prefix)
@ -37,12 +41,19 @@ func main() {
defer util.Remove(file.Name()) defer util.Remove(file.Name())
resp, err := http.Get(url) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
log.Fatalf("Failed to download archive. %s", err) log.Fatalf("Failed to download archive. %s", err)
} }
if len(githubApiToken) > 0 && len(githubUsername) > 0 {
req.SetBasicAuth(githubUsername, githubApiToken)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("Failed to download archive. %s", err)
}
defer resp.Body.Close() defer resp.Body.Close()
if _, err := io.Copy(file, resp.Body); err != nil { if _, err := io.Copy(file, resp.Body); err != nil {

View file

@ -21,12 +21,16 @@ import (
func main() { func main() {
var ( var (
prefix = "gitea-licenses" prefix = "gitea-licenses"
url = "https://api.github.com/repos/spdx/license-list-data/tarball" url = "https://api.github.com/repos/spdx/license-list-data/tarball"
destination = "" githubApiToken = ""
githubUsername = ""
destination = ""
) )
flag.StringVar(&destination, "dest", "options/license/", "destination for the licenses") flag.StringVar(&destination, "dest", "options/license/", "destination for the licenses")
flag.StringVar(&githubUsername, "username", "", "github username")
flag.StringVar(&githubApiToken, "token", "", "github api token")
flag.Parse() flag.Parse()
file, err := ioutil.TempFile(os.TempDir(), prefix) file, err := ioutil.TempFile(os.TempDir(), prefix)
@ -37,8 +41,16 @@ func main() {
defer util.Remove(file.Name()) defer util.Remove(file.Name())
resp, err := http.Get(url) req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalf("Failed to download archive. %s", err)
}
if len(githubApiToken) > 0 && len(githubUsername) > 0 {
req.SetBasicAuth(githubUsername, githubApiToken)
}
resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Fatalf("Failed to download archive. %s", err) log.Fatalf("Failed to download archive. %s", err)
} }