Skip to content

Commit

Permalink
moved get(TaskRepository) method into GitlabPluginCore, which is now a
Browse files Browse the repository at this point in the history
GitlabConnection manager, will be changed later

* this fixes the issue, where the gitlab project has been updated (new
milestones etc) and the local repository configuration is not up to date
  • Loading branch information
pweingardt committed Mar 12, 2014
1 parent 3b7e956 commit 0a5d9f9
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 138 deletions.
2 changes: 1 addition & 1 deletion de.weingardt.mylyn.gitlab.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Bundle-ClassPath: lib/gitlab-java.jar,
lib/jackson-core-asl-1.9.9.jar,
lib/jackson-mapper-asl-1.9.9.jar,
.
Bundle-Activator: de.weingardt.mylyn.gitlab.core.GitlabPlugin
Bundle-Activator: de.weingardt.mylyn.gitlab.core.GitlabPluginCore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum GitlabAttribute {
TaskAttribute.TYPE_SHORT_TEXT, GitlabFlag.ATTRIBUTE, GitlabFlag.READ_ONLY),

LABELS("Labels", GitlabAttributeKeys.labelsKey,
TaskAttribute.TYPE_SHORT_TEXT, GitlabFlag.ATTRIBUTE),
TaskAttribute.TYPE_LONG_TEXT, GitlabFlag.ATTRIBUTE),

