forked from gitlab4j/gitlab4j-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectApi.java
463 lines (413 loc) · 16.4 KB
/
ProjectApi.java
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package com.messners.gitlab.api;
import com.messners.gitlab.api.models.Event;
import com.messners.gitlab.api.models.Member;
import com.messners.gitlab.api.models.Project;
import com.messners.gitlab.api.models.ProjectHook;
import org.glassfish.jersey.uri.UriComponent;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
/**
* This class provides an entry point to all the GitLab API project calls.
*/
public class ProjectApi extends AbstractApi {
ProjectApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get a list of projects accessible by the authenticated user.
*
* GET /projects
*
* @return a list of projects accessible by the authenticated user
* @throws GitLabApiException
*/
public List<Project> getProjects() throws GitLabApiException {
Response response = get(Response.Status.OK, UriComponent.decodeQuery("per_page=9999", true), "projects");
return (response.readEntity(new GenericType<List<Project>>() {
}));
}
/**
* Get a list of all GitLab projects (admin only).
*
* GET /projects/all
*
* @return a list of all GitLab projects
* @throws GitLabApiException
*/
public List<Project> getAllProjects() throws GitLabApiException {
Response response = get(Response.Status.OK, UriComponent.decodeQuery("per_page=9999", true), "projects", "all");
return (response.readEntity(new GenericType<List<Project>>() {
}));
}
/**
* Get a list of projects owned by the authenticated user.
*
* GET /projects/owned
*
* @return a list of projects owned by the authenticated user
* @throws GitLabApiException
*/
public List<Project> getOwnedProjects() throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", "owned");
return (response.readEntity(new GenericType<List<Project>>() {
}));
}
/**
* Get a specific project, which is owned by the authentication user.
*
* GET /projects/:id
*
* @param projectId
* @return the specified project
* @throws GitLabApiException
*/
public Project getProject(Integer projectId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId);
return (response.readEntity(Project.class));
}
/**
* Get a specific project, which is owned by the authentication user.
*
* GET /projects/:id
*
* @param namespace the name of the project namespace or group
* @param project
* @return the specified project
* @throws GitLabApiException
*/
public Project getProject(String namespace, String project) throws GitLabApiException {
if (namespace == null) {
throw new RuntimeException("namespace cannot be null");
}
if (project == null) {
throw new RuntimeException("project cannot be null");
}
String pid = null;
try {
pid = URLEncoder.encode(namespace + "/" + project, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw (new GitLabApiException(uee));
}
Response response = get(Response.Status.OK, null, "projects", pid);
return (response.readEntity(Project.class));
}
/**
* Create a new project in the specified group.
*
* @param groupId
* @param projectName
* @return the created project
* @throws GitLabApiException
*/
public Project createProject(Integer groupId, String projectName) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "namespace_id", groupId);
addFormParam(formData, "name", projectName, true);
Response response = post(Response.Status.CREATED, formData, "projects");
return (response.readEntity(Project.class));
}
/**
* Creates new project owned by the current user.
*
* @param project the Project instance with the configuration for the new project
* @return a Project instance with the newly created project info
* @throws GitLabApiException
*/
public Project createProject(Project project) throws GitLabApiException {
return (createProject(project, null));
}
/**
* Creates new project owned by the current user. The following properties on the Project instance
* are utilized in the creation of the project:
*
* name (required) - new project name
* description (optional) - short project description
* issuesEnabled (optional)
* wallEnabled (optional)
* mergeRequestsEnabled (optional)
* wikiEnabled (optional)
* snippetsEnabled (optional)
* isPublic (optional) - if true same as setting visibility_level = 20
* visibilityLevel (optional)
*
* @param project the Project instance with the configuration for the new project
* @param importUrl
* @return a Project instance with the newly created project info
* @throws GitLabApiException
*/
public Project createProject(Project project, String importUrl) throws GitLabApiException {
if (project == null) {
return (null);
}
String name = project.getName();
if (name == null || name.trim().length() == 0) {
return (null);
}
Form formData = new Form();
if (project.getNamespace() != null) {
addFormParam(formData, "namespace_id", project.getNamespace().getId());
}
addFormParam(formData, "name", name, true);
addFormParam(formData, "description", project.getDescription());
addFormParam(formData, "issues_enabled", project.getIssuesEnabled());
addFormParam(formData, "wall_enabled", project.getWallEnabled());
addFormParam(formData, "merge_requests_enabled", project.getMergeRequestsEnabled());
addFormParam(formData, "wiki_enabled", project.getWikiEnabled());
addFormParam(formData, "snippets_enabled", project.getSnippetsEnabled());
addFormParam(formData, "public", project.getPublic());
addFormParam(formData, "visibility_level", project.getVisibilityLevel());
addFormParam(formData, "import_url", importUrl);
Response response = post(Response.Status.CREATED, formData, "projects");
return (response.readEntity(Project.class));
}
/**
* Creates a Project
*
* @param name The name of the project
* @param namespaceId The Namespace for the new project, otherwise null indicates to use the GitLab default (user)
* @param description A description for the project, null otherwise
* @param issuesEnabled Whether Issues should be enabled, otherwise null indicates to use GitLab default
* @param wallEnabled Whether The Wall should be enabled, otherwise null indicates to use GitLab default
* @param mergeRequestsEnabled Whether Merge Requests should be enabled, otherwise null indicates to use GitLab default
* @param wikiEnabled Whether a Wiki should be enabled, otherwise null indicates to use GitLab default
* @param snippetsEnabled Whether Snippets should be enabled, otherwise null indicates to use GitLab default
* @param publik Whether the project is public or private, if true same as setting visibilityLevel = 20, otherwise null indicates to use GitLab default
* @param visibilityLevel The visibility level of the project, otherwise null indicates to use GitLab default
* @param importUrl The Import URL for the project, otherwise null
* @return the Gitlab Project
* @throws GitLabApiException
*/
public Project createProject(String name, Integer namespaceId, String description, Boolean issuesEnabled, Boolean wallEnabled, Boolean mergeRequestsEnabled,
Boolean wikiEnabled, Boolean snippetsEnabled, Boolean publik, Integer visibilityLevel, String importUrl) throws GitLabApiException {
if (name == null || name.trim().length() == 0) {
return (null);
}
Form formData = new Form();
addFormParam(formData, "name", name, true);
addFormParam(formData, "namespace_id", namespaceId);
addFormParam(formData, "description", description);
addFormParam(formData, "issues_enabled", issuesEnabled);
addFormParam(formData, "wall_enabled", wallEnabled);
addFormParam(formData, "merge_requests_enabled", mergeRequestsEnabled);
addFormParam(formData, "wiki_enabled", wikiEnabled);
addFormParam(formData, "snippets_enabled", snippetsEnabled);
addFormParam(formData, "public", publik);
addFormParam(formData, "visibility_level", visibilityLevel);
addFormParam(formData, "import_url", importUrl);
Response response = post(Response.Status.CREATED, formData, "projects");
return (response.readEntity(Project.class));
}
/**
* Removes project with all resources(issues, merge requests etc).
*
* DELETE /projects/:id
*
* @param projectId
* @throws GitLabApiException
*/
public void deleteProject(Integer projectId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
delete(Response.Status.OK, null, "projects", projectId);
}
/**
* Removes project with all resources(issues, merge requests etc).
*
* DELETE /projects/:id
*
* @param project
* @throws GitLabApiException
*/
public void deleteProject(Project project) throws GitLabApiException {
deleteProject(project.getId());
}
/**
* Get a list of project team members.
*
* GET /projects/:id/members
*
* @param projectId
* @return the members belonging to the specified project
* @throws GitLabApiException
*/
public List<Member> getMembers(Integer projectId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "members");
return (response.readEntity(new GenericType<List<Member>>() {
}));
}
/**
* Gets a project team member.
*
* GET /projects/:id/members/:user_id
*
* @param projectId
* @param userId
* @return the member specified by the project ID/user ID pair
* @throws GitLabApiException
*/
public Member getMember(Integer projectId, Integer userId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "members", userId);
return (response.readEntity(Member.class));
}
/**
* Adds a user to a project team. This is an idempotent method and can be called multiple times
* with the same parameters. Adding team membership to a user that is already a member does not
* affect the existing membership.
*
* POST /projects/:id/members
*
* @param projectId
* @param userId
* @param accessLevel
* @return the added member
* @throws GitLabApiException
*/
public Member addMember(Integer projectId, Integer userId, Integer accessLevel) throws GitLabApiException {
Form formData = new Form();
formData.param("user_id", userId.toString());
formData.param("access_level", accessLevel.toString());
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "members");
return (response.readEntity(Member.class));
}
/**
* Removes user from project team.
*
* DELETE /projects/:id/members/:user_id
*
* @param projectId
* @param userId
* @throws GitLabApiException
*/
public void removeMember(Integer projectId, Integer userId) throws GitLabApiException {
delete(Response.Status.OK, null, "projects", projectId, "members", userId);
}
/**
* Get a project events for specific project. Sorted from newest to latest.
*
* GET /projects/:id/events
*
* @param projectId
* @return the project events for the specified project
* @throws GitLabApiException
*/
public List<Event> getProjectEvents(Integer projectId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "events");
return (response.readEntity(new GenericType<List<Event>>() {
}));
}
/**
* Get list of project hooks.
*
* GET /projects/:id/hooks
*
* @param projectId
* @return a list of project hooks for the specified project
* @throws GitLabApiException
*/
public List<ProjectHook> getHooks(Integer projectId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "hooks");
return (response.readEntity(new GenericType<List<ProjectHook>>() {
}));
}
/**
* Get a specific hook for project.
*
* GET /projects/:id/hooks/:hook_id
*
* @param projectId
* @param hookId
* @return the project hook for the specified project ID/hook ID pair
* @throws GitLabApiException
*/
public ProjectHook getHook(Integer projectId, Integer hookId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "hooks", hookId);
return (response.readEntity(ProjectHook.class));
}
/**
* Adds a hook to project.
*
* POST /projects/:id/hooks
*
* @param project
* @param url
* @param doPushEvents
* @param doIssuesEvents
* @param doMergeRequestsEvents
* @return the added project hook
* @throws GitLabApiException
*/
public ProjectHook addHook(Project project, String url, boolean doPushEvents, boolean doIssuesEvents, boolean doMergeRequestsEvents) throws GitLabApiException {
if (project == null) {
return (null);
}
return (addHook(project.getId(), url, doPushEvents, doIssuesEvents, doMergeRequestsEvents));
}
/**
* Adds a hook to project.
*
* POST /projects/:id/hooks
*
* @param projectId
* @param url
* @param doPushEvents
* @param doIssuesEvents
* @param doMergeRequestsEvents
* @return the added project hook
* @throws GitLabApiException
*/
public ProjectHook addHook(Integer projectId, String url, boolean doPushEvents, boolean doIssuesEvents, boolean doMergeRequestsEvents) throws GitLabApiException {
Form formData = new Form();
formData.param("url", url);
formData.param("push_events", Boolean.toString(doPushEvents));
formData.param("issues_enabled", Boolean.toString(doIssuesEvents));
formData.param("merge_requests_events", Boolean.toString(doMergeRequestsEvents));
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "hooks");
return (response.readEntity(ProjectHook.class));
}
/**
* Deletes a hook from the project.
*
* DELETE /projects/:id/hooks/:hook_id
*
* @param projectId
* @param hookId
* @throws GitLabApiException
*/
public void deleteHook(Integer projectId, Integer hookId) throws GitLabApiException {
delete(Response.Status.OK, null, "projects", projectId, "hooks", hookId);
}
/**
* Deletes a hook from the project.
*
* DELETE /projects/:id/hooks/:hook_id
*
* @param hook
* @throws GitLabApiException
*/
public void deleteHook(ProjectHook hook) throws GitLabApiException {
deleteHook(hook.getProjectId(), hook.getId());
}
/**
* Modifies a hook for project.
*
* PUT /projects/:id/hooks/:hook_id
*
* @param hook
* @return the modified project hook
* @throws GitLabApiException
*/
public ProjectHook modifyHook(ProjectHook hook) throws GitLabApiException {
Form formData = new Form();
formData.param("url", hook.getUrl());
formData.param("push_events", hook.getPushEvents().toString());
formData.param("issues_enabled", hook.getIssuesEvents().toString());
formData.param("merge_requests_events", hook.getMergeRequestsEvents().toString());
Response response = put(Response.Status.OK, formData.asMap(), "projects", hook.getProjectId(), "hooks", hook.getId());
return (response.readEntity(ProjectHook.class));
}
}