Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bb6bfa9

Browse files
committedOct 16, 2024·
Add group file uploads
1 parent 7197da9 commit bb6bfa9

File tree

6 files changed

+218
-99
lines changed

6 files changed

+218
-99
lines changed
 

‎src/main/java/org/gitlab4j/api/GroupApi.java

+80
Original file line numberDiff line numberDiff line change
@@ -2438,4 +2438,84 @@ public GroupHook addWebhook(Object groupIdOrPath, GroupHookParams groupHookParam
24382438
Response.Status.CREATED, groupHookParams.getForm(), "groups", getGroupIdOrPath(groupIdOrPath), "hooks");
24392439
return (response.readEntity(GroupHook.class));
24402440
}
2441+
2442+
/**
2443+
* Get all uploads of the group sorted by created_at in descending order.
2444+
*
2445+
* You must have at least the Maintainer role to use this endpoint.
2446+
*
2447+
* <pre><code>GitLab Endpoint: GET /groups/:id/uploads</code></pre>
2448+
*
2449+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance, required
2450+
* @return list of uploaded files
2451+
* @throws GitLabApiException if any exception occurs
2452+
*/
2453+
public List<UploadedFile> getUploadFiles(Object groupIdOrPath) throws GitLabApiException {
2454+
Response response = get(Response.Status.OK, null, "projects", getGroupIdOrPath(groupIdOrPath), "uploads");
2455+
return (response.readEntity(new GenericType<List<UploadedFile>>() {}));
2456+
}
2457+
2458+
/**
2459+
* Uploads a file to the specified group to be used in an issue or merge request description, or a comment.
2460+
*
2461+
* <pre><code>GitLab Endpoint: POST /groups/:id/uploads</code></pre>
2462+
*
2463+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance, required
2464+
* @param fileToUpload the File instance of the file to upload, required
2465+
* @return a FileUpload instance with information on the just uploaded file
2466+
* @throws GitLabApiException if any exception occurs
2467+
*/
2468+
public FileUpload uploadFile(Object groupIdOrPath, File fileToUpload) throws GitLabApiException {
2469+
return (uploadFile(groupIdOrPath, fileToUpload, null));
2470+
}
2471+
2472+
/**
2473+
* Uploads a file to the specified group to be used in an issue or merge request description, or a comment.
2474+
*
2475+
* <pre><code>GitLab Endpoint: POST /groups/:id/uploads</code></pre>
2476+
*
2477+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance, required
2478+
* @param fileToUpload the File instance of the file to upload, required
2479+
* @param mediaType unused; will be removed in the next major version
2480+
* @return a FileUpload instance with information on the just uploaded file
2481+
* @throws GitLabApiException if any exception occurs
2482+
*/
2483+
public FileUpload uploadFile(Object groupIdOrPath, File fileToUpload, String mediaType) throws GitLabApiException {
2484+
Response response = upload(
2485+
Response.Status.CREATED,
2486+
"file",
2487+
fileToUpload,
2488+
mediaType,
2489+
"groups",
2490+
getGroupIdOrPath(groupIdOrPath),
2491+
"uploads");
2492+
return (response.readEntity(FileUpload.class));
2493+
}
2494+
2495+
/**
2496+
* Uploads some data in an {@link InputStream} to the specified group,
2497+
* to be used in an issue or merge request description, or a comment.
2498+
*
2499+
* <pre><code>GitLab Endpoint: POST /groups/:id/uploads</code></pre>
2500+
*
2501+
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance, required
2502+
* @param inputStream the data to upload, required
2503+
* @param filename The filename of the file to upload
2504+
* @param mediaType unused; will be removed in the next major version
2505+
* @return a FileUpload instance with information on the just uploaded file
2506+
* @throws GitLabApiException if any exception occurs
2507+
*/
2508+
public FileUpload uploadFile(Object groupIdOrPath, InputStream inputStream, String filename, String mediaType)
2509+
throws GitLabApiException {
2510+
Response response = upload(
2511+
Response.Status.CREATED,
2512+
"file",
2513+
inputStream,
2514+
filename,
2515+
mediaType,
2516+
"groups",
2517+
getGroupIdOrPath(groupIdOrPath),
2518+
"uploads");
2519+
return (response.readEntity(FileUpload.class));
2520+
}
24412521
}

‎src/main/java/org/gitlab4j/api/ProjectApi.java

+17
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.gitlab4j.api.models.PushRules;
6868
import org.gitlab4j.api.models.RemoteMirror;
6969
import org.gitlab4j.api.models.Snippet;
70+
import org.gitlab4j.api.models.UploadedFile;
7071
import org.gitlab4j.api.models.Variable;
7172
import org.gitlab4j.api.models.Visibility;
7273
import org.gitlab4j.api.utils.ISO8601;
@@ -3040,6 +3041,22 @@ public Project unarchiveProject(Object projectIdOrPath) throws GitLabApiExceptio
30403041
return (response.readEntity(Project.class));
30413042
}
30423043

