-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathclient.go
More file actions
139 lines (115 loc) · 3.6 KB
/
client.go
File metadata and controls
139 lines (115 loc) · 3.6 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
package app
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/harrisoncramer/gitlab.nvim/cmd/app/git"
"github.com/hashicorp/go-retryablehttp"
gitlab "gitlab.com/gitlab-org/api/client-go"
)
type ProjectInfo struct {
ProjectId string
MergeId int64
}
/* The Client struct embeds all the methods from Gitlab for the different services */
type Client struct {
gitlab.MergeRequestsServiceInterface
gitlab.MergeRequestApprovalsServiceInterface
gitlab.DiscussionsServiceInterface
gitlab.ProjectsServiceInterface
gitlab.ProjectMembersServiceInterface
gitlab.JobsServiceInterface
gitlab.PipelinesServiceInterface
gitlab.LabelsServiceInterface
gitlab.AwardEmojiServiceInterface
gitlab.UsersServiceInterface
gitlab.DraftNotesServiceInterface
gitlab.ProjectMarkdownUploadsServiceInterface
}
/* NewClient parses and validates the project settings and initializes the Gitlab client. */
func NewClient() (*Client, error) {
if pluginOptions.GitlabUrl == "" {
return nil, errors.New("GitLab instance URL cannot be empty")
}
var apiCustUrl = fmt.Sprintf("%s/api/v4", pluginOptions.GitlabUrl)
gitlabOptions := []gitlab.ClientOptionFunc{
gitlab.WithBaseURL(apiCustUrl),
}
if pluginOptions.Debug.GitlabRequest {
gitlabOptions = append(gitlabOptions, gitlab.WithRequestLogHook(
func(l retryablehttp.Logger, r *http.Request, i int) {
logRequest("REQUEST TO GITLAB", r)
},
))
}
if pluginOptions.Debug.GitlabResponse {
gitlabOptions = append(gitlabOptions, gitlab.WithResponseLogHook(func(l retryablehttp.Logger, response *http.Response) {
logResponse("RESPONSE FROM GITLAB", response)
},
))
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: pluginOptions.ConnectionSettings.Insecure,
},
}
if proxy := pluginOptions.ConnectionSettings.Proxy; proxy != "" {
u, err := url.Parse(proxy)
if err != nil {
return nil, fmt.Errorf("parse proxy url: %w", err)
}
tr.Proxy = http.ProxyURL(u)
}
retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Transport = tr
gitlabOptions = append(gitlabOptions, gitlab.WithHTTPClient(retryClient.HTTPClient))
gitlabOptions = append(gitlabOptions, gitlab.WithoutRetries())
client, err := gitlab.NewClient(pluginOptions.AuthToken, gitlabOptions...)
if err != nil {
return nil, fmt.Errorf("failed to create client: %v", err)
}
return &Client{
client.MergeRequests,
client.MergeRequestApprovals,
client.Discussions,
client.Projects,
client.ProjectMembers,
client.Jobs,
client.Pipelines,
client.Labels,
client.AwardEmoji,
client.Users,
client.DraftNotes,
client.ProjectMarkdownUploads,
}, nil
}
/* InitProjectSettings fetch the project ID using the client */
func InitProjectSettings(c *Client, gitInfo git.GitData) (*ProjectInfo, error) {
opt := gitlab.GetProjectOptions{}
project, _, err := c.GetProject(gitInfo.ProjectPath(), &opt)
if err != nil {
return nil, fmt.Errorf("error getting project at %s: %w", gitInfo.RemoteUrl, err)
}
if project == nil {
return nil, fmt.Errorf("could not find project at %s", gitInfo.RemoteUrl)
}
projectId := fmt.Sprint(project.ID)
return &ProjectInfo{
ProjectId: projectId,
}, nil
}
/* handleError is a utililty handler that returns errors to the client along with their statuses and messages */
func handleError(w http.ResponseWriter, err error, message string, status int) {
w.WriteHeader(status)
response := ErrorResponse{
Message: message,
Details: err.Error(),
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
handleError(w, err, "Could not encode error response", http.StatusInternalServerError)
}
}