Skip to content

Commit 13533a4

Browse files
committed
Merge remote-tracking branch 'origin/main' into 6.x
# Conflicts: # src/main/java/org/gitlab4j/api/GroupApi.java # src/main/java/org/gitlab4j/api/ProjectApi.java
2 parents 8ee5bec + 5c891fe commit 13533a4

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import java.io.File;
4+
import java.io.InputStream;
45
import java.util.Arrays;
56
import java.util.Date;
67
import java.util.List;
@@ -10,6 +11,7 @@
1011

1112
import jakarta.ws.rs.core.Form;
1213
import jakarta.ws.rs.core.GenericType;
14+
import jakarta.ws.rs.core.MediaType;
1315
import jakarta.ws.rs.core.MultivaluedMap;
1416
import jakarta.ws.rs.core.Response;
1517

@@ -1879,6 +1881,24 @@ public Group setGroupAvatar(Object groupIdOrPath, File avatarFile) throws GitLab
18791881
return (response.readEntity(Group.class));
18801882
}
18811883

1884+
/**
1885+
* Gets the group avatar.
1886+
* Only working with GitLab 14.0 and above.
1887+
*
1888+
* <pre><code>GitLab Endpoint: GET /groups/:id/avatar</code></pre>
1889+
*
1890+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
1891+
* @return an InputStream to read the raw file from
1892+
* @throws GitLabApiException if any exception occurs
1893+
*/
1894+
public InputStream getAvatar(Object groupIdOrPath) throws GitLabApiException {
1895+
1896+
Response response = getWithAccepts(Response.Status.OK, null, MediaType.MEDIA_TYPE_WILDCARD,
1897+
"groups", getGroupIdOrPath(groupIdOrPath), "avatar");
1898+
return (response.readEntity(InputStream.class));
1899+
1900+
}
1901+
18821902
/**
18831903
* Share group with another group. Returns 200 and the group details on success.
18841904
*

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

+19
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.stream.Stream;
3636
import jakarta.ws.rs.core.Form;
3737
import jakarta.ws.rs.core.GenericType;
38+
import jakarta.ws.rs.core.MediaType;
3839
import jakarta.ws.rs.core.MultivaluedMap;
3940
import jakarta.ws.rs.core.Response;
4041
import org.gitlab4j.api.GitLabApi.ApiVersion;
@@ -1068,6 +1069,24 @@ public Project createProject(Project project, String importUrl) throws GitLabApi
10681069
return (response.readEntity(Project.class));
10691070
}
10701071

1072+
/**
1073+
* Gets the project avatar.
1074+
* Only working with GitLab 16.9 and above.
1075+
*
1076+
* <pre><code>GitLab Endpoint: GET /projects/:id/avatar</code></pre>
1077+
*
1078+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
1079+
* @return an InputStream to read the raw file from
1080+
* @throws GitLabApiException if any exception occurs
1081+
*/
1082+
public InputStream getAvatar(Object projectIdOrPath) throws GitLabApiException {
1083+
1084+
Response response = getWithAccepts(Response.Status.OK, null, MediaType.MEDIA_TYPE_WILDCARD,
1085+
"projects", getProjectIdOrPath(projectIdOrPath), "avatar");
1086+
return (response.readEntity(InputStream.class));
1087+
1088+
}
1089+
10711090
/**
10721091
* Creates a Project
10731092
*

src/test/java/org/gitlab4j/api/TestAvatarUpload.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.File;
88
import java.util.Map;
99

10+
import org.gitlab4j.api.models.Group;
1011
import org.gitlab4j.api.models.Project;
1112
import org.gitlab4j.api.models.User;
1213
import org.junit.jupiter.api.BeforeAll;
@@ -45,16 +46,18 @@ public class TestAvatarUpload extends AbstractIntegrationTest {
4546

4647
private static GitLabApi gitLabApi;
4748
private static Project testProject;
49+
private static Group testGroup;
4850

4951
public TestAvatarUpload() {
5052
super();
5153
}
5254

5355
@BeforeAll
5456
public static void setup() {
55-
// Must setup the connection to the GitLab test server and get the test Project instance
57+
// Must setup the connection to the GitLab test server and get the test Project and Group instances
5658
gitLabApi = baseTestSetup();
5759
testProject = getTestProject();
60+
testGroup = getTestGroup();
5861
}
5962

6063
@Test
@@ -100,4 +103,15 @@ public void testSetUserAvatar() throws GitLabApiException {
100103
assertNotNull(updatedUser);
101104
assertTrue(updatedUser.getAvatarUrl().endsWith(AVATAR_FILENAME));
102105
}
106+
107+
@Test
108+
public void testSetGroupAvatar() throws GitLabApiException {
109+
110+
assumeTrue(testGroup != null);
111+
112+
File avatarFile = new File("src/test/resources/org/gitlab4j/api", AVATAR_FILENAME);
113+
Group updatedGroup = gitLabApi.getGroupApi().setGroupAvatar(testGroup.getId(), avatarFile);
114+
assertNotNull(updatedGroup);
115+
assertTrue(updatedGroup.getAvatarUrl().endsWith(AVATAR_FILENAME));
116+
}
103117
}

src/test/java/org/gitlab4j/api/TestGroupApi.java

+28
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
import static org.junit.jupiter.api.Assertions.assertTrue;
77
import static org.junit.jupiter.api.Assumptions.assumeTrue;
88

9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.StandardCopyOption;
915
import java.util.List;
1016
import java.util.Optional;
1117
import java.util.stream.Stream;
@@ -21,6 +27,7 @@
2127
import org.junit.jupiter.api.AfterAll;
2228
import org.junit.jupiter.api.BeforeAll;
2329
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Disabled;
2431
import org.junit.jupiter.api.Tag;
2532
import org.junit.jupiter.api.Test;
2633
import org.junit.jupiter.api.extension.ExtendWith;
@@ -46,6 +53,8 @@ public class TestGroupApi extends AbstractIntegrationTest {
4653
private static final String TEST_GROUP_MEMBER_USERNAME = HelperUtils.getProperty(GROUP_MEMBER_USERNAME_KEY);
4754
private static final String TEST_REQUEST_ACCESS_USERNAME = HelperUtils.getProperty(TEST_REQUEST_ACCESS_USERNAME_KEY);
4855

56+
private static final String AVATAR_FILENAME = "avatar.png";
57+
4958
private static GitLabApi gitLabApi;
5059
private static Group testGroup;
5160
private static User testUser;
@@ -196,6 +205,25 @@ public void getOptionalGroup() {
196205
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus());
197206
}
198207

208+
@Test
209+
@Disabled("Required Gitlab version not less then 14.0")
210+
public void testGetAvatar() throws GitLabApiException, IOException {
211+
212+
assumeTrue(testGroup != null);
213+
214+
File avatarFile = new File("src/test/resources/org/gitlab4j/api", AVATAR_FILENAME);
215+
gitLabApi.getGroupApi().setGroupAvatar(testGroup.getId(), avatarFile);
216+
217+
// Get the avatar of the test Group
218+
InputStream in = gitLabApi.getGroupApi().getAvatar(testGroup);
219+
220+
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-avatar", "png");
221+
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
222+
223+
assertTrue(target.toFile().length() > 0);
224+
Files.delete(target);
225+
}
226+
199227

200228
@Test
201229
public void testRequestAccess() throws GitLabApiException {

src/test/java/org/gitlab4j/api/TestProjectApi.java

+28
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
import static org.junit.jupiter.api.Assertions.assertTrue;
3131
import static org.junit.jupiter.api.Assumptions.assumeTrue;
3232

33+
import java.io.File;
34+
import java.io.IOException;
35+
import java.io.InputStream;
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
38+
import java.nio.file.StandardCopyOption;
3339
import java.time.Instant;
3440
import java.util.Arrays;
3541
import java.util.Date;
@@ -53,6 +59,7 @@
5359
import org.junit.jupiter.api.AfterAll;
5460
import org.junit.jupiter.api.BeforeAll;
5561
import org.junit.jupiter.api.BeforeEach;
62+
import org.junit.jupiter.api.Disabled;
5663
import org.junit.jupiter.api.MethodOrderer;
5764
import org.junit.jupiter.api.Tag;
5865
import org.junit.jupiter.api.Test;
@@ -91,6 +98,8 @@ public class TestProjectApi extends AbstractIntegrationTest {
9198
private static final String TEST_XFER_PROJECT_NAME = "test-gitlab4j-xfer-project";
9299
private static final String TEST_VARIABLE_KEY_PREFIX = "TEST_VARIABLE_KEY_";
93100

101+
private static final String AVATAR_FILENAME = "avatar.png";
102+
94103
private static GitLabApi gitLabApi;
95104
private static Project testProject;
96105
private static User currentUser;
@@ -308,6 +317,25 @@ else if (TEST_PROJECT_NAME_2.equals(project.getName()))
308317
assertEquals(TEST_PROJECT_NAME_1, projects.get(1).getName());
309318
}
310319

320+
@Test
321+
@Disabled("Required Gitlab version not less then 16.9")
322+
public void testGetAvatar() throws GitLabApiException, IOException {
323+
324+
assumeTrue(testProject != null);
325+
326+
File avatarFile = new File("src/test/resources/org/gitlab4j/api", AVATAR_FILENAME);
327+
gitLabApi.getProjectApi().setProjectAvatar(testProject.getId(), avatarFile);
328+
329+
// Get the avatar of the test project
330+
InputStream in = gitLabApi.getProjectApi().getAvatar(testProject);
331+
332+
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-avatar", "png");
333+
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
334+
335+
assertTrue(target.toFile().length() > 0);
336+
Files.delete(target);
337+
}
338+
311339
@Test
312340
public void testListProjectsWithParams() throws GitLabApiException {
313341

0 commit comments

Comments
 (0)