GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( cd6b32...09e2c6 )
by Jackson
08:24
created

providers.mockResponse   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
package providers
2
3
import (
4
	"bytes"
5
	"fmt"
6
	"io/ioutil"
7
	"net/http"
8
	"net/url"
9
	"strconv"
10
	"strings"
11
	"time"
12
13
	"github.com/alibabacloud-go/debug/debug"
14
	"github.com/aliyun/credentials-go/credentials/internal/utils"
15
	"github.com/aliyun/credentials-go/credentials/request"
16
	"github.com/aliyun/credentials-go/credentials/response"
17
)
18
19
var debuglog = debug.Init("credential")
20
21
func mockResponse(statusCode int, content string) (res *http.Response) {
22
	status := strconv.Itoa(statusCode)
23
	res = &http.Response{
24
		Proto:      "HTTP/1.1",
25
		ProtoMajor: 1,
26
		Header:     map[string][]string{"sdk": {"test"}},
27
		StatusCode: statusCode,
28
		Status:     status + " " + http.StatusText(statusCode),
29
	}
30
	res.Body = ioutil.NopCloser(bytes.NewReader([]byte(content)))
31
	return
32
}
33
34
func doAction(request *request.CommonRequest, runtime *utils.Runtime) (content []byte, err error) {
35
	var urlEncoded string
36
	if request.BodyParams != nil {
37
		urlEncoded = utils.GetURLFormedMap(request.BodyParams)
38
	}
39
	httpRequest, err := http.NewRequest(request.Method, request.URL, strings.NewReader(urlEncoded))
40
	if err != nil {
41
		return
42
	}
43
	httpRequest.Proto = "HTTP/1.1"
44
	httpRequest.Host = request.Domain
45
	debuglog("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto)
46
	debuglog("> Host: %s", httpRequest.Host)
47
	for key, value := range request.Headers {
48
		if value != "" {
49
			debuglog("> %s: %s", key, value)
50
			httpRequest.Header[key] = []string{value}
51
		}
52
	}
53
	debuglog(">")
54
	httpClient := &http.Client{}
55
	httpClient.Timeout = time.Duration(runtime.ReadTimeout) * time.Second
56
	proxy := &url.URL{}
57
	if runtime.Proxy != "" {
58
		proxy, err = url.Parse(runtime.Proxy)
59
		if err != nil {
60
			return
61
		}
62
	}
63
	trans := &http.Transport{}
64
	if proxy != nil && runtime.Proxy != "" {
65
		trans.Proxy = http.ProxyURL(proxy)
66
	}
67
	trans.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second)
68
	httpClient.Transport = trans
69
	httpResponse, err := hookDo(httpClient.Do)(httpRequest)
70
	if err != nil {
71
		return
72
	}
73
	debuglog("< %s %s", httpResponse.Proto, httpResponse.Status)
74
	for key, value := range httpResponse.Header {
75
		debuglog("< %s: %v", key, strings.Join(value, ""))
76
	}
77
	debuglog("<")
78
79
	resp := &response.CommonResponse{}
80
	err = hookParse(resp.ParseFromHTTPResponse(httpResponse))
81
	if err != nil {
82
		return
83
	}
84
	debuglog("%s", resp.GetHTTPContentString())
85
	if resp.GetHTTPStatus() != http.StatusOK {
86
		err = fmt.Errorf("httpStatus: %d, message = %s", resp.GetHTTPStatus(), resp.GetHTTPContentString())
87
		return
88
	}
89
	return resp.GetHTTPContentBytes(), nil
90
}
91