Skip to content

Commit a28e1d8

Browse files
committed
Added method to upload user avatar (#314).
1 parent 251de6b commit a28e1d8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import org.gitlab4j.api.GitLabApi.ApiVersion;
44
import org.gitlab4j.api.models.CustomAttribute;
55
import org.gitlab4j.api.models.ImpersonationToken;
6+
import org.gitlab4j.api.models.Project;
67
import org.gitlab4j.api.models.ImpersonationToken.Scope;
78
import org.gitlab4j.api.models.SshKey;
89
import org.gitlab4j.api.models.User;
910

1011
import javax.ws.rs.core.Form;
1112
import javax.ws.rs.core.GenericType;
1213
import javax.ws.rs.core.Response;
14+
15+
import java.io.File;
1316
import java.util.Date;
1417
import java.util.List;
1518
import java.util.Objects;
@@ -995,4 +998,19 @@ private GitLabApiForm createGitLabApiForm() {
995998
GitLabApiForm formData = new GitLabApiForm();
996999
return (customAttributesEnabled ? formData.withParam("with_custom_attributes", true) : formData);
9971000
}
1001+
1002+
/**
1003+
* Uploads and sets the user's avatar for the specified user.
1004+
*
1005+
* <pre><code>PUT /users/:id</code></pre>
1006+
*
1007+
* @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
1008+
* @param avatarFile the File instance of the avatar file to upload
1009+
* @return the updated User instance
1010+
* @throws GitLabApiException if any exception occurs
1011+
*/
1012+
public User setUserAvatar(final Object userIdOrUsername, File avatarFile) throws GitLabApiException {
1013+
Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "users", getUserIdOrUsername(userIdOrUsername));
1014+
return (response.readEntity(User.class));
1015+
}
9981016
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.gitlab4j.api.GitLabApi.ApiVersion;
1111
import org.gitlab4j.api.models.Project;
12+
import org.gitlab4j.api.models.User;
1213
import org.junit.Before;
1314
import org.junit.BeforeClass;
1415
import org.junit.FixMethodOrder;
@@ -45,7 +46,8 @@ public class TestAvatarUpload {
4546
TEST_PROXY_USERNAME = TestUtils.getProperty("TEST_PROXY_USERNAME");
4647
TEST_PROXY_PASSWORD = TestUtils.getProperty("TEST_PROXY_PASSWORD");
4748
}
48-
49+
50+
4951
private static final String AVATAR_FILENAME = "avatar.png";
5052

5153
private static GitLabApi gitLabApi;
@@ -116,4 +118,16 @@ public void testSetProjectAvatarWithProxy() throws GitLabApiException {
116118
assertNotNull(updatedProject);
117119
assertTrue(updatedProject.getAvatarUrl().endsWith(AVATAR_FILENAME));
118120
}
121+
122+
@Test
123+
public void testSetUserAvatar() throws GitLabApiException {
124+
125+
User user = gitLabApi.getUserApi().getCurrentUser();
126+
assertNotNull(user);
127+
128+
File avatarFile = new File("src/test/resources/org/gitlab4j/api", AVATAR_FILENAME);
129+
User updatedUser = gitLabApi.getUserApi().setUserAvatar(user, avatarFile);
130+
assertNotNull(updatedUser);
131+
assertTrue(updatedUser.getAvatarUrl().endsWith(AVATAR_FILENAME));
132+
}
119133
}

0 commit comments

Comments
 (0)