-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.go
85 lines (62 loc) · 1.61 KB
/
gitlab.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
func NewGitlabClient(url string) GitlabClient {
return GitlabClient{
URL: url,
HTTPClient: http.DefaultClient,
Logger: log.New(ioutil.Discard, "", log.Lshortfile|log.Ldate|log.Ltime),
}
}
type GitlabClient struct {
URL string
HTTPClient *http.Client
Logger *log.Logger
}
func (gitlab *GitlabClient) CILint(fileContents []byte) (CILintResponse, error) {
var (
err error
urlStr = gitlab.URL + "/api/v4/ci/lint"
lintRequest = CILintRequest{
Content: string(fileContents),
}
requestBody []byte
request *http.Request
responseBody []byte
response *http.Response
ciLintResponse CILintResponse
)
gitlab.Logger.Println("gitlab url:", urlStr)
if requestBody, err = json.Marshal(lintRequest); err != nil {
return CILintResponse{}, err
}
request, err = http.NewRequest("POST", urlStr, bytes.NewBuffer(requestBody))
if err != nil {
return CILintResponse{}, err
}
request.Header.Set("Content-Type", "application/json")
if response, err = gitlab.HTTPClient.Do(request); err != nil {
return CILintResponse{}, err
}
defer response.Body.Close()
if responseBody, err = ioutil.ReadAll(response.Body); err != nil {
return CILintResponse{}, err
}
gitlab.Logger.Println("response body:", string(responseBody))
if err = json.Unmarshal(responseBody, &ciLintResponse); err != nil {
return CILintResponse{}, err
}
return ciLintResponse, nil
}
type CILintResponse struct {
Status string `status`
Errors []string `errors`
}
type CILintRequest struct {
Content string `json:"content"`
}