118 lines
3.5 KiB
Java
118 lines
3.5 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.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.eclipse.core.runtime.CoreException;
|
|
import org.eclipse.mylyn.tasks.core.TaskRepository;
|
|
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
|
|
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
|
|
|
|
import io.gitea.model.GiteaDateTimeUtils;
|
|
import io.gitea.model.Label;
|
|
import io.gitea.model.Milestone;
|
|
import io.gitea.model.User;
|
|
|
|
public class GiteaAttributeMapper extends TaskAttributeMapper {
|
|
|
|
public GiteaAttributeMapper(TaskRepository taskRepository) throws CoreException, IOException {
|
|
super(taskRepository);
|
|
}
|
|
|
|
@Override
|
|
public Map<String, String> getOptions(TaskAttribute attribute) {
|
|
if (attribute.getId().equals(GiteaAttribute.MILESTONE.getTaskKey())) {
|
|
return getAsMap(getMilestones());
|
|
} else {
|
|
return super.getOptions(attribute);
|
|
}
|
|
}
|
|
|
|
private GiteaConnection getConnection() throws CoreException {
|
|
return ConnectionManager.get(getTaskRepository());
|
|
}
|
|
|
|
@Override
|
|
public Date getDateValue(TaskAttribute attribute) {
|
|
if (attribute == null) {
|
|
return null;
|
|
}
|
|
Date parsedDate = GiteaDateTimeUtils.parseDate(attribute.getValue());
|
|
if (parsedDate != null) {
|
|
return parsedDate;
|
|
}
|
|
return super.getDateValue(attribute);
|
|
}
|
|
|
|
public User findProjectMemberByName(String name) {
|
|
try {
|
|
List<User> members = getConnection().getRepositoryCollaborators();
|
|
for (User member : members) {
|
|
if (member.getFullName().equals(name) || member.getLogin().equals(name)) {
|
|
return member;
|
|
}
|
|
}
|
|
} catch (CoreException e) {
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public Milestone findMilestoneByName(String name) {
|
|
try {
|
|
List<Milestone> milestones = getConnection().getMilestones();
|
|
for (Milestone m : milestones) {
|
|
if (m.getTitle().equals(name)) {
|
|
return m;
|
|
}
|
|
}
|
|
} catch (CoreException e) {
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private List<String> getMilestones() {
|
|
List<String> target = new ArrayList<String>();
|
|
try {
|
|
List<Milestone> milestones = getConnection().getMilestones();
|
|
for (Milestone m : milestones) {
|
|
target.add(m.getTitle());
|
|
}
|
|
} catch (CoreException e) {
|
|
}
|
|
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) {
|
|
HashMap<String, String> map = new HashMap<String, String>();
|
|
map.put("", "");
|
|
for (String s : list) {
|
|
map.put(s, s);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
}
|