Skip to content

Commit

Permalink
fix(github): use BasicAuthTransport (#1568)
Browse files Browse the repository at this point in the history
Co-authored-by: Jian Zeng <[email protected]>
  • Loading branch information
caicloud-bot and Jian Zeng authored Feb 3, 2021
1 parent 423a0e4 commit cd7801c
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions pkg/server/biz/scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit cd7801c

Please sign in to comment.