mylyn-gitea/io.gitea.mylyn.core/src/io/gitea/mylyn/core/GiteaTaskDataHandler.java

337 lines
13 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.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import org.apache.commons.lang.StringUtils;
import org.eclipse.core.internal.registry.OffsetTable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.tasks.core.ITaskMapping;
import org.eclipse.mylyn.tasks.core.RepositoryResponse;
import org.eclipse.mylyn.tasks.core.RepositoryResponse.ResponseKind;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMetaData;
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskOperation;
import io.gitea.ApiException;
import io.gitea.api.IssueApi;
import io.gitea.model.Comment;
import io.gitea.model.CreateIssueCommentOption;
import io.gitea.model.CreateIssueOption;
import io.gitea.model.EditIssueOption;
import io.gitea.model.GiteaDateTimeUtils;
import io.gitea.model.GiteaPriorityLevel;
import io.gitea.model.GiteaUser;
import io.gitea.model.Issue;
import io.gitea.model.IssueAction;
import io.gitea.model.IssueLabelsOption;
import io.gitea.model.IssueState;
import io.gitea.model.IssueType;
import io.gitea.model.Label;
import io.gitea.model.Milestone;
import io.gitea.model.User;
import io.gitea.mylyn.core.exceptions.GiteaException;
/**
* Handles the Issue<=>Task management. Downloads issues to tasks for a specific
* repository, update issue and creates new issues.
*/
public class GiteaTaskDataHandler extends AbstractTaskDataHandler {
public GiteaTaskDataHandler() {
}
@Override
public TaskAttributeMapper getAttributeMapper(TaskRepository repository) {
try {
return ConnectionManager.get(repository).mapper;
} catch (CoreException e) {
throw new Error(e);
}
}
/**
* Initialize Task from local repository.
*
*/
@Override
public boolean initializeTaskData(TaskRepository repository, TaskData data, ITaskMapping mapping,
IProgressMonitor monitor) throws CoreException {
createDefaultAttributes(data, false);
GiteaConnection connection = ConnectionManager.get(repository);
TaskAttribute root = data.getRoot();
root.getAttribute(GiteaAttribute.PROJECT.getTaskKey()).setValue(connection.repository.getName());// FIXME: not
// the
// project
root.getAttribute(GiteaAttribute.LABELS.getTaskKey()).setValue("");
root.getAttribute(GiteaAttribute.STATUS.getTaskKey()).setValue("open");
root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).setValue("");
return true;
}
/**
* Send Update to remote issues repository (create issue when required).
*
*/
@Override
public RepositoryResponse postTaskData(TaskRepository repository, TaskData data, Set<TaskAttribute> attributes,
IProgressMonitor monitor) throws CoreException {
GiteaAttributeMapper attributeMapper = (GiteaAttributeMapper) data.getAttributeMapper();
TaskAttribute root = data.getRoot();
String taskId = data.getTaskId();
// newLabelsList could be null or may content only one 'null' item when no label
// is defined for the task
List<Label> newLabelsList = attributeMapper
.findLabelsByNames(root.getAttribute(GiteaAttribute.LABELS.getTaskKey()).getValue());
List<Long> newLabelIds = new ArrayList<Long>();
if (newLabelsList != null)
newLabelsList.forEach(label -> {
if (label != null)
newLabelIds.add(label.getId());
});
String title = root.getAttribute(GiteaAttribute.TITLE.getTaskKey()).getValue();
String body = root.getAttribute(GiteaAttribute.BODY.getTaskKey()).getValue();
User assignee = null;
// Check assignee is still part of team member
for (TaskAttribute a : attributes) {
if (a.getId().equals(GiteaAttribute.ASSIGNEE.getTaskKey())) {
assignee = attributeMapper
.findProjectMemberByName(root.getAttribute(GiteaAttribute.ASSIGNEE.getTaskKey()).getValue());
}
}
Milestone milestone = attributeMapper
.findMilestoneByName(root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).getValue());
Long milestoneId = (milestone == null ? 0 : milestone.getId());
GiteaConnection connection = ConnectionManager.get(repository);
// FIXME:TODO: support due date
// OffsetDateTime dueDate =
// OffsetDateTime.GiteaDateTimeUtils.parseDate(root.getAttribute(GiteaAttribute.DUE_DATE.getTaskKey()).getValue());
try {
//monitor.beginTask("Uploading task", IProgressMonitor.UNKNOWN);
Issue issue = null;
if (data.isNew()) {
issue = connection.createIssue(
new CreateIssueOption().title(title).body(body).assignee(GiteaUser.getName(assignee))
.closed(false).milestone(milestoneId).labels(newLabelIds));
// FIXME Additional attributes like due date have to be set after issue creation
// Always the same caveats about TaskId:
// - represented by Issue number and not the issue Id which is internal Gitea database id.
// - Issue number is Long where TaskId is string representing an Integer
taskId = Integer.toString(issue.getNumber().intValue());
return new RepositoryResponse(ResponseKind.TASK_CREATED, taskId);
} else {
Long issueId = GiteaConnector.getTicketId(taskId);
issue = connection.issueGetIssue(issueId);
// A new comment is available
if (root.getAttribute(TaskAttribute.COMMENT_NEW) != null
&& !root.getAttribute(TaskAttribute.COMMENT_NEW).getValue().equals("")) {
connection.issueAddComment(issueId, new CreateIssueCommentOption()
.body(root.getAttribute(TaskAttribute.COMMENT_NEW).getValue()));
}
// Update labels if label list has changed
List<Label>oldLabels = issue.getLabels();
if (!issue.getLabels().equals(newLabelsList)) {
connection.issueReplaceLabels(issueId, new IssueLabelsOption().labels(newLabelIds));
}
// Kept Step by step only for debugging purpose ...
String action = root.getAttribute(TaskAttribute.OPERATION).getValue();
String state = issue.getState();
String newState = IssueAction.getEnum(action).toState(IssueState.getEnum(state)).toString();
// OffsetDateTime dueDate = new OffsetDateTime();
// FIXME:TODO: Support DueDate
boolean unsetDueDate = true;
issue = connection.editIssue(issueId,
new EditIssueOption().assignee(GiteaUser.getName(assignee)).milestone(milestoneId).body(body)
.state(newState).title(title)
// .dueDate(dueDate)
.unsetDueDate(unsetDueDate));
return new RepositoryResponse(ResponseKind.TASK_UPDATED, taskId);
}
} catch (Exception e) {
throw new GiteaException("Unknown connection error!");
} finally {
//monitor.done();
}
}
/**
* Get task data including comments from remote issue id.
*
* @param repository
* @param ticketId
* @return
* @throws CoreException
*/
public TaskData downloadTaskData(TaskRepository repository, Long ticketId) throws CoreException {
try {
GiteaConnection connection = ConnectionManager.get(repository);
Issue issue = connection.issueGetIssue(ticketId);
List<Comment> notes = connection.issueGetComments(issue);
return createTaskDataFromGiteaIssue(issue, repository, notes);
} catch (ApiException e) {
throw new GiteaException("Unknown connection error!");
}
}
public TaskData createTaskDataFromGiteaIssue(Issue issue, TaskRepository repository, List<Comment> notes)
throws CoreException {
GiteaConnection connection = ConnectionManager.get(repository);
TaskData data = new TaskData(connection.mapper, GiteaPluginCore.CONNECTOR_KIND, repository.getUrl(),
issue.getNumber().toString());
// Labels
List<String> labelsNames = new ArrayList<String>();
List<Label> issueLabels = issue.getLabels();
issueLabels.forEach((label) -> labelsNames.add(label.getName()));
String labels = StringUtils.join(labelsNames, ", ");
createDefaultAttributes(data, true);
TaskAttribute root = data.getRoot();
root.getAttribute(GiteaAttribute.AUTHOR.getTaskKey()).setValue(GiteaUser.getName(issue.getUser()));
root.getAttribute(GiteaAttribute.CREATED.getTaskKey()).setValue(issue.getCreatedAt().toString());
root.getAttribute(GiteaAttribute.BODY.getTaskKey()).setValue(issue.getBody() == null ? "" : issue.getBody());
root.getAttribute(GiteaAttribute.LABELS.getTaskKey()).setValue(labels);
root.getAttribute(GiteaAttribute.PROJECT.getTaskKey()).setValue(connection.repository.getName()); // FIXME:
root.getAttribute(GiteaAttribute.STATUS.getTaskKey()).setValue(issue.getState());
root.getAttribute(GiteaAttribute.TITLE.getTaskKey()).setValue(issue.getTitle());
root.getAttribute(GiteaAttribute.IID.getTaskKey()).setValue("" + issue.getNumber().toString());
root.getAttribute(GiteaAttribute.PRIORITY.getTaskKey())
.setValue(GiteaPriorityLevel.getPriority(labels).toString());
root.getAttribute(GiteaAttribute.TYPE.getTaskKey()).setValue(getType(labels));
if (issue.getMilestone() != null) {
root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).setValue(issue.getMilestone().getTitle());
}
if (issue.getUpdatedAt() != null) {
root.getAttribute(GiteaAttribute.UPDATED.getTaskKey()).setValue(issue.getUpdatedAt().toString());
}
if (IssueState.isClosed(issue.getState())) {
root.getAttribute(GiteaAttribute.COMPLETED.getTaskKey()).setValue(issue.getUpdatedAt().toString());
}
// Assignee name is either FulleName either Login either empty
List<String> assigneesNames = new ArrayList<String>();
List<User> assignees = issue.getAssignees();
if (assignees != null) {
assignees.forEach((assignee) -> assigneesNames.add(GiteaUser.getName(assignee)));
root.getAttribute(GiteaAttribute.ASSIGNEE.getTaskKey()).setValue(StringUtils.join(assigneesNames, ", "));
}
Collections.sort(notes, new Comparator<Comment>() {
@Override
public int compare(Comment o1, Comment o2) {
return o1.getCreatedAt().compareTo(o2.getCreatedAt());
}
});
int i = 0;
for (Comment note : notes) {
TaskCommentMapper cmapper = new TaskCommentMapper();
cmapper.setAuthor(repository.createPerson(GiteaUser.getName(note.getUser())));
cmapper.setCreationDate(GiteaDateTimeUtils.toDate(note.getCreatedAt()));// FIXME:
cmapper.setText(note.getBody());
cmapper.setNumber(++i);
TaskAttribute attribute = data.getRoot().createAttribute(TaskAttribute.PREFIX_COMMENT + (i + 1));
cmapper.applyTo(attribute);
}
GiteaAction[] actions = GiteaAction.getActions(issue);
for (GiteaAction action : actions) {
TaskAttribute attribute = data.getRoot().createAttribute(TaskAttribute.PREFIX_OPERATION + action.label);
TaskOperation.applyTo(attribute, action.label, action.label);
}
return data;
}
private void createDefaultAttributes(TaskData data, boolean existingTask) {
createAttribute(data, GiteaAttribute.BODY);
createAttribute(data, GiteaAttribute.TITLE);
createAttribute(data, GiteaAttribute.LABELS);
createAttribute(data, GiteaAttribute.STATUS);
createAttribute(data, GiteaAttribute.PROJECT);
createAttribute(data, GiteaAttribute.CREATED);
createAttribute(data, GiteaAttribute.COMPLETED);
createAttribute(data, GiteaAttribute.UPDATED);
createAttribute(data, GiteaAttribute.ASSIGNEE);
createAttribute(data, GiteaAttribute.MILESTONE);
createAttribute(data, GiteaAttribute.IID);
createAttribute(data, GiteaAttribute.PRIORITY);
createAttribute(data, GiteaAttribute.TYPE);
data.getRoot().getAttribute(GiteaAttribute.CREATED.getTaskKey()).setValue("" + (new Date().getTime()));
if (existingTask) {
data.getRoot().createAttribute(TaskAttribute.COMMENT_NEW).getMetaData()
.setType(TaskAttribute.TYPE_LONG_RICH_TEXT).setReadOnly(false);
createAttribute(data, GiteaAttribute.AUTHOR);
}
TaskAttribute operation = data.getRoot().createAttribute(TaskAttribute.OPERATION);
operation.getMetaData().setType(TaskAttribute.TYPE_OPERATION);
}
private void createAttribute(TaskData data, GiteaAttribute attribute) {
TaskAttribute attr = data.getRoot().createAttribute(attribute.getTaskKey());
TaskAttributeMetaData metaData = attr.getMetaData();
metaData.setType(attribute.getType());
metaData.setKind(attribute.getKind());
metaData.setLabel(attribute.toString());
metaData.setReadOnly(attribute.isReadOnly());
}
/**
* Returns the type string for Mylyn. Uses a regular expression to check for
* types in the given label.
*
* @param labels
* @return
*/
private String getType(String labels) {
Matcher m = IssueType.PATTERN.matcher(labels);
if (m.find()) {
return m.group(1);
}
return "";
}
}