From cd7801cd14c1ae1e12b8c0992cf1a65e91ba3e9b Mon Sep 17 00:00:00 2001 From: caicloud-bot Date: Wed, 3 Feb 2021 12:37:08 +0800 Subject: [PATCH] fix(github): use BasicAuthTransport (#1568) Co-authored-by: Jian Zeng --- pkg/server/biz/scm/github/github.go | 33 +++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/pkg/server/biz/scm/github/github.go b/pkg/server/biz/scm/github/github.go index fc824bea9..811d3560a 100644 --- a/pkg/server/biz/scm/github/github.go +++ b/pkg/server/biz/scm/github/github.go @@ -22,7 +22,6 @@ import ( "fmt" "io/ioutil" "net/http" - "net/url" "reflect" "regexp" "strings" @@ -427,22 +426,19 @@ func (g *Github) GetPullRequestSHA(repoURL string, number int) (string, error) { // with OAuth token. // Refer to https://developer.github.com/v3/auth/#basic-authentication func newClientByBasicAuth(server, username, password string) (*github.Client, error) { - transport := &http.Transport{ - Proxy: func(req *http.Request) (*url.URL, error) { - req.SetBasicAuth(username, password) - return nil, nil - }, - } - - httpClient := &http.Client{ - Transport: transport, - } - if !isPublic(server) { - transport.TLSClientConfig = &tls.Config{ - InsecureSkipVerify: true, + transport := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, } - client, err := github.NewEnterpriseClient(server, server, httpClient) + client, err := github.NewEnterpriseClient(server, server, &http.Client{ + Transport: &github.BasicAuthTransport{ + Username: username, + Password: password, + Transport: transport, + }, + }) if err != nil { log.Error("Fail to new client for enterprise Github as %v", err) return nil, err @@ -451,7 +447,12 @@ func newClientByBasicAuth(server, username, password string) (*github.Client, er return client, nil } - return github.NewClient(httpClient), nil + return github.NewClient(&http.Client{ + Transport: &github.BasicAuthTransport{ + Username: username, + Password: password, + }, + }), nil } // newClientByToken news Github client by token.