UPDATED("Updated", TaskAttribute.DATE_MODIFICATION,
TaskAttribute.TYPE_DATETIME, GitlabFlag.READ_ONLY, GitlabFlag.ATTRIBUTE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,15 @@
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.gitlab.api.GitlabAPI;
import org.gitlab.api.models.GitlabMilestone;
import org.gitlab.api.models.GitlabProjectMember;

public class GitlabAttributeMapper extends TaskAttributeMapper {

private List<GitlabMilestone> milestones;
private List<GitlabProjectMember> members;

public GitlabAttributeMapper(TaskRepository taskRepository) throws CoreException, IOException {
super(taskRepository);
}

public void update(GitlabConnection connection) throws IOException {
GitlabAPI api = connection.api();
milestones = api.getMilestones(connection.project);
members = api.getProjectMembers(connection.project);
}

@Override
public Map<String, String> getOptions(TaskAttribute attribute) {
switch(attribute.getId()) {
Expand All @@ -38,28 +28,44 @@ public Map<String, String> getOptions(TaskAttribute attribute) {
return super.getOptions(attribute);
}

private GitlabConnection getConnection() throws CoreException {
return GitlabPluginCore.get().get(getTaskRepository());
}

public GitlabProjectMember findProjectMemberByName(String name) {
for(GitlabProjectMember member : members) {
if(member.getName().equals(name) || member.getUsername().equals(name)) {
return member;
try {
List<GitlabProjectMember> members = getConnection().getProjectMembers();
for(GitlabProjectMember member : members) {
if(member.getName().equals(name) || member.getUsername().equals(name)) {
return member;
}
}
}
} catch (CoreException e) {
}
return null;
}

public GitlabMilestone findMilestoneByName(String name) {
for(GitlabMilestone m : milestones) {
if(m.getTitle().equals(name)) {
return m;
try {
List<GitlabMilestone> milestones = getConnection().getMilestones();
for(GitlabMilestone m : milestones) {
if(m.getTitle().equals(name)) {
return m;
}
}
} catch (CoreException e) {
}
return null;
}

private List<String> getMilestones() {
List<String> target = new ArrayList<>();
for(GitlabMilestone m : milestones) {
target.add(m.getTitle());
try {
List<GitlabMilestone> milestones = getConnection().getMilestones();
for(GitlabMilestone m : milestones) {
target.add(m.getTitle());
}
} catch (CoreException e) {
}
return target;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package de.weingardt.mylyn.gitlab.core;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.gitlab.api.GitlabAPI;
import org.gitlab.api.models.GitlabMilestone;
import org.gitlab.api.models.GitlabProject;
import org.gitlab.api.models.GitlabProjectMember;
import org.gitlab.api.models.GitlabSession;


Expand All @@ -12,6 +18,9 @@ public class GitlabConnection {
public final GitlabProject project;
public final GitlabAttributeMapper mapper;

private List<GitlabMilestone> milestones;
private List<GitlabProjectMember> members;

public GitlabConnection(String host, GitlabProject project, GitlabSession session,
GitlabAttributeMapper mapper) {
this.host = host;
Expand All @@ -24,4 +33,17 @@ public GitlabAPI api() {
return GitlabAPI.connect(host, session.getPrivateToken());
}

public void update() throws IOException {
milestones = api().getMilestones(project);
members = api().getProjectMembers(project);
}

public List<GitlabMilestone> getMilestones() {
return Collections.unmodifiableList(milestones);
}

public List<GitlabProjectMember> getProjectMembers() {
return Collections.unmodifiableList(members);
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package de.weingardt.mylyn.gitlab.core;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
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.commons.net.AuthenticationType;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.ITask;
Expand All @@ -20,52 +17,12 @@
import org.eclipse.mylyn.tasks.core.sync.ISynchronizationSession;
import org.gitlab.api.GitlabAPI;
import org.gitlab.api.models.GitlabIssue;
import org.gitlab.api.models.GitlabProject;
import org.gitlab.api.models.GitlabSession;

import de.weingardt.mylyn.gitlab.core.exceptions.GitlabException;

public class GitlabConnector extends AbstractRepositoryConnector {

private HashMap<String, GitlabConnection> connections = new HashMap<>();

private GitlabTaskDataHandler handler = new GitlabTaskDataHandler(this);

public GitlabConnection get(TaskRepository repository) throws CoreException {
return get(repository, false);
}

private GitlabConnection get(TaskRepository repository, boolean forceUpdate) throws GitlabException {
if(connections.containsKey(repository.getUrl()) && !forceUpdate) {
return connections.get(repository.getUrl());
} else {
try {
URL url = new URL(repository.getUrl());
String projectPath = url.getPath().substring(1);
String host = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort();
String username = repository.getCredentials(AuthenticationType.REPOSITORY).getUserName();
String password= repository.getCredentials(AuthenticationType.REPOSITORY).getPassword();

GitlabSession session = GitlabAPI.connect(host, username, password);

GitlabAPI api = GitlabAPI.connect(host, session.getPrivateToken());

List<GitlabProject> projects = api.getProjects();
for(GitlabProject p : projects) {
if(p.getPathWithNamespace().equals(projectPath)) {
GitlabConnection connection = new GitlabConnection(host, p, session,
new GitlabAttributeMapper(repository));
connection.mapper.update(connection);
connections.put(repository.getUrl(), connection);
return connection;
}
}
} catch(Exception | Error e) {
throw new GitlabException("Failed to connect to: " + repository.getUrl());
}
}
throw new GitlabException("Failed to connect to: " + repository.getUrl());
}
private GitlabTaskDataHandler handler = new GitlabTaskDataHandler();

@Override
public boolean canCreateNewTask(TaskRepository repository) {
Expand All @@ -79,7 +36,7 @@ public boolean canCreateTaskFromKey(TaskRepository repository) {

@Override
public String getConnectorKind() {
return GitlabPlugin.CONNECTOR_KIND;
return GitlabPluginCore.CONNECTOR_KIND;
}

@Override
Expand Down Expand Up @@ -119,7 +76,7 @@ public IStatus performQuery(TaskRepository repository, IRepositoryQuery query,
IProgressMonitor monitor) {

try {
GitlabConnection connection = get(repository);
GitlabConnection connection = GitlabPluginCore.get().get(repository);
GitlabAPI api = connection.api();

GitlabIssueSearch search = new GitlabIssueSearch(query);
Expand All @@ -135,13 +92,13 @@ public IStatus performQuery(TaskRepository repository, IRepositoryQuery query,
} catch (CoreException | Error | IOException e) {
}

return new Status(Status.ERROR, GitlabPlugin.ID_PLUGIN, "Unable to execute Query!");
return new Status(Status.ERROR, GitlabPluginCore.ID_PLUGIN, "Unable to execute Query!");
}

@Override
public void updateRepositoryConfiguration(TaskRepository repository,
IProgressMonitor monitor) throws CoreException {
get(repository, true);
GitlabPluginCore.get().get(repository, true);
}

@Override
Expand All @@ -153,7 +110,7 @@ public void updateTaskFromTaskData(TaskRepository repository, ITask task, TaskDa

public static void validate(TaskRepository taskRepo) throws CoreException {
try {
GitlabPlugin.get().getConnector().get(taskRepo);
GitlabPluginCore.get().get(taskRepo);
} catch (Exception | Error e) {
throw new GitlabException("Connection not successful or repository not found!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
package de.weingardt.mylyn.gitlab.core;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.gitlab.api.models.GitlabIssue;

public class GitlabIssueSearch {

private final static String priorityPatternFormat = "priority:(%s)";
private final static String typePatternFormat = "type:(%s)";


private Pattern titlePattern;
private Pattern descriptionPattern;
private Pattern labelPattern;
private Pattern assigneePattern;

private String assignee;
private String milestone;
private String state;

private List<Pattern> labelPatterns = new ArrayList<Pattern>();

private static int flags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;

public GitlabIssueSearch(IRepositoryQuery query) {
titlePattern = Pattern.compile(query.getAttribute("title"), flags);
descriptionPattern = Pattern.compile(query.getAttribute("description"), flags);
labelPattern = Pattern.compile(query.getAttribute("label"), flags);
assigneePattern = Pattern.compile(query.getAttribute("assignee"), flags);

if(!query.getAttribute("label").equals("")) {
labelPatterns.add(Pattern.compile(query.getAttribute("label"), flags));
}

if(!query.getAttribute("priority").equals("")) {
labelPatterns.add(
Pattern.compile(String.format(priorityPatternFormat, query.getAttribute("priority")),
flags));
}

if(!query.getAttribute("type").equals("")) {
labelPatterns.add(
Pattern.compile(String.format(typePatternFormat, query.getAttribute("type")),
flags));
}

milestone = query.getAttribute("milestone");
assignee = query.getAttribute("assignee");
state = query.getAttribute("state");
}

Expand All @@ -29,15 +56,30 @@ public boolean doesMatch(GitlabIssue issue) {
return false;
}

if(!descriptionPattern.pattern().equals("") && !descriptionPattern.matcher(issue.getTitle()).find()) {
if(!descriptionPattern.pattern().equals("") && !descriptionPattern.matcher(issue.getDescription()).find()) {
return false;
}

if(!labelPattern.pattern().equals("") && !labelPattern.matcher(issue.getTitle()).find()) {
Set<Pattern> matches = new HashSet<>();
for(Pattern p : labelPatterns) {
for(String label : issue.getLabels()) {
if(p.matcher(label).find()) {
matches.add(p);
}
}
}
if(matches.size() != labelPatterns.size()) {
return false;
}

if(!assigneePattern.pattern().equals("") && !assigneePattern.matcher(issue.getTitle()).find()) {
if(!assignee.equals("") &&
(issue.getAssignee() == null || !assignee.equals(issue.getAssignee().getUsername()))) {
return false;
}


if(!milestone.equals("") &&
(issue.getMilestone() == null || !milestone.equals(issue.getMilestone().getTitle()))) {
return false;
}

Expand Down

This file was deleted.

Loading

0 comments on commit 0a5d9f9

Please sign in to comment.