Skip to content

Commit

Permalink
Skip milestones when repo issues disabled (eclipse-mylyn#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paranoja committed Apr 28, 2024
1 parent 37c8916 commit 84fa85b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.mylyn.commons.net;bundle-version="4.2.0",
com.google.gson,
org.eclipse.mylyn.commons.sdk.util;bundle-version="4.2.0",
org.eclipse.mylyn.tests.util;bundle-version="4.2.0"
org.eclipse.mylyn.tests.util;bundle-version="4.2.0",
org.eclipse.mylyn.commons.repositories.http.core,
org.mockito.mockito-core,
net.bytebuddy.byte-buddy,
net.bytebuddy.byte-buddy-agent,
org.apache.httpcomponents.httpclient,
org.apache.httpcomponents.httpcore
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: org.eclipse.mylyn.gitlab.core.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright © 2024 max
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html.
*
* Contributors:
* See git history
*******************************************************************************/

package org.eclipse.mylyn.internal.gitlab.core;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.http.client.methods.HttpRequestBase;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation;
import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient;
import org.eclipse.mylyn.gitlab.core.GitlabCoreActivator;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

class GitlabRestClientTest {

@SuppressWarnings("nls")
@Test
void ignoreMilestonesForProjectsWithIssuesDisabled() throws Exception {
RepositoryLocation location = new RepositoryLocation("http://localhost");
CommonHttpClient httpClient = Mockito.mock(CommonHttpClient.class);
when(httpClient.getLocation()).thenReturn(location);

GitlabRepositoryConnector connector = new GitlabRepositoryConnector();
String repoUrl = "http://localhost";
TaskRepository repository = new TaskRepository(GitlabCoreActivator.CONNECTOR_KIND, repoUrl);
repository.setProperty(GitlabCoreActivator.USE_PERSONAL_ACCESS_TOKEN, "true");
repository.setProperty(GitlabCoreActivator.PERSONAL_ACCESS_TOKEN, "test-token");
GitlabRestClient client = new GitlabRestClient(location, httpClient, connector, repository);

JsonObject project = new Gson().fromJson("""
{
"id": 20240428,
"issues_enabled": false
}
""", JsonObject.class);
client.getProjectMilestones(project, mock(IOperationMonitor.class, Mockito.RETURNS_SELF));
verify(httpClient, times(0)).execute(any(HttpRequestBase.class), any(IOperationMonitor.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ public class GitlabRestClient {
@SuppressWarnings("restriction")
public GitlabRestClient(RepositoryLocation location, GitlabRepositoryConnector connector,
TaskRepository taskRepository) {
client = new CommonHttpClient(location);
this(location, new CommonHttpClient(location), connector, taskRepository);
}

@SuppressWarnings("restriction")
GitlabRestClient(RepositoryLocation location, CommonHttpClient client, GitlabRepositoryConnector connector,
TaskRepository taskRepository) {
this.client = client;
this.connector = connector;
this.taskRepository = taskRepository;
}
Expand Down Expand Up @@ -1256,7 +1262,7 @@ public GitlabConfiguration getConfiguration(TaskRepository repository, IOperatio
for (JsonElement project : (JsonArray) projects) {
JsonObject projectObject = (JsonObject) project;
JsonArray labels = getProjectLabels(projectObject.get("id").getAsString(), monitor); //$NON-NLS-1$
JsonArray milestones = getProjectMilestones(projectObject.get("id").getAsString(), monitor); //$NON-NLS-1$
JsonArray milestones = getProjectMilestones(projectObject, monitor);
config.addProject(projectObject, labels, milestones);
}
if (GitlabCoreActivator.DEBUG_REST_CLIENT_TRACE) {
Expand All @@ -1278,7 +1284,7 @@ public GitlabConfiguration getConfiguration(TaskRepository repository, IOperatio
}
JsonObject projectObject = projectDetail;
JsonArray labels = getProjectLabels(projectObject.get("id").getAsString(), monitor); //$NON-NLS-1$
JsonArray milestones = getProjectMilestones(projectObject.get("id").getAsString(), monitor); //$NON-NLS-1$
JsonArray milestones = getProjectMilestones(projectObject, monitor);
if (GitlabCoreActivator.DEBUG_REST_CLIENT_TRACE) {
GitlabCoreActivator.DEBUG_TRACE.trace(GitlabCoreActivator.REST_CLIENT_TRACE,
/* repository.getRepositoryUrl() + */ "get Project: (" + (i + 1) + "/" //$NON-NLS-1$ //$NON-NLS-2$
Expand Down Expand Up @@ -1314,7 +1320,7 @@ public GitlabConfiguration getConfiguration(TaskRepository repository, IOperatio
JsonElement project = projectsArray.get(j);
JsonObject projectObject = (JsonObject) project;
JsonArray labels = getProjectLabels(projectObject.get("id").getAsString(), monitor); //$NON-NLS-1$
JsonArray milestones = getProjectMilestones(projectObject.get("id").getAsString(), monitor); //$NON-NLS-1$
JsonArray milestones = getProjectMilestones(projectObject, monitor);
if (GitlabCoreActivator.DEBUG_REST_CLIENT_TRACE) {
GitlabCoreActivator.DEBUG_TRACE.trace(GitlabCoreActivator.REST_CLIENT_TRACE,
/* repository.getRepositoryUrl() + */ "get Group (" + (i + 1) + "/" + groupList.length //$NON-NLS-1$ //$NON-NLS-2$
Expand Down Expand Up @@ -1532,7 +1538,12 @@ protected JsonArray parseFromJson(InputStreamReader in) throws GitlabException {
return jsonArray;
}

public JsonArray getProjectMilestones(String projectid, IOperationMonitor monitor) throws GitlabException {
public JsonArray getProjectMilestones(JsonObject project, IOperationMonitor monitor) throws GitlabException {
String projectid = project.get("id").getAsString();
boolean issuesEnabled = project.get("issues_enabled").getAsBoolean();
if (!issuesEnabled) {
return new JsonArray(0);
}
getAccessTokenIfNotPresent(monitor);
JsonArray jsonArray = new GitlabJSonArrayOperation(client,
"/projects/" + projectid + "/milestones?include_parent_milestones=true") { //$NON-NLS-1$ //$NON-NLS-2$
Expand Down

0 comments on commit 84fa85b

Please sign in to comment.