Skip to content

Commit

Permalink
Topic improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jmini committed Aug 16, 2023
1 parent f63fef9 commit 1cb3685
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 20 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/gitlab4j/api/ProjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,10 @@ public Project createProject(Project project, String importUrl) throws GitLabApi
if (project.getTagList() != null && !project.getTagList().isEmpty()) {
throw new IllegalArgumentException("GitLab API v3 does not support tag lists when creating projects");
}

if (project.getTopics() != null && !project.getTopics().isEmpty()) {
throw new IllegalArgumentException("GitLab API v3 does not support topics when creating projects");
}
} else {
Visibility visibility = (project.getVisibility() != null ? project.getVisibility() :
project.getPublic() == Boolean.TRUE ? Visibility.PUBLIC : null);
Expand All @@ -1051,6 +1055,10 @@ public Project createProject(Project project, String importUrl) throws GitLabApi
if (project.getTagList() != null && !project.getTagList().isEmpty()) {
formData.withParam("tag_list", String.join(",", project.getTagList()));
}

if (project.getTopics() != null && !project.getTopics().isEmpty()) {
formData.withParam("topics", String.join(",", project.getTopics()));
}
}

Response response = post(Response.Status.CREATED, formData, "projects");
Expand Down Expand Up @@ -1314,6 +1322,10 @@ public Project updateProject(Project project) throws GitLabApiException {
if (project.getTagList() != null && !project.getTagList().isEmpty()) {
throw new IllegalArgumentException("GitLab API v3 does not support tag lists when updating projects");
}

if (project.getTopics() != null && !project.getTopics().isEmpty()) {
throw new IllegalArgumentException("GitLab API v3 does not support topics when updating projects");
}
} else {
Visibility visibility = (project.getVisibility() != null ? project.getVisibility() :
project.getPublic() == Boolean.TRUE ? Visibility.PUBLIC : null);
Expand All @@ -1322,6 +1334,10 @@ public Project updateProject(Project project) throws GitLabApiException {
if (project.getTagList() != null && !project.getTagList().isEmpty()) {
formData.withParam("tag_list", String.join(",", project.getTagList()));
}

if (project.getTopics() != null && !project.getTopics().isEmpty()) {
formData.withParam("topics", String.join(",", project.getTopics()));
}
}

Response response = putWithFormData(Response.Status.OK, formData, "projects", projectIdentifier);
Expand Down
37 changes: 32 additions & 5 deletions src/main/java/org/gitlab4j/api/TopicsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public TopicsApi(GitLabApi gitLabApi) {
* @throws GitLabApiException if any exception occurs
*/
public List<Topic> getTopics() throws GitLabApiException {

return (getTopics(getDefaultPerPage()).all());
}

Expand Down Expand Up @@ -132,6 +131,7 @@ public Topic updateTopic(Integer id, TopicParams params) throws GitLabApiExcepti
params.getForm(false), "topics", id);
return (response.readEntity(Topic.class));
}

/**
* Uploads and sets the topic's avatar for the specified topic.
*
Expand All @@ -147,19 +147,46 @@ public Topic updateTopicAvatar(final Integer id, File avatarFile) throws GitLabA
return (response.readEntity(Topic.class));
}

/**
* Delete the topic's avatar for the specified topic.
*
* <pre><code>PUT /topics/:id</code></pre>
*
* @param id the topic in the form of an Integer
* @return the updated Topic instance
* @throws GitLabApiException if any exception occurs
*/
public Topic deleteTopicAvatar(final Integer id) throws GitLabApiException {
Response response = putUpload(Response.Status.OK, "avatar", null, "topics", id);
return (response.readEntity(Topic.class));
}

/**
* You must be an administrator to delete a project topic. When you delete a project topic, you also delete the topic assignment for projects.
*
* <pre><code>DELETE /topics/:id</code></pre>
*
* @param id the topic in the form of an Integer
* @throws GitLabApiException if any exception occurs
*/
public void deleteTopic(Integer id) throws GitLabApiException {
if(isApiVersion(GitLabApi.ApiVersion.V3)){
throw new GitLabApiException("Topics need api v4+");
}
delete(Response.Status.NO_CONTENT,null, "topics", id);
}



