1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package gitlab
18
19 import (
20 "fmt"
21 "net/http"
22 )
23
24
25
26
27
28 type SearchService struct {
29 client *Client
30 }
31
32
33
34
35 type SearchOptions struct {
36 ListOptions
37 Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
38 }
39
40 type searchOptions struct {
41 SearchOptions
42 Scope string `url:"scope" json:"scope"`
43 Search string `url:"search" json:"search"`
44 }
45
46
47
48
49 func (s *SearchService) Projects(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Project, *Response, error) {
50 var ps []*Project
51 resp, err := s.search("projects", query, &ps, opt, options...)
52 return ps, resp, err
53 }
54
55
56
57
58
59 func (s *SearchService) ProjectsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Project, *Response, error) {
60 var ps []*Project
61 resp, err := s.searchByGroup(gid, "projects", query, &ps, opt, options...)
62 return ps, resp, err
63 }
64
65
66
67
68 func (s *SearchService) Issues(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Issue, *Response, error) {
69 var is []*Issue
70 resp, err := s.search("issues", query, &is, opt, options...)
71 return is, resp, err
72 }
73
74
75
76
77
78 func (s *SearchService) IssuesByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Issue, *Response, error) {
79 var is []*Issue
80 resp, err := s.searchByGroup(gid, "issues", query, &is, opt, options...)
81 return is, resp, err
82 }
83
84
85
86
87
88 func (s *SearchService) IssuesByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Issue, *Response, error) {
89 var is []*Issue
90 resp, err := s.searchByProject(pid, "issues", query, &is, opt, options...)
91 return is, resp, err
92 }
93
94
95
96
97
98 func (s *SearchService) MergeRequests(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
99 var ms []*MergeRequest
100 resp, err := s.search("merge_requests", query, &ms, opt, options...)
101 return ms, resp, err
102 }
103
104
105
106
107
108
109 func (s *SearchService) MergeRequestsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
110 var ms []*MergeRequest
111 resp, err := s.searchByGroup(gid, "merge_requests", query, &ms, opt, options...)
112 return ms, resp, err
113 }
114
115
116
117
118
119
120 func (s *SearchService) MergeRequestsByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
121 var ms []*MergeRequest
122 resp, err := s.searchByProject(pid, "merge_requests", query, &ms, opt, options...)
123 return ms, resp, err
124 }
125
126
127
128
129 func (s *SearchService) Milestones(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Milestone, *Response, error) {
130 var ms []*Milestone
131 resp, err := s.search("milestones", query, &ms, opt, options...)
132 return ms, resp, err
133 }
134
135
136
137
138
139 func (s *SearchService) MilestonesByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Milestone, *Response, error) {
140 var ms []*Milestone
141 resp, err := s.searchByGroup(gid, "milestones", query, &ms, opt, options...)
142 return ms, resp, err
143 }
144
145
146
147
148
149 func (s *SearchService) MilestonesByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Milestone, *Response, error) {
150 var ms []*Milestone
151 resp, err := s.searchByProject(pid, "milestones", query, &ms, opt, options...)
152 return ms, resp, err
153 }
154
155
156
157
158
159 func (s *SearchService) SnippetTitles(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Snippet, *Response, error) {
160 var ss []*Snippet
161 resp, err := s.search("snippet_titles", query, &ss, opt, options...)
162 return ss, resp, err
163 }
164
165
166
167
168
169 func (s *SearchService) SnippetBlobs(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Snippet, *Response, error) {
170 var ss []*Snippet
171 resp, err := s.search("snippet_blobs", query, &ss, opt, options...)
172 return ss, resp, err
173 }
174
175
176
177
178
179 func (s *SearchService) NotesByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Note, *Response, error) {
180 var ns []*Note
181 resp, err := s.searchByProject(pid, "notes", query, &ns, opt, options...)
182 return ns, resp, err
183 }
184
185
186
187
188
189 func (s *SearchService) WikiBlobs(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Wiki, *Response, error) {
190 var ws []*Wiki
191 resp, err := s.search("wiki_blobs", query, &ws, opt, options...)
192 return ws, resp, err
193 }
194
195
196
197
198
199
200 func (s *SearchService) WikiBlobsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Wiki, *Response, error) {
201 var ws []*Wiki
202 resp, err := s.searchByGroup(gid, "wiki_blobs", query, &ws, opt, options...)
203 return ws, resp, err
204 }
205
206
207
208
209
210
211 func (s *SearchService) WikiBlobsByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Wiki, *Response, error) {
212 var ws []*Wiki
213 resp, err := s.searchByProject(pid, "wiki_blobs", query, &ws, opt, options...)
214 return ws, resp, err
215 }
216
217
218
219
220 func (s *SearchService) Commits(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Commit, *Response, error) {
221 var cs []*Commit
222 resp, err := s.search("commits", query, &cs, opt, options...)
223 return cs, resp, err
224 }
225
226
227
228
229
230 func (s *SearchService) CommitsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Commit, *Response, error) {
231 var cs []*Commit
232 resp, err := s.searchByGroup(gid, "commits", query, &cs, opt, options...)
233 return cs, resp, err
234 }
235
236
237
238
239
240 func (s *SearchService) CommitsByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Commit, *Response, error) {
241 var cs []*Commit
242 resp, err := s.searchByProject(pid, "commits", query, &cs, opt, options...)
243 return cs, resp, err
244 }
245
246
247 type Blob struct {
248 Basename string `json:"basename"`
249 Data string `json:"data"`
250 Path string `json:"path"`
251 Filename string `json:"filename"`
252 ID string `json:"id"`
253 Ref string `json:"ref"`
254 Startline int `json:"startline"`
255 ProjectID int `json:"project_id"`
256 }
257
258
259
260
261 func (s *SearchService) Blobs(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Blob, *Response, error) {
262 var bs []*Blob
263 resp, err := s.search("blobs", query, &bs, opt, options...)
264 return bs, resp, err
265 }
266
267
268
269
270
271 func (s *SearchService) BlobsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Blob, *Response, error) {
272 var bs []*Blob
273 resp, err := s.searchByGroup(gid, "blobs", query, &bs, opt, options...)
274 return bs, resp, err
275 }
276
277
278
279
280
281 func (s *SearchService) BlobsByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Blob, *Response, error) {
282 var bs []*Blob
283 resp, err := s.searchByProject(pid, "blobs", query, &bs, opt, options...)
284 return bs, resp, err
285 }
286
287
288
289
290 func (s *SearchService) Users(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*User, *Response, error) {
291 var ret []*User
292 resp, err := s.search("users", query, &ret, opt, options...)
293 return ret, resp, err
294 }
295
296
297
298
299
300 func (s *SearchService) UsersByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*User, *Response, error) {
301 var ret []*User
302 resp, err := s.searchByGroup(gid, "users", query, &ret, opt, options...)
303 return ret, resp, err
304 }
305
306
307
308
309
310 func (s *SearchService) UsersByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*User, *Response, error) {
311 var ret []*User
312 resp, err := s.searchByProject(pid, "users", query, &ret, opt, options...)
313 return ret, resp, err
314 }
315
316 func (s *SearchService) search(scope, query string, result interface{}, opt *SearchOptions, options ...RequestOptionFunc) (*Response, error) {
317 opts := &searchOptions{SearchOptions: *opt, Scope: scope, Search: query}
318
319 req, err := s.client.NewRequest(http.MethodGet, "search", opts, options)
320 if err != nil {
321 return nil, err
322 }
323
324 return s.client.Do(req, result)
325 }
326
327 func (s *SearchService) searchByGroup(gid interface{}, scope, query string, result interface{}, opt *SearchOptions, options ...RequestOptionFunc) (*Response, error) {
328 group, err := parseID(gid)
329 if err != nil {
330 return nil, err
331 }
332 u := fmt.Sprintf("groups/%s/-/search", PathEscape(group))
333
334 opts := &searchOptions{SearchOptions: *opt, Scope: scope, Search: query}
335
336 req, err := s.client.NewRequest(http.MethodGet, u, opts, options)
337 if err != nil {
338 return nil, err
339 }
340
341 return s.client.Do(req, result)
342 }
343
344 func (s *SearchService) searchByProject(pid interface{}, scope, query string, result interface{}, opt *SearchOptions, options ...RequestOptionFunc) (*Response, error) {
345 project, err := parseID(pid)
346 if err != nil {
347 return nil, err
348 }
349 u := fmt.Sprintf("projects/%s/-/search", PathEscape(project))
350
351 opts := &searchOptions{SearchOptions: *opt, Scope: scope, Search: query}
352
353 req, err := s.client.NewRequest(http.MethodGet, u, opts, options)
354 if err != nil {
355 return nil, err
356 }
357
358 return s.client.Do(req, result)
359 }
360
View as plain text