-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab_client.go
More file actions
220 lines (192 loc) · 6.05 KB
/
gitlab_client.go
File metadata and controls
220 lines (192 loc) · 6.05 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package module
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// GitLabPipeline represents a GitLab CI pipeline.
type GitLabPipeline struct {
ID int `json:"id"`
Status string `json:"status"`
Ref string `json:"ref"`
SHA string `json:"sha"`
WebURL string `json:"web_url"`
CreatedAt string `json:"created_at"`
}
// GitLabMergeRequest represents a GitLab merge request.
type GitLabMergeRequest struct {
ID int `json:"id"`
IID int `json:"iid"`
Title string `json:"title"`
State string `json:"state"`
SourceBranch string `json:"source_branch"`
TargetBranch string `json:"target_branch"`
WebURL string `json:"web_url"`
}
// MROptions holds options for creating a merge request.
type MROptions struct {
SourceBranch string
TargetBranch string
Title string
Description string
Labels []string
}
// GitLabClient is a lightweight GitLab REST API v4 client.
// When baseURL is "mock://", all methods return canned responses for testing.
type GitLabClient struct {
baseURL string
token string
http *http.Client
}
// NewGitLabClient creates a new GitLabClient.
func NewGitLabClient(baseURL, token string) *GitLabClient {
return &GitLabClient{
baseURL: strings.TrimRight(baseURL, "/"),
token: token,
http: &http.Client{
Timeout: 30 * time.Second,
},
}
}
func (c *GitLabClient) isMock() bool {
return strings.HasPrefix(c.baseURL, "mock:") // TrimRight removes trailing slashes
}
// TriggerPipeline triggers a pipeline for the given project ref.
func (c *GitLabClient) TriggerPipeline(projectID, ref string, variables map[string]string) (*GitLabPipeline, error) {
if c.isMock() {
return &GitLabPipeline{
ID: 42,
Status: "created",
Ref: ref,
SHA: "abc123def456",
WebURL: "https://gitlab.example.com/" + projectID + "/-/pipelines/42",
CreatedAt: time.Now().UTC().Format(time.RFC3339),
}, nil
}
body := map[string]any{"ref": ref}
if len(variables) > 0 {
vars := make([]map[string]string, 0, len(variables))
for k, v := range variables {
vars = append(vars, map[string]string{"key": k, "value": v})
}
body["variables"] = vars
}
encoded := encodeProjectID(projectID)
resp, err := c.doRequest("POST", fmt.Sprintf("/api/v4/projects/%s/pipeline", encoded), body)
if err != nil {
return nil, err
}
var pipeline GitLabPipeline
if err := json.Unmarshal(resp, &pipeline); err != nil {
return nil, fmt.Errorf("gitlab: failed to decode pipeline response: %w", err)
}
return &pipeline, nil
}
// GetPipeline retrieves the status of a pipeline by ID.
func (c *GitLabClient) GetPipeline(projectID string, pipelineID int) (*GitLabPipeline, error) {
if c.isMock() {
return &GitLabPipeline{
ID: pipelineID,
Status: "success",
Ref: "main",
SHA: "abc123def456",
WebURL: fmt.Sprintf("https://gitlab.example.com/%s/-/pipelines/%d", projectID, pipelineID),
CreatedAt: time.Now().UTC().Format(time.RFC3339),
}, nil
}
encoded := encodeProjectID(projectID)
resp, err := c.doRequest("GET", fmt.Sprintf("/api/v4/projects/%s/pipelines/%d", encoded, pipelineID), nil)
if err != nil {
return nil, err
}
var pipeline GitLabPipeline
if err := json.Unmarshal(resp, &pipeline); err != nil {
return nil, fmt.Errorf("gitlab: failed to decode pipeline response: %w", err)
}
return &pipeline, nil
}
// CreateMergeRequest creates a merge request in the given project.
func (c *GitLabClient) CreateMergeRequest(projectID string, opts MROptions) (*GitLabMergeRequest, error) {
if c.isMock() {
return &GitLabMergeRequest{
ID: 100,
IID: 1,
Title: opts.Title,
State: "opened",
SourceBranch: opts.SourceBranch,
TargetBranch: opts.TargetBranch,
WebURL: "https://gitlab.example.com/" + projectID + "/-/merge_requests/1",
}, nil
}
body := map[string]any{
"source_branch": opts.SourceBranch,
"target_branch": opts.TargetBranch,
"title": opts.Title,
}
if opts.Description != "" {
body["description"] = opts.Description
}
if len(opts.Labels) > 0 {
body["labels"] = strings.Join(opts.Labels, ",")
}
encoded := encodeProjectID(projectID)
resp, err := c.doRequest("POST", fmt.Sprintf("/api/v4/projects/%s/merge_requests", encoded), body)
if err != nil {
return nil, err
}
var mr GitLabMergeRequest
if err := json.Unmarshal(resp, &mr); err != nil {
return nil, fmt.Errorf("gitlab: failed to decode MR response: %w", err)
}
return &mr, nil
}
// CommentOnMR posts a note (comment) on a merge request.
func (c *GitLabClient) CommentOnMR(projectID string, mrIID int, body string) error {
if c.isMock() {
return nil
}
encoded := encodeProjectID(projectID)
_, err := c.doRequest("POST", fmt.Sprintf("/api/v4/projects/%s/merge_requests/%d/notes", encoded, mrIID),
map[string]any{"body": body})
return err
}
// doRequest performs an authenticated HTTP request to the GitLab API.
func (c *GitLabClient) doRequest(method, path string, payload any) ([]byte, error) {
var bodyReader io.Reader
if payload != nil {
data, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("gitlab: failed to encode request: %w", err)
}
bodyReader = bytes.NewReader(data)
}
req, err := http.NewRequest(method, c.baseURL+path, bodyReader)
if err != nil {
return nil, fmt.Errorf("gitlab: failed to create request: %w", err)
}
req.Header.Set("PRIVATE-TOKEN", c.token)
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("gitlab: request failed: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("gitlab: failed to read response: %w", err)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("gitlab: API error %d: %s", resp.StatusCode, string(respBody))
}
return respBody, nil
}
// encodeProjectID URL-encodes a project path (e.g. "group/project" → "group%2Fproject").
func encodeProjectID(id string) string {
return strings.ReplaceAll(id, "/", "%2F")
}