Implement TasK/Issue creation and modification (#16)
This commit is contained in:
parent
b35520e4d8
commit
e7b73155c6
@ -7,12 +7,42 @@ package io.gitea.model;
|
|||||||
public enum IssueAction {
|
public enum IssueAction {
|
||||||
CLOSE("close"),
|
CLOSE("close"),
|
||||||
LEAVE("leave"),
|
LEAVE("leave"),
|
||||||
REOPEN("open");
|
REOPEN("reopen");
|
||||||
|
|
||||||
IssueAction(String label) {
|
IssueAction(String label) {
|
||||||
this.label = label;
|
this.label = label;
|
||||||
}
|
}
|
||||||
public final String label;
|
public final String label;
|
||||||
|
|
||||||
|
public static IssueAction getEnum(String other) {
|
||||||
|
if ((other == null)||(other.isBlank()) || (other.isEmpty()))
|
||||||
|
return LEAVE;
|
||||||
|
for (IssueAction a : IssueAction.values())
|
||||||
|
if (a.equals(other)) return a;
|
||||||
|
throw new IllegalArgumentException(other + " is not a IssueAction value");
|
||||||
|
}
|
||||||
|
/**Compute targeted state when apply action as transition to a state.
|
||||||
|
*
|
||||||
|
* [from State: OPENED] --> Action: CLOSE --> [to State:CLOSED]
|
||||||
|
* [from State: CLOSED] --> Action: REOPEN --> [to State: OPENDED]
|
||||||
|
* All other cases have no impact on State.
|
||||||
|
*
|
||||||
|
* @param fromState
|
||||||
|
* @return Targeted state as IssueState.
|
||||||
|
*/
|
||||||
|
public IssueState toState(IssueState fromState) {
|
||||||
|
if ((this == CLOSE) && (fromState == IssueState.STATE_OPENED)) return IssueState.STATE_CLOSED;
|
||||||
|
if ((this == REOPEN) && (fromState == IssueState.STATE_CLOSED)) return IssueState.STATE_OPENED;
|
||||||
|
return fromState;
|
||||||
|
}
|
||||||
|
static public IssueState toState(IssueState from, IssueAction onAction) {
|
||||||
|
return onAction.toState(from);
|
||||||
|
}
|
||||||
|
public boolean equals(IssueAction arg0) {
|
||||||
|
return this.label.equals(arg0.toString());
|
||||||
|
}
|
||||||
|
public boolean equals(String arg0) {
|
||||||
|
return this.label.equals(arg0);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,12 @@ public enum IssueState {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IssueState getEnum(String value) {
|
||||||
|
for(IssueState s: IssueState.values()) {
|
||||||
|
if (s.equals(value)) return s;
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException(value + " is not a IssueState value");
|
||||||
|
}
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,8 @@ public enum GiteaAttribute {
|
|||||||
|
|
||||||
COMPLETED("Completed", TaskAttribute.DATE_COMPLETION, TaskAttribute.TYPE_DATETIME, GiteaFlag.READ_ONLY),
|
COMPLETED("Completed", TaskAttribute.DATE_COMPLETION, TaskAttribute.TYPE_DATETIME, GiteaFlag.READ_ONLY),
|
||||||
|
|
||||||
|
DUE_DATE("Due Date", TaskAttribute.DATE_DUE, TaskAttribute.TYPE_DATE, GiteaFlag.ATTRIBUTE),
|
||||||
|
|
||||||
AUTHOR("Author", TaskAttribute.USER_REPORTER, TaskAttribute.TYPE_PERSON, GiteaFlag.READ_ONLY, GiteaFlag.ATTRIBUTE),
|
AUTHOR("Author", TaskAttribute.USER_REPORTER, TaskAttribute.TYPE_PERSON, GiteaFlag.READ_ONLY, GiteaFlag.ATTRIBUTE),
|
||||||
|
|
||||||
PROJECT("Project", TaskAttribute.PRODUCT, TaskAttribute.TYPE_SHORT_TEXT, GiteaFlag.READ_ONLY, GiteaFlag.ATTRIBUTE),
|
PROJECT("Project", TaskAttribute.PRODUCT, TaskAttribute.TYPE_SHORT_TEXT, GiteaFlag.READ_ONLY, GiteaFlag.ATTRIBUTE),
|
||||||
|
@ -17,6 +17,7 @@ import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
|
|||||||
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
|
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
|
||||||
|
|
||||||
import io.gitea.model.GiteaDateTimeUtils;
|
import io.gitea.model.GiteaDateTimeUtils;
|
||||||
|
import io.gitea.model.Label;
|
||||||
import io.gitea.model.Milestone;
|
import io.gitea.model.Milestone;
|
||||||
import io.gitea.model.User;
|
import io.gitea.model.User;
|
||||||
|
|
||||||
@ -89,6 +90,21 @@ public class GiteaAttributeMapper extends TaskAttributeMapper {
|
|||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Label> findLabelsByNames(String names) {
|
||||||
|
List<Label> found = new ArrayList<Label>();
|
||||||
|
try {
|
||||||
|
List<Label> labels = getConnection().getLabels();
|
||||||
|
HashMap<String, Label> map = new HashMap<String,Label>();
|
||||||
|
for (Label l : labels) {
|
||||||
|
map.put(l.getName(),l);
|
||||||
|
}
|
||||||
|
for (String n: names.split(",")) {
|
||||||
|
found.add(map.get(n));
|
||||||
|
}
|
||||||
|
} catch (CoreException e) {
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
private HashMap<String, String> getAsMap(List<String> list) {
|
private HashMap<String, String> getAsMap(List<String> list) {
|
||||||
HashMap<String, String> map = new HashMap<String, String>();
|
HashMap<String, String> map = new HashMap<String, String>();
|
||||||
map.put("", "");
|
map.put("", "");
|
||||||
|
@ -15,7 +15,13 @@ import io.gitea.api.IssueApi;
|
|||||||
import io.gitea.api.RepositoryApi;
|
import io.gitea.api.RepositoryApi;
|
||||||
import io.gitea.api.UserApi;
|
import io.gitea.api.UserApi;
|
||||||
import io.gitea.model.Comment;
|
import io.gitea.model.Comment;
|
||||||
|
import io.gitea.model.CreateIssueCommentOption;
|
||||||
|
import io.gitea.model.CreateIssueOption;
|
||||||
|
import io.gitea.model.EditIssueCommentOption;
|
||||||
|
import io.gitea.model.EditIssueOption;
|
||||||
|
import io.gitea.model.EditLabelOption;
|
||||||
import io.gitea.model.Issue;
|
import io.gitea.model.Issue;
|
||||||
|
import io.gitea.model.IssueLabelsOption;
|
||||||
import io.gitea.model.IssueState;
|
import io.gitea.model.IssueState;
|
||||||
import io.gitea.model.Label;
|
import io.gitea.model.Label;
|
||||||
import io.gitea.model.Milestone;
|
import io.gitea.model.Milestone;
|
||||||
@ -184,4 +190,35 @@ public class GiteaConnection {
|
|||||||
public List<Comment> issueGetComments(Issue issue) throws ApiException {
|
public List<Comment> issueGetComments(Issue issue) throws ApiException {
|
||||||
return issueGetComments(issue, null, null);
|
return issueGetComments(issue, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Add a new comment to an issue.
|
||||||
|
*
|
||||||
|
* @param issueId
|
||||||
|
* @param body
|
||||||
|
* @return
|
||||||
|
* @throws ApiException
|
||||||
|
*/
|
||||||
|
public Comment issueAddComment(Long issueId, CreateIssueCommentOption body) throws ApiException {
|
||||||
|
return issueApi().issueCreateComment(owner, repo, issueId, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Comment issueAddComment(Issue issue, CreateIssueCommentOption body) throws ApiException {
|
||||||
|
return issueAddComment(issue.getId(), body);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Create an issue.
|
||||||
|
*
|
||||||
|
* @param body
|
||||||
|
* @return
|
||||||
|
* @throws ApiException
|
||||||
|
*/
|
||||||
|
public Issue createIssue(CreateIssueOption body) throws ApiException {
|
||||||
|
return issueApi().issueCreateIssue(owner, repo, body);
|
||||||
|
}
|
||||||
|
public Issue editIssue(Long issueId, EditIssueOption body) throws ApiException {
|
||||||
|
return issueApi().issueEditIssue(owner, repo, issueId, body);
|
||||||
|
}
|
||||||
|
public List<Label> issueReplaceLabels(Long issueId, IssueLabelsOption labels) throws ApiException {
|
||||||
|
return issueApi().issueReplaceLabels(owner, repo, issueId, labels);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
package io.gitea.mylyn.core;
|
package io.gitea.mylyn.core;
|
||||||
|
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
@ -12,10 +13,12 @@ import java.util.List;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.eclipse.core.internal.registry.OffsetTable;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
import org.eclipse.mylyn.tasks.core.ITaskMapping;
|
import org.eclipse.mylyn.tasks.core.ITaskMapping;
|
||||||
import org.eclipse.mylyn.tasks.core.RepositoryResponse;
|
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.TaskRepository;
|
||||||
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
|
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
|
||||||
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
|
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
|
||||||
@ -26,11 +29,17 @@ import org.eclipse.mylyn.tasks.core.data.TaskData;
|
|||||||
import org.eclipse.mylyn.tasks.core.data.TaskOperation;
|
import org.eclipse.mylyn.tasks.core.data.TaskOperation;
|
||||||
|
|
||||||
import io.gitea.ApiException;
|
import io.gitea.ApiException;
|
||||||
|
import io.gitea.api.IssueApi;
|
||||||
import io.gitea.model.Comment;
|
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.GiteaDateTimeUtils;
|
||||||
import io.gitea.model.GiteaPriorityLevel;
|
import io.gitea.model.GiteaPriorityLevel;
|
||||||
import io.gitea.model.GiteaUser;
|
import io.gitea.model.GiteaUser;
|
||||||
import io.gitea.model.Issue;
|
import io.gitea.model.Issue;
|
||||||
|
import io.gitea.model.IssueAction;
|
||||||
|
import io.gitea.model.IssueLabelsOption;
|
||||||
import io.gitea.model.IssueState;
|
import io.gitea.model.IssueState;
|
||||||
import io.gitea.model.IssueType;
|
import io.gitea.model.IssueType;
|
||||||
import io.gitea.model.Label;
|
import io.gitea.model.Label;
|
||||||
@ -39,237 +48,289 @@ import io.gitea.model.User;
|
|||||||
import io.gitea.mylyn.core.exceptions.GiteaException;
|
import io.gitea.mylyn.core.exceptions.GiteaException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the issues. It maps the attributes from the Gitea API to real Mylyn
|
* Handles the Issue<=>Task management. Downloads issues to tasks for a specific
|
||||||
* issue attributes, downloads the issues for a specific task repository, and
|
* repository, update issue and creates new issues.
|
||||||
* creates new issues.
|
|
||||||
*/
|
*/
|
||||||
public class GiteaTaskDataHandler extends AbstractTaskDataHandler {
|
public class GiteaTaskDataHandler extends AbstractTaskDataHandler {
|
||||||
|
|
||||||
public GiteaTaskDataHandler() {
|
public GiteaTaskDataHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TaskAttributeMapper getAttributeMapper(TaskRepository repository) {
|
public TaskAttributeMapper getAttributeMapper(TaskRepository repository) {
|
||||||
try {
|
try {
|
||||||
return ConnectionManager.get(repository).mapper;
|
return ConnectionManager.get(repository).mapper;
|
||||||
} catch (CoreException e) {
|
} catch (CoreException e) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public boolean initializeTaskData(TaskRepository repository, TaskData data, ITaskMapping mapping,
|
* Initialize Task from local repository.
|
||||||
IProgressMonitor monitor) throws CoreException {
|
*
|
||||||
createDefaultAttributes(data, false);
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean initializeTaskData(TaskRepository repository, TaskData data, ITaskMapping mapping,
|
||||||
|
IProgressMonitor monitor) throws CoreException {
|
||||||
|
createDefaultAttributes(data, false);
|
||||||
|
|
||||||
GiteaConnection connection = ConnectionManager.get(repository);
|
GiteaConnection connection = ConnectionManager.get(repository);
|
||||||
TaskAttribute root = data.getRoot();
|
TaskAttribute root = data.getRoot();
|
||||||
|
|
||||||
root.getAttribute(GiteaAttribute.PROJECT.getTaskKey()).setValue(connection.repository.getName());// FIXME: not
|
root.getAttribute(GiteaAttribute.PROJECT.getTaskKey()).setValue(connection.repository.getName());// FIXME: not
|
||||||
// the project
|
// the
|
||||||
root.getAttribute(GiteaAttribute.LABELS.getTaskKey()).setValue("");
|
// project
|
||||||
root.getAttribute(GiteaAttribute.STATUS.getTaskKey()).setValue("open");
|
root.getAttribute(GiteaAttribute.LABELS.getTaskKey()).setValue("");
|
||||||
root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).setValue("");
|
root.getAttribute(GiteaAttribute.STATUS.getTaskKey()).setValue("open");
|
||||||
|
root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).setValue("");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public RepositoryResponse postTaskData(TaskRepository repository, TaskData data, Set<TaskAttribute> attributes,
|
* Send Update to remote issues repository (create issue when required).
|
||||||
IProgressMonitor monitor) throws CoreException {
|
*
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public RepositoryResponse postTaskData(TaskRepository repository, TaskData data, Set<TaskAttribute> attributes,
|
||||||
|
IProgressMonitor monitor) throws CoreException {
|
||||||
|
|
||||||
GiteaAttributeMapper attributeMapper = (GiteaAttributeMapper) data.getAttributeMapper();
|
GiteaAttributeMapper attributeMapper = (GiteaAttributeMapper) data.getAttributeMapper();
|
||||||
|
TaskAttribute root = data.getRoot();
|
||||||
|
String taskId = data.getTaskId();
|
||||||
|
|
||||||
TaskAttribute root = data.getRoot();
|
// newLabelsList could be null or may content only one 'null' item when no label
|
||||||
String labels = root.getAttribute(GiteaAttribute.LABELS.getTaskKey()).getValue();
|
// is defined for the task
|
||||||
String title = root.getAttribute(GiteaAttribute.TITLE.getTaskKey()).getValue();
|
List<Label> newLabelsList = attributeMapper
|
||||||
String body = root.getAttribute(GiteaAttribute.BODY.getTaskKey()).getValue();
|
.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());
|
||||||
|
});
|
||||||
|
|
||||||
Long assigneeId = 0L;
|
String title = root.getAttribute(GiteaAttribute.TITLE.getTaskKey()).getValue();
|
||||||
|
String body = root.getAttribute(GiteaAttribute.BODY.getTaskKey()).getValue();
|
||||||
|
|
||||||
// TODO : confirm gitea behaviour
|
User assignee = null;
|
||||||
// We have to check, if the assignee has changed. The gitea api leaves three
|
// Check assignee is still part of team member
|
||||||
// posiblities for the assignee ID:
|
for (TaskAttribute a : attributes) {
|
||||||
// 0: leave as it is
|
if (a.getId().equals(GiteaAttribute.ASSIGNEE.getTaskKey())) {
|
||||||
// -1: unassign
|
assignee = attributeMapper
|
||||||
// real id: assign
|
.findProjectMemberByName(root.getAttribute(GiteaAttribute.ASSIGNEE.getTaskKey()).getValue());
|
||||||
// If we didnt do this, Gitea would create a comment everytime we edit the
|
}
|
||||||
// issue and there is still no
|
}
|
||||||
// assignee
|
|
||||||
for (TaskAttribute a : attributes) {
|
|
||||||
if (a.getId().equals(GiteaAttribute.ASSIGNEE.getTaskKey())) {
|
|
||||||
User assignee = attributeMapper
|
|
||||||
.findProjectMemberByName(root.getAttribute(GiteaAttribute.ASSIGNEE.getTaskKey()).getValue());
|
|
||||||
assigneeId = (assignee == null ? -1 : assignee.getId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Milestone milestone = attributeMapper
|
Milestone milestone = attributeMapper
|
||||||
.findMilestoneByName(root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).getValue());
|
.findMilestoneByName(root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).getValue());
|
||||||
Long milestoneId = (milestone == null ? 0 : milestone.getId());
|
Long milestoneId = (milestone == null ? 0 : milestone.getId());
|
||||||
|
|
||||||
/*
|
GiteaConnection connection = ConnectionManager.get(repository);
|
||||||
* FIXME: Support issue creation GiteaConnection connection =
|
// FIXME:TODO: support due date
|
||||||
* ConnectionManager.get(repository); Gitlea API api = connection.api(); try {
|
// OffsetDateTime dueDate =
|
||||||
* monitor.beginTask("Uploading task", IProgressMonitor.UNKNOWN); Issue issue =
|
// OffsetDateTime.GiteaDateTimeUtils.parseDate(root.getAttribute(GiteaAttribute.DUE_DATE.getTaskKey()).getValue());
|
||||||
* null; if(data.isNew()) { issue =
|
|
||||||
* api.createIssue(connection.repository.getId(), assigneeId, milestoneId,
|
|
||||||
* labels, body, title); return new
|
|
||||||
* RepositoryResponse(ResponseKind.TASK_CREATED, "" + issue.getIid()); } else {
|
|
||||||
* if(root.getAttribute(TaskAttribute.COMMENT_NEW) != null &&
|
|
||||||
* !root.getAttribute(TaskAttribute.COMMENT_NEW).getValue().equals("")) {
|
|
||||||
* api.createNote(connection.repository.getId(),
|
|
||||||
* GiteaConnector.getTicketId(data.getTaskId()),
|
|
||||||
* root.getAttribute(TaskAttribute.COMMENT_NEW).getValue()); } String action =
|
|
||||||
* root.getAttribute(TaskAttribute.OPERATION).getValue(); issue =
|
|
||||||
* api.editIssue(connection.repository.getId(),
|
|
||||||
* GiteaConnector.getTicketId(data.getTaskId()), assigneeId, milestoneId,
|
|
||||||
* labels, body, title, GiteaAction.find(action).getGiteaIssueAction()); return
|
|
||||||
* new RepositoryResponse(ResponseKind.TASK_UPDATED, "" + issue.getIid()); } }
|
|
||||||
* catch (IOException e) { throw new
|
|
||||||
* GiteaException("Unknown connection error!"); } finally { monitor.done(); }
|
|
||||||
*/
|
|
||||||
return null;// FIXME:
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskData downloadTaskData(TaskRepository repository, Long ticketId) throws CoreException {
|
try {
|
||||||
try {
|
//monitor.beginTask("Uploading task", IProgressMonitor.UNKNOWN);
|
||||||
GiteaConnection connection = ConnectionManager.get(repository);
|
Issue issue = null;
|
||||||
Issue issue = connection.issueGetIssue(ticketId);
|
if (data.isNew()) {
|
||||||
List<Comment> notes = connection.issueGetComments(issue);
|
issue = connection.createIssue(
|
||||||
return createTaskDataFromGiteaIssue(issue, repository, notes);
|
new CreateIssueOption().title(title).body(body).assignee(GiteaUser.getName(assignee))
|
||||||
} catch (ApiException e) {
|
.closed(false).milestone(milestoneId).labels(newLabelIds));
|
||||||
throw new GiteaException("Unknown connection error!");
|
// 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);
|
||||||
|
|
||||||
public TaskData createTaskDataFromGiteaIssue(Issue issue, TaskRepository repository, List<Comment> notes)
|
// A new comment is available
|
||||||
throws CoreException {
|
if (root.getAttribute(TaskAttribute.COMMENT_NEW) != null
|
||||||
GiteaConnection connection = ConnectionManager.get(repository);
|
&& !root.getAttribute(TaskAttribute.COMMENT_NEW).getValue().equals("")) {
|
||||||
TaskData data = new TaskData(connection.mapper, GiteaPluginCore.CONNECTOR_KIND, repository.getUrl(),
|
|
||||||
issue.getNumber().toString());
|
|
||||||
|
|
||||||
// Labels
|
connection.issueAddComment(issueId, new CreateIssueCommentOption()
|
||||||
List<String> labelsNames = new ArrayList<String>();
|
.body(root.getAttribute(TaskAttribute.COMMENT_NEW).getValue()));
|
||||||
List<Label> issueLabels = issue.getLabels();
|
}
|
||||||
issueLabels.forEach((label) -> labelsNames.add(label.getName()));
|
// Update labels if label list has changed
|
||||||
String labels = StringUtils.join(labelsNames, ", ");
|
List<Label>oldLabels = issue.getLabels();
|
||||||
|
|
||||||
createDefaultAttributes(data, true);
|
if (!issue.getLabels().equals(newLabelsList)) {
|
||||||
|
connection.issueReplaceLabels(issueId, new IssueLabelsOption().labels(newLabelIds));
|
||||||
|
}
|
||||||
|
|
||||||
TaskAttribute root = data.getRoot();
|
// Kept Step by step only for debugging purpose ...
|
||||||
root.getAttribute(GiteaAttribute.AUTHOR.getTaskKey()).setValue(GiteaUser.getName(issue.getUser()));
|
String action = root.getAttribute(TaskAttribute.OPERATION).getValue();
|
||||||
root.getAttribute(GiteaAttribute.CREATED.getTaskKey()).setValue(issue.getCreatedAt().toString());
|
String state = issue.getState();
|
||||||
root.getAttribute(GiteaAttribute.BODY.getTaskKey()).setValue(issue.getBody() == null ? "" : issue.getBody());
|
String newState = IssueAction.getEnum(action).toState(IssueState.getEnum(state)).toString();
|
||||||
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());
|
// OffsetDateTime dueDate = new OffsetDateTime();
|
||||||
root.getAttribute(GiteaAttribute.PRIORITY.getTaskKey())
|
// FIXME:TODO: Support DueDate
|
||||||
.setValue(GiteaPriorityLevel.getPriority(labels).toString());
|
boolean unsetDueDate = true;
|
||||||
root.getAttribute(GiteaAttribute.TYPE.getTaskKey()).setValue(getType(labels));
|
|
||||||
|
|
||||||
if (issue.getMilestone() != null) {
|
issue = connection.editIssue(issueId,
|
||||||
root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).setValue(issue.getMilestone().getTitle());
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (issue.getUpdatedAt() != null) {
|
/**
|
||||||
root.getAttribute(GiteaAttribute.UPDATED.getTaskKey()).setValue(issue.getUpdatedAt().toString());
|
* 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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (IssueState.isClosed(issue.getState())) {
|
public TaskData createTaskDataFromGiteaIssue(Issue issue, TaskRepository repository, List<Comment> notes)
|
||||||
root.getAttribute(GiteaAttribute.COMPLETED.getTaskKey()).setValue(issue.getUpdatedAt().toString());
|
throws CoreException {
|
||||||
}
|
GiteaConnection connection = ConnectionManager.get(repository);
|
||||||
|
TaskData data = new TaskData(connection.mapper, GiteaPluginCore.CONNECTOR_KIND, repository.getUrl(),
|
||||||
|
issue.getNumber().toString());
|
||||||
|
|
||||||
// Assignee name is either FulleName either Login either empty
|
// Labels
|
||||||
List<String> assigneesNames = new ArrayList<String>();
|
List<String> labelsNames = new ArrayList<String>();
|
||||||
List<User> assignees = issue.getAssignees();
|
List<Label> issueLabels = issue.getLabels();
|
||||||
if (assignees != null) {
|
issueLabels.forEach((label) -> labelsNames.add(label.getName()));
|
||||||
assignees.forEach((assignee) -> assigneesNames.add(GiteaUser.getName(assignee)));
|
String labels = StringUtils.join(labelsNames, ", ");
|
||||||
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;
|
createDefaultAttributes(data, true);
|
||||||
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);
|
TaskAttribute root = data.getRoot();
|
||||||
for (GiteaAction action : actions) {
|
root.getAttribute(GiteaAttribute.AUTHOR.getTaskKey()).setValue(GiteaUser.getName(issue.getUser()));
|
||||||
TaskAttribute attribute = data.getRoot().createAttribute(TaskAttribute.PREFIX_OPERATION + action.label);
|
root.getAttribute(GiteaAttribute.CREATED.getTaskKey()).setValue(issue.getCreatedAt().toString());
|
||||||
TaskOperation.applyTo(attribute, action.label, action.label);
|
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());
|
||||||
|
|
||||||
return data;
|
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));
|
||||||
|
|
||||||
private void createDefaultAttributes(TaskData data, boolean existingTask) {
|
if (issue.getMilestone() != null) {
|
||||||
createAttribute(data, GiteaAttribute.BODY);
|
root.getAttribute(GiteaAttribute.MILESTONE.getTaskKey()).setValue(issue.getMilestone().getTitle());
|
||||||
createAttribute(data, GiteaAttribute.TITLE);
|
}
|
||||||
createAttribute(data, GiteaAttribute.LABELS);
|
|
||||||
createAttribute(data, GiteaAttribute.STATUS);
|
|
||||||
createAttribute(data, GiteaAttribute.PROJECT);
|
|
||||||
|
|
||||||
createAttribute(data, GiteaAttribute.CREATED);
|
if (issue.getUpdatedAt() != null) {
|
||||||
createAttribute(data, GiteaAttribute.COMPLETED);
|
root.getAttribute(GiteaAttribute.UPDATED.getTaskKey()).setValue(issue.getUpdatedAt().toString());
|
||||||
createAttribute(data, GiteaAttribute.UPDATED);
|
}
|
||||||
createAttribute(data, GiteaAttribute.ASSIGNEE);
|
|
||||||
createAttribute(data, GiteaAttribute.MILESTONE);
|
|
||||||
|
|
||||||
createAttribute(data, GiteaAttribute.IID);
|
if (IssueState.isClosed(issue.getState())) {
|
||||||
createAttribute(data, GiteaAttribute.PRIORITY);
|
root.getAttribute(GiteaAttribute.COMPLETED.getTaskKey()).setValue(issue.getUpdatedAt().toString());
|
||||||
createAttribute(data, GiteaAttribute.TYPE);
|
}
|
||||||
|
|
||||||
data.getRoot().getAttribute(GiteaAttribute.CREATED.getTaskKey()).setValue("" + (new Date().getTime()));
|
// 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());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (existingTask) {
|
int i = 0;
|
||||||
data.getRoot().createAttribute(TaskAttribute.COMMENT_NEW).getMetaData()
|
for (Comment note : notes) {
|
||||||
.setType(TaskAttribute.TYPE_LONG_RICH_TEXT).setReadOnly(false);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
createAttribute(data, GiteaAttribute.AUTHOR);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
TaskAttribute operation = data.getRoot().createAttribute(TaskAttribute.OPERATION);
|
return data;
|
||||||
operation.getMetaData().setType(TaskAttribute.TYPE_OPERATION);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void createAttribute(TaskData data, GiteaAttribute attribute) {
|
private void createDefaultAttributes(TaskData data, boolean existingTask) {
|
||||||
TaskAttribute attr = data.getRoot().createAttribute(attribute.getTaskKey());
|
createAttribute(data, GiteaAttribute.BODY);
|
||||||
TaskAttributeMetaData metaData = attr.getMetaData();
|
createAttribute(data, GiteaAttribute.TITLE);
|
||||||
metaData.setType(attribute.getType());
|
createAttribute(data, GiteaAttribute.LABELS);
|
||||||
metaData.setKind(attribute.getKind());
|
createAttribute(data, GiteaAttribute.STATUS);
|
||||||
metaData.setLabel(attribute.toString());
|
createAttribute(data, GiteaAttribute.PROJECT);
|
||||||
metaData.setReadOnly(attribute.isReadOnly());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
createAttribute(data, GiteaAttribute.CREATED);
|
||||||
* Returns the type string for Mylyn. Uses a regular expression to check for
|
createAttribute(data, GiteaAttribute.COMPLETED);
|
||||||
* types in the given label.
|
createAttribute(data, GiteaAttribute.UPDATED);
|
||||||
*
|
createAttribute(data, GiteaAttribute.ASSIGNEE);
|
||||||
* @param labels
|
createAttribute(data, GiteaAttribute.MILESTONE);
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private String getType(String labels) {
|
|
||||||
Matcher m = IssueType.PATTERN.matcher(labels);
|
|
||||||
if (m.find()) {
|
|
||||||
return m.group(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
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 "";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user