1+ package org .gitlab4j .api ;
2+
3+ import java .util .List ;
4+ import java .util .stream .Stream ;
5+
6+ import org .gitlab4j .api .models .Commit ;
7+ import org .gitlab4j .api .models .Issue ;
8+ import org .gitlab4j .api .models .MergeRequest ;
9+ import org .gitlab4j .api .models .Milestone ;
10+ import org .gitlab4j .api .models .Note ;
11+ import org .gitlab4j .api .models .Project ;
12+ import org .gitlab4j .api .models .SearchBlob ;
13+ import org .gitlab4j .api .models .Snippet ;
14+ import org .gitlab4j .api .models .User ;
15+
16+ /**
17+ * This class provides an entry point to all the GitLab API Search API calls.
18+ * @see <a href="https://gitlab.com/help/api/search.md">Search API</a>
19+ */
20+ public class SearchApi extends AbstractApi {
21+
22+ public SearchApi (GitLabApi gitLabApi ) {
23+ super (gitLabApi );
24+ }
25+
26+ /**
27+ * Search globally across the GitLab instance.
28+ *
29+ * <pre><code>GitLab Endpoint: POST /search?scope=:scope&search=:search-query</code></pre>
30+ *
31+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
32+ * projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs, users
33+ * @param search the search query
34+ * @return a List containing the object type specified by the scope
35+ * @throws GitLabApiException if any exception occurs
36+ * @since GitLab 10.5
37+ */
38+ public List <?> globalSearch (SearchScope scope , String search ) throws GitLabApiException {
39+ return (globalSearch (scope , search , this .getDefaultPerPage ()).all ());
40+ }
41+
42+ /**
43+ * Search globally across the GitLab instance.
44+ *
45+ * <pre><code>GitLab Endpoint: POST /search?scope=:scope&search=:search-query</code></pre>
46+ *
47+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
48+ * projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs, users
49+ * @param search the search query
50+ * @return a Stream containing the object type specified by the scope
51+ * @throws GitLabApiException if any exception occurs
52+ * @since GitLab 10.5
53+ */
54+ public Stream <?> globalSearchStream (SearchScope scope , String search ) throws GitLabApiException {
55+ return (globalSearch (scope , search , getDefaultPerPage ()).stream ());
56+ }
57+
58+ /**
59+ * Search globally across the GitLab instance.
60+ *
61+ * <pre><code>GitLab Endpoint: POST /search?scope=:scope&search=:search-query</code></pre>
62+ *
63+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
64+ * projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs, users
65+ * @param search the search query
66+ * @param itemsPerPage the number of items that will be fetched per page
67+ * @return a Pager containing the object type specified by the scope
68+ * @throws GitLabApiException if any exception occurs
69+ * @since GitLab 10.5
70+ */
71+ public Pager <?> globalSearch (SearchScope scope , String search , int itemsPerPage ) throws GitLabApiException {
72+
73+ GitLabApiForm formData = new GitLabApiForm ()
74+ .withParam ("scope" , scope , true )
75+ .withParam ("search" , search , true );
76+
77+ switch (scope ) {
78+ case BLOBS :
79+ return (new Pager <SearchBlob >(this , SearchBlob .class , itemsPerPage , formData .asMap (), "search" ));
80+
81+ case COMMITS :
82+ return (new Pager <Commit >(this , Commit .class , itemsPerPage , formData .asMap (), "search" ));
83+
84+ case PROJECTS :
85+ return (new Pager <Project >(this , Project .class , itemsPerPage , formData .asMap (), "search" ));
86+
87+ case ISSUES :
88+ return (new Pager <Issue >(this , Issue .class , itemsPerPage , formData .asMap (), "search" ));
89+
90+ case MERGE_REQUESTS :
91+ return (new Pager <MergeRequest >(this , MergeRequest .class , itemsPerPage , formData .asMap (), "search" ));
92+
93+ case MILESTONES :
94+ return (new Pager <Milestone >(this , Milestone .class , itemsPerPage , formData .asMap (), "search" ));
95+
96+ case SNIPPET_TITLES :
97+ return (new Pager <Snippet >(this , Snippet .class , itemsPerPage , formData .asMap (), "search" ));
98+
99+ case SNIPPET_BLOBS :
100+ return (new Pager <Snippet >(this , Snippet .class , itemsPerPage , formData .asMap (), "search" ));
101+
102+ case USERS :
103+ return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (), "search" ));
104+
105+ case WIKI_BLOBS :
106+ return (new Pager <SearchBlob >(this , SearchBlob .class , itemsPerPage , formData .asMap (), "search" ));
107+
108+ default :
109+ throw new GitLabApiException ("Invalid SearchScope [" + scope + "]" );
110+ }
111+ }
112+
113+ /**
114+ * Search within the specified group. If a user is not a member of a group and the group is private,
115+ * a request on that group will result to a 404 status code.
116+ *
117+ * <pre><code>GitLab Endpoint: POST /groups/:groupId/search?scope=:scope&search=:search-query</code></pre>
118+ *
119+ * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
120+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
121+ * projects, issues, merge_requests, milestones, users
122+ * @param search the search query
123+ * @return a List containing the object type specified by the scope
124+ * @throws GitLabApiException if any exception occurs
125+ * @since GitLab 10.5
126+ */
127+ public List <?> groupSearch (Object groupIdOrPath , GroupSearchScope scope , String search ) throws GitLabApiException {
128+ return (groupSearch (groupIdOrPath , scope , search , this .getDefaultPerPage ()).all ());
129+ }
130+
131+ /**
132+ * Search within the specified group. If a user is not a member of a group and the group is private,
133+ * a request on that group will result to a 404 status code.
134+ *
135+ * <pre><code>GitLab Endpoint: POST /groups/:groupId/search?scope=:scope&search=:search-query</code></pre>
136+ *
137+ * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
138+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
139+ * projects, issues, merge_requests, milestones, users
140+ * @param search the search query
141+ * @return a Stream containing the object type specified by the scope
142+ * @throws GitLabApiException if any exception occurs
143+ * @since GitLab 10.5
144+ */
145+ public Stream <?> groupSearchStream (Object groupIdOrPath , GroupSearchScope scope , String search ) throws GitLabApiException {
146+ return (groupSearch (groupIdOrPath , scope , search , getDefaultPerPage ()).stream ());
147+ }
148+
149+ /**
150+ * Search within the specified group. If a user is not a member of a group and the group is private,
151+ * a request on that group will result to a 404 status code.
152+ *
153+ * <pre><code>GitLab Endpoint: POST /groups/:groupId/search?scope=:scope&search=:search-query</code></pre>
154+ *
155+ * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
156+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
157+ * projects, issues, merge_requests, milestones, users
158+ * @param search the search query
159+ * @param itemsPerPage the number of items that will be fetched per page
160+ * @return a Pager containing the object type specified by the scope
161+ * @throws GitLabApiException if any exception occurs
162+ * @since GitLab 10.5
163+ */
164+ public Pager <?> groupSearch (Object groupIdOrPath , GroupSearchScope scope , String search , int itemsPerPage ) throws GitLabApiException {
165+
166+ GitLabApiForm formData = new GitLabApiForm ()
167+ .withParam ("scope" , scope , true )
168+ .withParam ("search" , search , true );
169+
170+ switch (scope ) {
171+ case PROJECTS :
172+ return (new Pager <Project >(this , Project .class , itemsPerPage , formData .asMap (),
173+ "groups" , getGroupIdOrPath (groupIdOrPath ), "search" ));
174+
175+ case ISSUES :
176+ return (new Pager <Issue >(this , Issue .class , itemsPerPage , formData .asMap (),
177+ "groups" , getGroupIdOrPath (groupIdOrPath ), "search" ));
178+
179+ case MERGE_REQUESTS :
180+ return (new Pager <MergeRequest >(this , MergeRequest .class , itemsPerPage , formData .asMap (),
181+ "groups" , getGroupIdOrPath (groupIdOrPath ), "search" ));
182+
183+ case MILESTONES :
184+ return (new Pager <Milestone >(this , Milestone .class , itemsPerPage , formData .asMap (),
185+ "groups" , getGroupIdOrPath (groupIdOrPath ), "search" ));
186+
187+ case USERS :
188+ return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (),
189+ "groups" , getGroupIdOrPath (groupIdOrPath ), "search" ));
190+
191+ default :
192+ throw new GitLabApiException ("Invalid GroupSearchScope [" + scope + "]" );
193+ }
194+ }
195+
196+ /**
197+ * Search within the specified project. If a user is not a member of a project and the project is private,
198+ * a request on that project will result to a 404 status code.
199+ *
200+ * <pre><code>GitLab Endpoint: POST /projects/:projectId/search?scope=:scope&search=:search-query</code></pre>
201+ *
202+ * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
203+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
204+ * issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs, users
205+ * @param search the search query
206+ * @return a List containing the object type specified by the scope
207+ * @throws GitLabApiException if any exception occurs
208+ * @since GitLab 10.5
209+ */
210+ public List <?> projectSearch (Object projectIdOrPath , ProjectSearchScope scope , String search ) throws GitLabApiException {
211+ return (projectSearch (projectIdOrPath , scope , search , this .getDefaultPerPage ()).all ());
212+ }
213+
214+ /**
215+ * Search within the specified project. If a user is not a member of a project and the project is private,
216+ * a request on that project will result to a 404 status code.
217+ *
218+ * <pre><code>GitLab Endpoint: POST /projects/:projectId/search?scope=:scope&search=:search-query</code></pre>
219+ *
220+ * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
221+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
222+ * issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs, users
223+ * @param search the search query
224+ * @return a Stream containing the object type specified by the scope
225+ * @throws GitLabApiException if any exception occurs
226+ * @since GitLab 10.5
227+ */
228+ public Stream <?> projectSearchStream (Object projectIdOrPath , ProjectSearchScope scope , String search ) throws GitLabApiException {
229+ return (projectSearch (projectIdOrPath , scope , search , getDefaultPerPage ()).stream ());
230+ }
231+
232+ /**
233+ * Search within the specified project. If a user is not a member of a project and the project is private,
234+ * a request on that project will result to a 404 status code.
235+ *
236+ * <pre><code>GitLab Endpoint: POST /project/:projectId/search?scope=:scope&search=:search-query</code></pre>
237+ *
238+ * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
239+ * @param scope search the expression within the specified scope. Currently these scopes are supported:
240+ * issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs, users
241+ * @param search the search query
242+ * @param itemsPerPage the number of items that will be fetched per page
243+ * @return a Pager containing the object type specified by the scope
244+ * @throws GitLabApiException if any exception occurs
245+ * @since GitLab 10.5
246+ */
247+ public Pager <?> projectSearch (Object projectIdOrPath , ProjectSearchScope scope , String search , int itemsPerPage ) throws GitLabApiException {
248+
249+ GitLabApiForm formData = new GitLabApiForm ()
250+ .withParam ("scope" , scope , true )
251+ .withParam ("search" , search , true );
252+
253+ switch (scope ) {
254+ case BLOBS :
255+ return (new Pager <SearchBlob >(this , SearchBlob .class , itemsPerPage , formData .asMap (),
256+ "projects" , getProjectIdOrPath (projectIdOrPath ), "search" ));
257+
258+ case COMMITS :
259+ return (new Pager <Commit >(this , Commit .class , itemsPerPage , formData .asMap (),
260+ "projects" , getProjectIdOrPath (projectIdOrPath ), "search" ));
261+
262+ case ISSUES :
263+ return (new Pager <Issue >(this , Issue .class , itemsPerPage , formData .asMap (),
264+ "projects" , getProjectIdOrPath (projectIdOrPath ), "search" ));
265+
266+ case MERGE_REQUESTS :
267+ return (new Pager <MergeRequest >(this , MergeRequest .class , itemsPerPage , formData .asMap (),
268+ "projects" , getProjectIdOrPath (projectIdOrPath ), "search" ));
269+
270+ case MILESTONES :
271+ return (new Pager <Milestone >(this , Milestone .class , itemsPerPage , formData .asMap (),
272+ "projects" , getProjectIdOrPath (projectIdOrPath ), "search" ));
273+
274+ case NOTES :
275+ return (new Pager <Note >(this , Note .class , itemsPerPage , formData .asMap (),
276+ "projects" , getProjectIdOrPath (projectIdOrPath ), "search" ));
277+
278+ case WIKI_BLOBS :
279+ return (new Pager <SearchBlob >(this , SearchBlob .class , itemsPerPage , formData .asMap (),
280+ "projects" , getProjectIdOrPath (projectIdOrPath ), "search" ));
281+
282+ case USERS :
283+ return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (),
284+ "projects" , getProjectIdOrPath (projectIdOrPath ), "search" ));
285+
286+ default :
287+ throw new GitLabApiException ("Invalid ProjectSearchScope [" + scope + "]" );
288+ }
289+ }
290+ }
0 commit comments