/**
* You must be an administrator to merge a source topic into a target topic. When you merge topics, you delete the source topic and move all assigned projects to the target topic.
*
* <pre><code>DELETE /topics/:id</code></pre>
*
* @param sourceTopicId ID of source project topic
* @param targetTopicId ID of target project topic
* @throws GitLabApiException if any exception occurs
*/
public Topic mergeTopics(Integer sourceTopicId, Integer targetTopicId) throws GitLabApiException {
Response response = post(Response.Status.OK,new GitLabApiForm().withParam("source_topic_id",sourceTopicId).withParam("target_topic_id",targetTopicId),"topics/merge");
return (response.readEntity(Topic.class));
}


}
20 changes: 10 additions & 10 deletions src/main/java/org/gitlab4j/api/models/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class Topic {

private String description;

private int total_projects_count;
private int totalProjectsCount;

private String avatar_url;
private String avatarUrl;

public Integer getId() {
return id;
Expand Down Expand Up @@ -48,20 +48,20 @@ public void setDescription(String description) {
this.description = description;
}

public int getTotal_projects_count() {
return total_projects_count;
public int getTotalProjectsCount() {
return totalProjectsCount;
}

public void setTotal_projects_count(int total_projects_count) {
this.total_projects_count = total_projects_count;
public void setTotalProjectsCount(int totalProjectsCount) {
this.totalProjectsCount = totalProjectsCount;
}

public String getAvatar_url() {
return avatar_url;
public String getAvatarUrl() {
return avatarUrl;
}

public void setAvatar_url(String avatar_url) {
this.avatar_url = avatar_url;
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}

@Override
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/gitlab4j/api/models/TopicParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
* and {@link TopicsApi#updateTopic(Integer, TopicParams)} methods to set
* the parameters for the call to the GitLab API.
*
* Avatar Upload has its own Upload in {@link TopicsApi#setTopicAvatar(Integer,File)}
* Avatar Upload has its own Upload in {@link TopicsApi#updateTopicAvatar(Integer,File)}
*/
public class TopicParams {

private String name;
private String title;
private String description;



public TopicParams withName(String name) {
this.name = name;
return (this);
Expand All @@ -35,8 +33,6 @@ public TopicParams withDescription(String description) {
return (this);
}



/**
* Get the form params for a group create oir update call.
*
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
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;
Expand Down Expand Up @@ -735,6 +736,12 @@ public void testTodos() throws Exception {
assertTrue(compareJson(todos, "todos.json"));
}

@Test
public void testTopic() throws Exception {
Topic topic = unmarshalResource(Topic.class, "topic.json");
assertTrue(compareJson(topic, "topic.json"));
}

@Test
public void testTree() throws Exception {
List<TreeItem> tree = unmarshalResourceList(TreeItem.class, "tree.json");
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/org/gitlab4j/api/TestTopicsApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.gitlab4j.api;

import static org.gitlab4j.api.JsonUtils.compareJson;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;

import java.io.IOException;

import javax.ws.rs.core.MultivaluedMap;

import org.gitlab4j.api.models.Topic;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;

public class TestTopicsApi implements Constants {

@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private MockResponse response;

@BeforeEach
public void setUp() throws Exception {
openMocks(this);
}

@Test
public void testGetTopic() throws Exception {
initGetTopic();
Topic result = new TopicsApi(gitLabApi).getTopic(1);
assertNotNull(result);
assertTrue(compareJson(result, "topic.json"));
}

private void initGetTopic() throws Exception, IOException {
response = new MockResponse(Topic.class, "topic.json", null);
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
when(gitLabApiClient.get(attributeCaptor.capture(), Mockito.<Object>any())).thenReturn(response);
}
}
8 changes: 8 additions & 0 deletions src/test/resources/org/gitlab4j/api/topic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 1,
"name": "gitlab",
"title": "GitLab",
"description": "GitLab is an open source end-to-end software development platform with built-in version control, issue tracking, code review, CI/CD, and more.",
"total_projects_count": 1000,
"avatar_url": "http://www.gravatar.com/avatar/a0d477b3ea21970ce6ffcbb817b0b435?s=80&d=identicon"
}

0 comments on commit 1cb3685

Please sign in to comment.