Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group file uploads and list uploads #1184

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/main/java/org/gitlab4j/api/GroupApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2438,4 +2438,84 @@ public GroupHook addWebhook(Object groupIdOrPath, GroupHookParams groupHookParam
Response.Status.CREATED, groupHookParams.getForm(), "groups", getGroupIdOrPath(groupIdOrPath), "hooks");
return (response.readEntity(GroupHook.class));
}

/**
* Get all uploads of the group sorted by created_at in descending order.
*
* You must have at least the Maintainer role to use this endpoint.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/uploads</code></pre>
*
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance, required
* @return list of uploaded files
* @throws GitLabApiException if any exception occurs
*/
public List<UploadedFile> getUploadFiles(Object groupIdOrPath) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", getGroupIdOrPath(groupIdOrPath), "uploads");
return (response.readEntity(new GenericType<List<UploadedFile>>() {}));
}

/**
* Uploads a file to the specified group to be used in an issue or merge request description, or a comment.
*
* <pre><code>GitLab Endpoint: POST /groups/:id/uploads</code></pre>
*
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance, required
* @param fileToUpload the File instance of the file to upload, required
* @return a FileUpload instance with information on the just uploaded file
* @throws GitLabApiException if any exception occurs
*/
public FileUpload uploadFile(Object groupIdOrPath, File fileToUpload) throws GitLabApiException {
return (uploadFile(groupIdOrPath, fileToUpload, null));
}

/**
* Uploads a file to the specified group to be used in an issue or merge request description, or a comment.
*
* <pre><code>GitLab Endpoint: POST /groups/:id/uploads</code></pre>
*
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance, required
* @param fileToUpload the File instance of the file to upload, required
* @param mediaType unused; will be removed in the next major version
* @return a FileUpload instance with information on the just uploaded file
* @throws GitLabApiException if any exception occurs
*/
public FileUpload uploadFile(Object groupIdOrPath, File fileToUpload, String mediaType) throws GitLabApiException {
Response response = upload(
Response.Status.CREATED,
"file",
fileToUpload,
mediaType,
"groups",
getGroupIdOrPath(groupIdOrPath),
"uploads");
return (response.readEntity(FileUpload.class));
}

/**
* Uploads some data in an {@link InputStream} to the specified group,
* to be used in an issue or merge request description, or a comment.
*
* <pre><code>GitLab Endpoint: POST /groups/:id/uploads</code></pre>
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance, required
* @param inputStream the data to upload, required
* @param filename The filename of the file to upload
* @param mediaType unused; will be removed in the next major version
* @return a FileUpload instance with information on the just uploaded file
* @throws GitLabApiException if any exception occurs
*/
public FileUpload uploadFile(Object groupIdOrPath, InputStream inputStream, String filename, String mediaType)
throws GitLabApiException {
Response response = upload(
Response.Status.CREATED,
"file",
inputStream,
filename,
mediaType,
"groups",
getGroupIdOrPath(groupIdOrPath),
"uploads");
return (response.readEntity(FileUpload.class));
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/gitlab4j/api/ProjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.gitlab4j.api.models.PushRules;
import org.gitlab4j.api.models.RemoteMirror;
import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.UploadedFile;
import org.gitlab4j.api.models.Variable;
import org.gitlab4j.api.models.Visibility;
import org.gitlab4j.api.utils.ISO8601;
Expand Down Expand Up @@ -3040,6 +3041,22 @@ public Project unarchiveProject(Object projectIdOrPath) throws GitLabApiExceptio
return (response.readEntity(Project.class));
}

/**
* Get all uploads of the project sorted by created_at in descending order.
*
* You must have at least the Maintainer role to use this endpoint.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/uploads</code></pre>
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance, required
* @return list of uploaded files
* @throws GitLabApiException if any exception occurs
*/
public List<UploadedFile> getUploadFiles(Object projectIdOrPath) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "uploads");
return (response.readEntity(new GenericType<List<UploadedFile>>() {}));
}

