Skip to content

Commit

Permalink
Add API-based avatar retrieval (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
sichapman authored Apr 12, 2024
1 parent c3a0341 commit 5c891fe
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/main/java/org/gitlab4j/api/GroupApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gitlab4j.api;

import java.io.File;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
Expand All @@ -11,6 +12,7 @@

import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

Expand Down Expand Up @@ -1881,6 +1883,24 @@ public Group setGroupAvatar(Object groupIdOrPath, File avatarFile) throws GitLab
return (response.readEntity(Group.class));
}

/**
* Gets the group avatar.
* Only working with GitLab 14.0 and above.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/avatar</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @return an InputStream to read the raw file from
* @throws GitLabApiException if any exception occurs
*/
public InputStream getAvatar(Object groupIdOrPath) throws GitLabApiException {

Response response = getWithAccepts(Response.Status.OK, null, MediaType.MEDIA_TYPE_WILDCARD,
"groups", getGroupIdOrPath(groupIdOrPath), "avatar");
return (response.readEntity(InputStream.class));

}

/**
* Share group with another group. Returns 200 and the group details on success.
*
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/gitlab4j/api/ProjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.stream.Stream;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
Expand Down Expand Up @@ -1068,6 +1069,24 @@ public Project createProject(Project project, String importUrl) throws GitLabApi
return (response.readEntity(Project.class));
}

/**
* Gets the project avatar.
* Only working with GitLab 16.9 and above.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/avatar</code></pre>
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @return an InputStream to read the raw file from
* @throws GitLabApiException if any exception occurs
*/
public InputStream getAvatar(Object projectIdOrPath) throws GitLabApiException {

Response response = getWithAccepts(Response.Status.OK, null, MediaType.MEDIA_TYPE_WILDCARD,
"projects", getProjectIdOrPath(projectIdOrPath), "avatar");
return (response.readEntity(InputStream.class));

}

/**
* Creates a Project
*
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/gitlab4j/api/TestAvatarUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.File;
import java.util.Map;

import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.User;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -45,16 +46,18 @@ public class TestAvatarUpload extends AbstractIntegrationTest {

private static GitLabApi gitLabApi;
private static Project testProject;
private static Group testGroup;

public TestAvatarUpload() {
super();
}

@BeforeAll
public static void setup() {
// Must setup the connection to the GitLab test server and get the test Project instance
// Must setup the connection to the GitLab test server and get the test Project and Group instances
gitLabApi = baseTestSetup();
testProject = getTestProject();
testGroup = getTestGroup();
}

@Test
Expand Down Expand Up @@ -100,4 +103,15 @@ public void testSetUserAvatar() throws GitLabApiException {
assertNotNull(updatedUser);
assertTrue(updatedUser.getAvatarUrl().endsWith(AVATAR_FILENAME));
}

@Test
public void testSetGroupAvatar() throws GitLabApiException {

assumeTrue(testGroup != null);

File avatarFile = new File("src/test/resources/org/gitlab4j/api", AVATAR_FILENAME);
Group updatedGroup = gitLabApi.getGroupApi().setGroupAvatar(testGroup.getId(), avatarFile);
assertNotNull(updatedGroup);
assertTrue(updatedGroup.getAvatarUrl().endsWith(AVATAR_FILENAME));
}
}
28 changes: 28 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGroupApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
Expand All @@ -21,6 +27,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -46,6 +53,8 @@ public class TestGroupApi extends AbstractIntegrationTest {
private static final String TEST_GROUP_MEMBER_USERNAME = HelperUtils.getProperty(GROUP_MEMBER_USERNAME_KEY);
private static final String TEST_REQUEST_ACCESS_USERNAME = HelperUtils.getProperty(TEST_REQUEST_ACCESS_USERNAME_KEY);

private static final String AVATAR_FILENAME = "avatar.png";

private static GitLabApi gitLabApi;
private static Group testGroup;
private static User testUser;
Expand Down Expand Up @@ -196,6 +205,25 @@ public void getOptionalGroup() {
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus());
}

@Test
@Disabled("Required Gitlab version not less then 14.0")
public void testGetAvatar() throws GitLabApiException, IOException {

assumeTrue(testGroup != null);

File avatarFile = new File("src/test/resources/org/gitlab4j/api", AVATAR_FILENAME);
gitLabApi.getGroupApi().setGroupAvatar(testGroup.getId(), avatarFile);

// Get the avatar of the test Group
InputStream in = gitLabApi.getGroupApi().getAvatar(testGroup);

Path target = Files.createTempFile(TEST_PROJECT_NAME + "-avatar", "png");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);

assertTrue(target.toFile().length() > 0);
Files.delete(target);
}


@Test
public void testRequestAccess() throws GitLabApiException {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/gitlab4j/api/TestProjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -56,6 +62,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -94,6 +101,8 @@ public class TestProjectApi extends AbstractIntegrationTest {
private static final String TEST_XFER_PROJECT_NAME = "test-gitlab4j-xfer-project";
private static final String TEST_VARIABLE_KEY_PREFIX = "TEST_VARIABLE_KEY_";

private static final String AVATAR_FILENAME = "avatar.png";

private static GitLabApi gitLabApi;
private static Project testProject;
private static User currentUser;
Expand Down Expand Up @@ -311,6 +320,25 @@ else if (TEST_PROJECT_NAME_2.equals(project.getName()))
assertEquals(TEST_PROJECT_NAME_1, projects.get(1).getName());
}

@Test
@Disabled("Required Gitlab version not less then 16.9")
public void testGetAvatar() throws GitLabApiException, IOException {

assumeTrue(testProject != null);

File avatarFile = new File("src/test/resources/org/gitlab4j/api", AVATAR_FILENAME);
gitLabApi.getProjectApi().setProjectAvatar(testProject.getId(), avatarFile);

// Get the avatar of the test project
InputStream in = gitLabApi.getProjectApi().getAvatar(testProject);

Path target = Files.createTempFile(TEST_PROJECT_NAME + "-avatar", "png");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);

assertTrue(target.toFile().length() > 0);
Files.delete(target);
}

@Test
public void testListProjectsWithParams() throws GitLabApiException {

Expand Down

0 comments on commit 5c891fe

Please sign in to comment.