161 lines
5.2 KiB
Java
161 lines
5.2 KiB
Java
// Copyright (c) 2021, Fr.Terrot. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package io.gitea.mylyn.core;
|
|
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import org.eclipse.core.runtime.CoreException;
|
|
import org.eclipse.core.runtime.IProgressMonitor;
|
|
import org.eclipse.core.runtime.IStatus;
|
|
import org.eclipse.core.runtime.Status;
|
|
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
|
|
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
|
|
import org.eclipse.mylyn.tasks.core.ITask;
|
|
import org.eclipse.mylyn.tasks.core.TaskRepository;
|
|
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
|
|
import org.eclipse.mylyn.tasks.core.data.TaskData;
|
|
import org.eclipse.mylyn.tasks.core.data.TaskDataCollector;
|
|
import org.eclipse.mylyn.tasks.core.data.TaskMapper;
|
|
import org.eclipse.mylyn.tasks.core.sync.ISynchronizationSession;
|
|
import io.gitea.*;
|
|
import io.gitea.model.Issue;
|
|
import io.gitea.mylyn.core.exceptions.GiteaException;
|
|
|
|
/**
|
|
* Establishes a connection to the Gitea Instance and handles all requests like
|
|
* search requests etc.
|
|
*/
|
|
public class GiteaConnector extends AbstractRepositoryConnector {
|
|
|
|
private GiteaTaskDataHandler handler = new GiteaTaskDataHandler();
|
|
|
|
@Override
|
|
public boolean canCreateNewTask(TaskRepository repository) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean canCreateTaskFromKey(TaskRepository repository) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public String getConnectorKind() {
|
|
return GiteaPluginCore.CONNECTOR_KIND;
|
|
}
|
|
|
|
@Override
|
|
public String getLabel() {
|
|
return "Gitea Issues";
|
|
}
|
|
|
|
@Override
|
|
public String getRepositoryUrlFromTaskUrl(String arg0) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public TaskData getTaskData(TaskRepository repository, String id, IProgressMonitor monitor) throws CoreException {
|
|
|
|
try {
|
|
monitor.beginTask("Task Download", IProgressMonitor.UNKNOWN);
|
|
return handler.downloadTaskData(repository, GiteaConnector.getTicketId(id));
|
|
} finally {
|
|
monitor.done();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String getTaskIdFromTaskUrl(String url) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public String getTaskUrl(String arg0, String arg1) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasTaskChanged(TaskRepository repository, ITask task, TaskData data) {
|
|
TaskMapper mapper = new GiteaTaskMapper(data);
|
|
if (data.isPartial()) {
|
|
return mapper.hasChanges(task);
|
|
} else {
|
|
Date repositoryDate = mapper.getModificationDate();
|
|
Date localDate = task.getModificationDate();
|
|
if (repositoryDate != null && repositoryDate.equals(localDate)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public IStatus performQuery(TaskRepository repository, IRepositoryQuery query, TaskDataCollector collector,
|
|
ISynchronizationSession session, IProgressMonitor monitor) {
|
|
|
|
try {
|
|
monitor.beginTask("Tasks querying", IProgressMonitor.UNKNOWN);
|
|
GiteaConnection connection = ConnectionManager.get(repository);
|
|
GiteaIssueSearch search = new GiteaIssueSearch(query);
|
|
List<Issue> issues = connection.repoGetAllIssues();
|
|
|
|
for (Issue i : issues) {
|
|
if (search.doesMatch(i)) {
|
|
collector.accept(
|
|
handler.createTaskDataFromGiteaIssue(i, repository, connection.issueGetComments(i)));
|
|
}
|
|
}
|
|
return Status.OK_STATUS;
|
|
} catch (ApiException e) {
|
|
return new Status(Status.ERROR, GiteaPluginCore.ID_PLUGIN, "Unable to execute Query: " + e.getMessage());
|
|
} catch (CoreException e) {
|
|
return new Status(Status.ERROR, GiteaPluginCore.ID_PLUGIN, "Unable to execute Query: " + e.getMessage());
|
|
} finally {
|
|
monitor.done();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void updateRepositoryConfiguration(TaskRepository repository, IProgressMonitor monitor)
|
|
throws CoreException {
|
|
try {
|
|
monitor.beginTask("Updating repository configuration", IProgressMonitor.UNKNOWN);
|
|
ConnectionManager.get(repository, true);
|
|
} finally {
|
|
monitor.done();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void updateTaskFromTaskData(TaskRepository repository, ITask task, TaskData data) {
|
|
GiteaTaskMapper mapper = new GiteaTaskMapper(data);
|
|
mapper.applyTo(task);
|
|
}
|
|
|
|
public static void validate(TaskRepository taskRepo) throws CoreException {
|
|
try {
|
|
ConnectionManager.validate(taskRepo);
|
|
} catch (GiteaException e) {
|
|
throw e;
|
|
} catch (Exception e) {
|
|
throw new GiteaException("Connection not successful or repository not found: " + e.getMessage());
|
|
} catch (Error e) {
|
|
throw new GiteaException("Connection not successful or repository not found: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public AbstractTaskDataHandler getTaskDataHandler() {
|
|
return handler;
|
|
}
|
|
|
|
public static Long getTicketId(String id) {
|
|
return (long) Integer.parseInt(id);
|
|
}
|
|
|
|
}
|