/**
* Uploads a file to the specified project to be used in an issue or merge request description, or a comment.
*
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/gitlab4j/api/models/UploadedByUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.gitlab4j.api.models;

import java.io.Serializable;

import org.gitlab4j.api.utils.JacksonJson;

public class UploadedByUser implements Serializable {
private static final long serialVersionUID = 1L;

private Long id;
private String username;
private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
61 changes: 61 additions & 0 deletions src/main/java/org/gitlab4j/api/models/UploadedFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.gitlab4j.api.models;

import java.io.Serializable;
import java.util.Date;

import org.gitlab4j.api.utils.JacksonJson;

public class UploadedFile implements Serializable {
private static final long serialVersionUID = 1L;

private Long id;
private Long size;
private String filename;
private Date createdAt;
private UploadedByUser uploadedBy;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getSize() {
return size;
}

public void setSize(Long size) {
this.size = size;
}

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public Date getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public UploadedByUser getUploadedBy() {
return uploadedBy;
}

public void setUploadedBy(UploadedByUser uploadedBy) {
this.uploadedBy = uploadedBy;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
106 changes: 7 additions & 99 deletions src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,106 +33,8 @@
import java.util.List;
import java.util.Map;

import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Application;
import org.gitlab4j.api.models.ApplicationSettings;
import org.gitlab4j.api.models.ApprovalRule;
import org.gitlab4j.api.models.ApprovalState;
import org.gitlab4j.api.models.ArtifactsFile;
import org.gitlab4j.api.models.AuditEvent;
import org.gitlab4j.api.models.AwardEmoji;
import org.gitlab4j.api.models.Badge;
import org.gitlab4j.api.models.Blame;
import org.gitlab4j.api.models.Board;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Bridge;
import org.gitlab4j.api.models.ChildEpic;
import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CommitPayload;
import org.gitlab4j.api.models.CommitStatus;
import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.Contributor;
import org.gitlab4j.api.models.CreatedChildEpic;
import org.gitlab4j.api.models.DeployKey;
import org.gitlab4j.api.models.DeployToken;
import org.gitlab4j.api.models.Deployment;
import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Discussion;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.Environment;
import org.gitlab4j.api.models.Epic;
import org.gitlab4j.api.models.EpicIssue;
import org.gitlab4j.api.models.EpicIssueLink;
import org.gitlab4j.api.models.Event;
import org.gitlab4j.api.models.ExportStatus;
import org.gitlab4j.api.models.ExternalStatusCheck;
import org.gitlab4j.api.models.ExternalStatusCheckResult;
import org.gitlab4j.api.models.ExternalStatusCheckStatus;
import org.gitlab4j.api.models.FileUpload;
import org.gitlab4j.api.models.GitLabCiTemplate;
import org.gitlab4j.api.models.GitLabCiTemplateElement;
import org.gitlab4j.api.models.GpgSignature;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.GroupAccessToken;
import org.gitlab4j.api.models.GroupHook;
import org.gitlab4j.api.models.HealthCheckInfo;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImportStatus;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.IssueLink;
import org.gitlab4j.api.models.IssuesStatistics;
import org.gitlab4j.api.models.Iteration;
import org.gitlab4j.api.models.Job;
import org.gitlab4j.api.models.Key;
import org.gitlab4j.api.models.Label;
import org.gitlab4j.api.models.LabelEvent;
import org.gitlab4j.api.models.LdapGroupLink;
import org.gitlab4j.api.models.Link;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.MergeRequestDiff;
import org.gitlab4j.api.models.MergeRequestVersion;
import org.gitlab4j.api.models.Metadata;
import org.gitlab4j.api.models.Milestone;
import org.gitlab4j.api.models.Note;
import org.gitlab4j.api.models.NotificationSettings;
import org.gitlab4j.api.models.OauthTokenResponse;
import org.gitlab4j.api.models.*;
import org.gitlab4j.api.models.Package;
import org.gitlab4j.api.models.PackageFile;
import org.gitlab4j.api.models.PersonalAccessToken;
import org.gitlab4j.api.models.Pipeline;
import org.gitlab4j.api.models.PipelineSchedule;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectAccessToken;
import org.gitlab4j.api.models.ProjectApprovalsConfig;
import org.gitlab4j.api.models.ProjectFetches;
import org.gitlab4j.api.models.ProjectGroup;
import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.ProtectedBranch;
import org.gitlab4j.api.models.ProtectedTag;
import org.gitlab4j.api.models.PushRules;
import org.gitlab4j.api.models.RegistryRepository;
import org.gitlab4j.api.models.RelatedEpic;
import org.gitlab4j.api.models.RelatedEpicLink;
import org.gitlab4j.api.models.Release;
import org.gitlab4j.api.models.RemoteMirror;
import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.models.Runner;
import org.gitlab4j.api.models.RunnerDetail;
import org.gitlab4j.api.models.SamlGroupLink;
import org.gitlab4j.api.models.SearchBlob;
import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.SshKey;
import org.gitlab4j.api.models.SystemHook;
import org.gitlab4j.api.models.Tag;
import org.gitlab4j.api.models.Todo;
import org.gitlab4j.api.models.Topic;
import org.gitlab4j.api.models.TreeItem;
import org.gitlab4j.api.models.Trigger;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Variable;
import org.gitlab4j.api.services.JiraService;
import org.gitlab4j.api.services.SlackService;
import org.gitlab4j.api.webhook.ExternalStatusCheckEvent;
Expand Down Expand Up @@ -802,6 +704,12 @@ public void testUser() throws Exception {
assertTrue(compareJson(user, "user.json"));
}

@Test
public void testUploadedFile() throws Exception {
UploadedFile uploadedFile = unmarshalResource(UploadedFile.class, "uploaded-file.json");
assertTrue(compareJson(uploadedFile, "uploaded-file.json"));
}

@Test
public void testImpersonationToken() throws Exception {
ImpersonationToken token = unmarshalResource(ImpersonationToken.class, "impersonation-token.json");
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/org/gitlab4j/api/uploaded-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 1623,
"size": 435,
"filename": "some-feature.png",
"created_at": "2024-09-23T13:52:15Z",
"uploaded_by": {
"id": 54,
"username": "john.smith",
"name": "John Smith"
}
}
Loading