diff --git a/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core.test/META-INF/MANIFEST.MF b/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core.test/META-INF/MANIFEST.MF index 7dcca39f68..8f12086dd1 100644 --- a/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core.test/META-INF/MANIFEST.MF +++ b/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core.test/META-INF/MANIFEST.MF @@ -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 diff --git a/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core.test/src/org/eclipse/mylyn/internal/gitlab/core/GitlabRestClientTest.java b/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core.test/src/org/eclipse/mylyn/internal/gitlab/core/GitlabRestClientTest.java new file mode 100644 index 0000000000..cf2c35834c --- /dev/null +++ b/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core.test/src/org/eclipse/mylyn/internal/gitlab/core/GitlabRestClientTest.java @@ -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)); + } +} diff --git a/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core/src/org/eclipse/mylyn/internal/gitlab/core/GitlabRestClient.java b/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core/src/org/eclipse/mylyn/internal/gitlab/core/GitlabRestClient.java index 6f46a41dc8..bc8bb3c4b0 100644 --- a/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core/src/org/eclipse/mylyn/internal/gitlab/core/GitlabRestClient.java +++ b/mylyn.tasks/connectors/gitlab/org.eclipse.mylyn.gitlab.core/src/org/eclipse/mylyn/internal/gitlab/core/GitlabRestClient.java @@ -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; } @@ -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) { @@ -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$ @@ -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$ @@ -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$