/
client.go
126 lines (100 loc) · 3.01 KB
/
client.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Copyright 2021 The KodeRover Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gitlab
import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/xanzy/go-gitlab"
"github.com/koderover/zadig/pkg/tool/httpclient"
)
// TODO: LOU: unify the github/gitlab helpers
type listFunc func(options *gitlab.ListOptions) ([]interface{}, *gitlab.Response, error)
type ListOptions struct {
// Page number of the results to fetch. Default: 1
Page int
// Results per page (max 100). Default: 30
PerPage int
// NoPaginated indicates if we need to fetch all result or just one page. True means fetching just one page
NoPaginated bool
}
type Client struct {
*gitlab.Client
}
func NewClient(id int, address, accessToken, proxyAddr string, enableProxy bool) (*Client, error) {
var client *http.Client
if enableProxy {
proxyURL, err := url.Parse(proxyAddr)
if err != nil {
return nil, err
}
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
client = &http.Client{Transport: transport}
} else {
client = http.DefaultClient
}
token, err := UpdateGitlabToken(id, accessToken)
if err != nil {
return nil, fmt.Errorf("failed to refresh gitlab token, err: %s", err)
}
cli, err := gitlab.NewOAuthClient(token, gitlab.WithBaseURL(address), gitlab.WithHTTPClient(client))
if err != nil {
return nil, fmt.Errorf("failed to create gitlab client, err: %s", err)
}
return &Client{Client: cli}, nil
}
func generateProjectName(owner, repo string) string {
return fmt.Sprintf("%s/%s", owner, repo)
}
func paginated(f listFunc, opts *ListOptions) ([]interface{}, *gitlab.Response, error) {
if opts == nil {
opts = &ListOptions{}
}
if opts.Page == 0 {
opts.Page = 1
}
if opts.PerPage == 0 {
opts.PerPage = 100
}
ghOpts := &gitlab.ListOptions{Page: opts.Page, PerPage: opts.PerPage}
if opts.NoPaginated {
return f(ghOpts)
}
var all, result []interface{}
var response *gitlab.Response
var err error
for ghOpts.Page > 0 {
result, response, err = f(ghOpts)
err = wrapError(response, err)
if err != nil {
return nil, response, err
}
all = append(all, result...)
ghOpts.Page = response.NextPage
}
return all, response, err
}
func wrap(obj interface{}, res *gitlab.Response, err error) (interface{}, error) {
return obj, wrapError(res, err)
}
func wrapError(res *gitlab.Response, err error) error {
if res == nil && err != nil {
return err
}
if res.StatusCode > 399 {
body, _ := io.ReadAll(res.Body)
return httpclient.NewGenericServerResponse(res.StatusCode, res.Request.Method, string(body))
}
return nil
}