3044+
/**
3045+
* Get all uploads of the project sorted by created_at in descending order.
3046+
*
3047+
* You must have at least the Maintainer role to use this endpoint.
3048+
*
3049+
* <pre><code>GitLab Endpoint: GET /groups/:id/uploads</code></pre>
3050+
*
3051+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance, required
3052+
* @return list of uploaded files
3053+
* @throws GitLabApiException if any exception occurs
3054+
*/
3055+
public List<UploadedFile> getUploadFiles(Object projectIdOrPath) throws GitLabApiException {
3056+
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "uploads");
3057+
return (response.readEntity(new GenericType<List<UploadedFile>>() {}));
3058+
}
3059+
30433060
/**
30443061
* Uploads a file to the specified project to be used in an issue or merge request description, or a comment.
30453062
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.io.Serializable;
4+
5+
import org.gitlab4j.api.utils.JacksonJson;
6+
7+
public class UploadedByUser implements Serializable {
8+
private static final long serialVersionUID = 1L;
9+
10+
private Long id;
11+
private String username;
12+
private String name;
13+
14+
public Long getId() {
15+
return id;
16+
}
17+
18+
public void setId(Long id) {
19+
this.id = id;
20+
}
21+
22+
public String getUsername() {
23+
return username;
24+
}
25+
26+
public void setUsername(String username) {
27+
this.username = username;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return (JacksonJson.toJsonString(this));
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.io.Serializable;
4+
import java.util.Date;
5+
6+
import org.gitlab4j.api.utils.JacksonJson;
7+
8+
public class UploadedFile implements Serializable {
9+
private static final long serialVersionUID = 1L;
10+
11+
private Long id;
12+
private Long size;
13+
private String filename;
14+
private Date createdAt;
15+
private UploadedByUser uploadedBy;
16+
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public void setId(Long id) {
22+
this.id = id;
23+
}
24+
25+
public Long getSize() {
26+
return size;
27+
}
28+
29+
public void setSize(Long size) {
30+
this.size = size;
31+
}
32+
33+
public String getFilename() {
34+
return filename;
35+
}
36+
37+
public void setFilename(String filename) {
38+
this.filename = filename;
39+
}
40+
41+
public Date getCreatedAt() {
42+
return createdAt;
43+
}
44+
45+
public void setCreatedAt(Date createdAt) {
46+
this.createdAt = createdAt;
47+
}
48+
49+
public UploadedByUser getUploadedBy() {
50+
return uploadedBy;
51+
}
52+
53+
public void setUploadedBy(UploadedByUser uploadedBy) {
54+
this.uploadedBy = uploadedBy;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return (JacksonJson.toJsonString(this));
60+
}
61+
}

‎src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

+7-99
Original file line numberDiff line numberDiff line change
@@ -33,106 +33,8 @@
3333
import java.util.List;
3434
import java.util.Map;
3535

36-
import org.gitlab4j.api.models.AccessRequest;
37-
import org.gitlab4j.api.models.Application;
38-
import org.gitlab4j.api.models.ApplicationSettings;
39-
import org.gitlab4j.api.models.ApprovalRule;
40-
import org.gitlab4j.api.models.ApprovalState;
41-
import org.gitlab4j.api.models.ArtifactsFile;
42-
import org.gitlab4j.api.models.AuditEvent;
43-
import org.gitlab4j.api.models.AwardEmoji;
44-
import org.gitlab4j.api.models.Badge;
45-
import org.gitlab4j.api.models.Blame;
46-
import org.gitlab4j.api.models.Board;
47-
import org.gitlab4j.api.models.Branch;
48-
import org.gitlab4j.api.models.Bridge;
49-
import org.gitlab4j.api.models.ChildEpic;
50-
import org.gitlab4j.api.models.Comment;
51-
import org.gitlab4j.api.models.Commit;
52-
import org.gitlab4j.api.models.CommitPayload;
53-
import org.gitlab4j.api.models.CommitStatus;
54-
import org.gitlab4j.api.models.CompareResults;
55-
import org.gitlab4j.api.models.Contributor;
56-
import org.gitlab4j.api.models.CreatedChildEpic;
57-
import org.gitlab4j.api.models.DeployKey;
58-
import org.gitlab4j.api.models.DeployToken;
59-
import org.gitlab4j.api.models.Deployment;
60-
import org.gitlab4j.api.models.Diff;
61-
import org.gitlab4j.api.models.Discussion;
62-
import org.gitlab4j.api.models.Email;
63-
import org.gitlab4j.api.models.Environment;
64-
import org.gitlab4j.api.models.Epic;
65-
import org.gitlab4j.api.models.EpicIssue;
66-
import org.gitlab4j.api.models.EpicIssueLink;
67-
import org.gitlab4j.api.models.Event;
68-
import org.gitlab4j.api.models.ExportStatus;
69-
import org.gitlab4j.api.models.ExternalStatusCheck;
70-
import org.gitlab4j.api.models.ExternalStatusCheckResult;
71-
import org.gitlab4j.api.models.ExternalStatusCheckStatus;
72-
import org.gitlab4j.api.models.FileUpload;
73-
import org.gitlab4j.api.models.GitLabCiTemplate;
74-
import org.gitlab4j.api.models.GitLabCiTemplateElement;
75-
import org.gitlab4j.api.models.GpgSignature;
76-
import org.gitlab4j.api.models.Group;
77-
import org.gitlab4j.api.models.GroupAccessToken;
78-
import org.gitlab4j.api.models.GroupHook;
79-
import org.gitlab4j.api.models.HealthCheckInfo;
80-
import org.gitlab4j.api.models.ImpersonationToken;
81-
import org.gitlab4j.api.models.ImportStatus;
82-
import org.gitlab4j.api.models.Issue;
83-
import org.gitlab4j.api.models.IssueLink;
84-
import org.gitlab4j.api.models.IssuesStatistics;
85-
import org.gitlab4j.api.models.Iteration;
86-
import org.gitlab4j.api.models.Job;
87-
import org.gitlab4j.api.models.Key;
88-
import org.gitlab4j.api.models.Label;
89-
import org.gitlab4j.api.models.LabelEvent;
90-
import org.gitlab4j.api.models.LdapGroupLink;
91-
import org.gitlab4j.api.models.Link;
92-
import org.gitlab4j.api.models.Member;
93-
import org.gitlab4j.api.models.MergeRequest;
94-
import org.gitlab4j.api.models.MergeRequestDiff;
95-
import org.gitlab4j.api.models.MergeRequestVersion;
96-
import org.gitlab4j.api.models.Metadata;
97-
import org.gitlab4j.api.models.Milestone;
98-
import org.gitlab4j.api.models.Note;
99-
import org.gitlab4j.api.models.NotificationSettings;
100-
import org.gitlab4j.api.models.OauthTokenResponse;
36+
import org.gitlab4j.api.models.*;
10137
import org.gitlab4j.api.models.Package;
102-
import org.gitlab4j.api.models.PackageFile;
103-
import org.gitlab4j.api.models.PersonalAccessToken;
104-
import org.gitlab4j.api.models.Pipeline;
105-
import org.gitlab4j.api.models.PipelineSchedule;
106-
import org.gitlab4j.api.models.Project;
107-
import org.gitlab4j.api.models.ProjectAccessToken;
108-
import org.gitlab4j.api.models.ProjectApprovalsConfig;
109-
import org.gitlab4j.api.models.ProjectFetches;
110-
import org.gitlab4j.api.models.ProjectGroup;
111-
import org.gitlab4j.api.models.ProjectHook;
112-
import org.gitlab4j.api.models.ProjectUser;
113-
import org.gitlab4j.api.models.ProtectedBranch;
114-
import org.gitlab4j.api.models.ProtectedTag;
115-
import org.gitlab4j.api.models.PushRules;
116-
import org.gitlab4j.api.models.RegistryRepository;
117-
import org.gitlab4j.api.models.RelatedEpic;
118-
import org.gitlab4j.api.models.RelatedEpicLink;
119-
import org.gitlab4j.api.models.Release;
120-
import org.gitlab4j.api.models.RemoteMirror;
121-
import org.gitlab4j.api.models.RepositoryFile;
122-
import org.gitlab4j.api.models.Runner;
123-
import org.gitlab4j.api.models.RunnerDetail;
124-
import org.gitlab4j.api.models.SamlGroupLink;
125-
import org.gitlab4j.api.models.SearchBlob;
126-
import org.gitlab4j.api.models.Snippet;
127-
import org.gitlab4j.api.models.SshKey;
128-
import org.gitlab4j.api.models.SystemHook;
129-
import org.gitlab4j.api.models.Tag;
130-
import org.gitlab4j.api.models.Todo;
131-
import org.gitlab4j.api.models.Topic;
132-
import org.gitlab4j.api.models.TreeItem;
133-
import org.gitlab4j.api.models.Trigger;
134-
import org.gitlab4j.api.models.User;
135-
import org.gitlab4j.api.models.Variable;
13638
import org.gitlab4j.api.services.JiraService;
13739
import org.gitlab4j.api.services.SlackService;
13840
import org.gitlab4j.api.webhook.ExternalStatusCheckEvent;
@@ -802,6 +704,12 @@ public void testUser() throws Exception {
802704
assertTrue(compareJson(user, "user.json"));
803705
}
804706

707+
@Test
708+
public void testUploadedFile() throws Exception {
709+
UploadedFile uploadedFile = unmarshalResource(UploadedFile.class, "uploaded-file.json");
710+
assertTrue(compareJson(uploadedFile, "uploaded-file.json"));
711+
}
712+
805713
@Test
806714
public void testImpersonationToken() throws Exception {
807715
ImpersonationToken token = unmarshalResource(ImpersonationToken.class, "impersonation-token.json");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"id": 1623,
3+
"size": 435,
4+
"filename": "some-feature.png",
5+
"created_at": "2024-09-23T13:52:15Z",
6+
"uploaded_by": {
7+
"id": 54,
8+
"username": "john.smith",
9+
"name": "John Smith"
10+
}
11+
}

0 commit comments

Comments
 (0)
Please sign in to comment.