From 36b42046230cdc078744302eb47937eefb863ee5 Mon Sep 17 00:00:00 2001 From: KirCute_ECT <951206789@qq.com> Date: Sun, 16 Feb 2025 12:21:03 +0800 Subject: [PATCH] feat(github): support github proxy (#7979 close #7963) --- drivers/github/driver.go | 17 ++++++++++++++--- drivers/github/meta.go | 3 ++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/github/driver.go b/drivers/github/driver.go index 996c79c7..dee4cbbf 100644 --- a/drivers/github/driver.go +++ b/drivers/github/driver.go @@ -85,10 +85,13 @@ func (d *Github) Init(ctx context.Context) error { } d.client = base.NewRestyClient(). SetHeader("Accept", "application/vnd.github.object+json"). - SetHeader("Authorization", "Bearer "+d.Token). SetHeader("X-GitHub-Api-Version", "2022-11-28"). SetLogger(log.StandardLogger()). SetDebug(false) + token := strings.TrimSpace(d.Token) + if token != "" { + d.client = d.client.SetHeader("Authorization", "Bearer "+token) + } if d.Ref == "" { repo, err := d.getRepo() if err != nil { @@ -149,8 +152,13 @@ func (d *Github) Link(ctx context.Context, file model.Obj, args model.LinkArgs) if obj.Type == "submodule" { return nil, errors.New("cannot download a submodule") } + url := obj.DownloadURL + ghProxy := strings.TrimSpace(d.Addition.GitHubProxy) + if ghProxy != "" { + url = strings.Replace(url, "https://raw.githubusercontent.com", ghProxy, 1) + } return &model.Link{ - URL: obj.DownloadURL, + URL: url, }, nil } @@ -679,8 +687,11 @@ func (d *Github) putBlob(ctx context.Context, s model.FileStreamer, up driver.Up return "", err } req.Header.Set("Accept", "application/vnd.github+json") - req.Header.Set("Authorization", "Bearer "+d.Token) req.Header.Set("X-GitHub-Api-Version", "2022-11-28") + token := strings.TrimSpace(d.Token) + if token != "" { + req.Header.Set("Authorization", "Bearer "+token) + } req.ContentLength = length res, err := base.HttpClient.Do(req) diff --git a/drivers/github/meta.go b/drivers/github/meta.go index 0df4aa60..05e704be 100644 --- a/drivers/github/meta.go +++ b/drivers/github/meta.go @@ -7,10 +7,11 @@ import ( type Addition struct { driver.RootPath - Token string `json:"token" type:"string" required:"true"` + Token string `json:"token" type:"string"` Owner string `json:"owner" type:"string" required:"true"` Repo string `json:"repo" type:"string" required:"true"` Ref string `json:"ref" type:"string" help:"A branch, a tag or a commit SHA, main branch by default."` + GitHubProxy string `json:"gh_proxy" type:"string" help:"GitHub proxy, e.g. https://ghproxy.net/raw.githubusercontent.com or https://gh-proxy.com/raw.githubusercontent.com"` CommitterName string `json:"committer_name" type:"string"` CommitterEmail string `json:"committer_email" type:"string"` AuthorName string `json:"author_name" type:"string"`