Skip to content

Commit

Permalink
fix(gitlab-api): retrieve only one project when initializing connection
Browse files Browse the repository at this point in the history
  • Loading branch information
youdowell-au committed Jun 11, 2018
1 parent 0100b6f commit ce1da62
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 166 deletions.
11 changes: 6 additions & 5 deletions de.weingardt.mylyn.gitlab.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
Manifest-Version: 1.0
eManifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Mylyn Gitlab Connector Core
Bundle-SymbolicName: de.weingardt.mylyn.gitlab.core;singleton:=true
Bundle-Version: 1.6.0
Bundle-Version: 1.6.1
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.mylyn.commons.core;bundle-version="[3.8.0,4.0.0)",
org.eclipse.mylyn.commons.net;bundle-version="[3.8.0,4.0.0)",
org.eclipse.mylyn.tasks.core;bundle-version="[3.8.0,4.0.0)"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Export-Package: de.weingardt.mylyn.gitlab.core,
de.weingardt.mylyn.gitlab.core.exceptions,
org.gitlab.api.models
Export-Package: de.weingardt.mylyn.gitlab.core,de.weingardt.mylyn.gitl
ab.core.exceptions,org.gitlab.api.models
Bundle-Vendor: Weingardt Software
Bundle-ClassPath: .,
lib/commons-io-2.4.jar,
lib/jackson-annotations-2.5.3.jar,
lib/jackson-core-2.5.3.jar,
lib/jackson-databind-2.5.3.jar,
lib/slf4j-api-1.8.0-beta2.jar,
lib/slf4j-simple-1.8.0-beta2.jar,
lib/java-gitlab-api-4.0.1-SNAPSHOT.jar
Bundle-Activator: de.weingardt.mylyn.gitlab.core.GitlabPluginCore
2 changes: 2 additions & 0 deletions de.weingardt.mylyn.gitlab.core/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ bin.includes = META-INF/,\
lib/jackson-annotations-2.5.3.jar,\
lib/jackson-core-2.5.3.jar,\
lib/jackson-databind-2.5.3.jar,\
lib/slf4j-api-1.8.0-beta2.jar,\
lib/slf4j-simple-1.8.0-beta2.jar,\
about_files/,\
about.html,\
lib/java-gitlab-api-4.0.1-SNAPSHOT.jar
Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion de.weingardt.mylyn.gitlab.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<relativePath>../pom.xml</relativePath>
<groupId>de.weingardt.mylyn.gitlab</groupId>
<artifactId>de.weingardt.mylyn.gitlab-parent</artifactId>
<version>1.6.0</version>
<version>1.6.1</version>
</parent>

<artifactId>de.weingardt.mylyn.gitlab.core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.weingardt.mylyn.gitlab.core;

import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -16,160 +15,176 @@
import de.weingardt.mylyn.gitlab.core.exceptions.UnknownProjectException;

/**
* The ConnectionManager is a singleton that handles all
* GitlabConnection instances in a HashMap. The key in this HashMap
* is the URL to the Gitlab instance constructed using a TaskRepository
* The ConnectionManager is a singleton that handles all GitlabConnection instances in a HashMap.
* The key in this HashMap is the URL to the Gitlab instance constructed using a TaskRepository
* class.
* @author paul
*
* @author paul
*/
public class ConnectionManager {

/**
* The HashMap used to store all GitlabConnections
*/
private static HashMap<String, GitlabConnection> connections = new HashMap<String, GitlabConnection>();

/**
* The pattern is used to verify a ULR to a valid Gitlab project URL.
*/
private static Pattern URLPattern = Pattern.compile("((?:http|https)://(?:[^\\/]*))/((?:.*?)/(?:[^\\/]*?))$");

/**
* Returns the GitlabConnection for the given task repository
* @param repository
* @return
* @throws GitlabException
*/
static public GitlabConnection get(TaskRepository repository) throws GitlabException {
return get(repository, false);
}

/**
* Returns the GitlabConnection for the given task repository. If it
* failes for whatever reason, it returns null.
* @param repository
* @return
*/
static public GitlabConnection getSafe(TaskRepository repository) {
try {
return get(repository);
} catch (GitlabException e) {
return null;
}
}

/**
* Constructs a URL string for the given task repository.
* @param repository
* @return
*/
private static String constructURL(TaskRepository repository) {
String username = repository.getCredentials(AuthenticationType.REPOSITORY).getUserName();
String password = repository.getCredentials(AuthenticationType.REPOSITORY).getPassword();
return repository.getUrl() + "?username=" + username + "&password=" + password.hashCode();
}

/**
* Validates the given task repository and returns a GitlabConnection if
* the task repository is a valid repository.
* @param repository
* @return
* @throws GitlabException
*/
static GitlabConnection validate(TaskRepository repository) throws GitlabException {
try {
String projectPath = null;
String host = null;

if(repository.getProperty("gitlabBaseUrl").trim().length() > 0) {
host = repository.getProperty("gitlabBaseUrl").trim();
if(!repository.getUrl().startsWith(host)) {
throw new GitlabException("Invalid project URL!");
}

projectPath = repository.getUrl().replaceFirst(Matcher.quoteReplacement(host), "");
if(projectPath.startsWith("/")) {
projectPath = projectPath.substring(1);
}
} else {
Matcher matcher = URLPattern.matcher(repository.getUrl());
if(!matcher.find()) {
throw new GitlabException("Invalid Project-URL!");
}

projectPath = matcher.group(2);
host = matcher.group(1);
}

String username = repository.getCredentials(AuthenticationType.REPOSITORY).getUserName();
String password= repository.getCredentials(AuthenticationType.REPOSITORY).getPassword();

GitlabSession session = null;
String token = null;

if(repository.getProperty("usePrivateToken") != null && repository.getProperty("usePrivateToken").equals("true")) {
session = GitlabAPI.connect(host, password).getCurrentSession();
token = password;
} else {
session = GitlabAPI.connect(host, username, password);
token = session.getPrivateToken();
}

GitlabAPI api = GitlabAPI.connect(host, token);

if(projectPath.endsWith(".git")) {
projectPath = projectPath.substring(0, projectPath.length() - 4);
}

List<GitlabProject> projects = api.getProjects();
for(GitlabProject p : projects) {
if(p.getPathWithNamespace().equals(projectPath)) {
GitlabConnection connection = new GitlabConnection(host, p, token,
new GitlabAttributeMapper(repository));
return connection;
}
}
// At this point the authentication was successful, but the corresponding project
// could not be found!
throw new UnknownProjectException(projectPath);
} catch(GitlabException e) {
throw e;
} catch(Exception e) {
throw GitlabExceptionHandler.handle(e);
} catch(Error e) {
throw GitlabExceptionHandler.handle(e);
}
}

/**
* Returns a *valid* GitlabConnection, otherwise this method throws an exception.
* @param repository
* @param forceUpdate if true, a new GitlabConnection instance will be created, even if a Gitlab
* Connection already exists for the given task repository
* @return
* @throws GitlabException
*/
static GitlabConnection get(TaskRepository repository, boolean forceUpdate) throws GitlabException {
try {
String hash = constructURL(repository);
if(connections.containsKey(hash) && !forceUpdate) {
return connections.get(hash);
} else {
GitlabConnection connection = validate(repository);

connections.put(hash, connection);
connection.update();

return connection;
}
} catch(GitlabException e) {
throw e;
} catch(Exception e) {
throw GitlabExceptionHandler.handle(e);
} catch(Error e) {
throw GitlabExceptionHandler.handle(e);
}
}
/**
* The HashMap used to store all GitlabConnections
*/
private static HashMap<String, GitlabConnection> connections =
new HashMap<>();

/**
* The pattern is used to verify a ULR to a valid Gitlab project URL.
*/
private static Pattern URLPattern =
Pattern.compile("((?:http|https)://(?:[^\\/]*))/((?:.*?)/(?:[^\\/]*?))$");

/**
* Constructs a URL string for the given task repository.
*
* @param repository
* @return
*/
private static String constructURL(TaskRepository repository) {
final String username =
repository.getCredentials(AuthenticationType.REPOSITORY).getUserName();
final String password =
repository.getCredentials(AuthenticationType.REPOSITORY).getPassword();
return repository.getUrl() + "?username=" + username + "&password=" + password.hashCode();
}

/**
* Returns the GitlabConnection for the given task repository
*
* @param repository
* @return
* @throws GitlabException
*/
static public GitlabConnection get(TaskRepository repository) throws GitlabException {
return get(repository, false);
}

/**
* Returns a *valid* GitlabConnection, otherwise this method throws an exception.
*
* @param repository
* @param forceUpdate if true, a new GitlabConnection instance will be created, even if a Gitlab
* Connection already exists for the given task repository
* @return
* @throws GitlabException
*/
static GitlabConnection get(TaskRepository repository, boolean forceUpdate)
throws GitlabException {
try {
final String hash = constructURL(repository);
if (connections.containsKey(hash) && !forceUpdate) {
return connections.get(hash);
} else {
final GitlabConnection connection = validate(repository);

connections.put(hash, connection);
connection.update();

return connection;
}
} catch (final GitlabException e) {
throw e;
} catch (final Exception e) {
throw GitlabExceptionHandler.handle(e);
} catch (final Error e) {
throw GitlabExceptionHandler.handle(e);
}
}

/**
* Returns the GitlabConnection for the given task repository. If it failes for whatever reason,
* it returns null.
*
* @param repository
* @return
*/
static public GitlabConnection getSafe(TaskRepository repository) {
try {
return get(repository);
} catch (final GitlabException e) {
return null;
}
}

/**
* Validates the given task repository and returns a GitlabConnection if the task repository is a
* valid repository.
*
* @param repository
* @return
* @throws GitlabException
*/
static GitlabConnection validate(TaskRepository repository) throws GitlabException {
try {
String projectPath = null;
String host = null;

if (repository.getProperty("gitlabBaseUrl").trim().length() > 0) {
host = repository.getProperty("gitlabBaseUrl").trim();
if (!repository.getUrl().startsWith(host)) {
throw new GitlabException("Invalid project URL!");
}

projectPath = repository.getUrl().replaceFirst(Matcher.quoteReplacement(host), "");
if (projectPath.startsWith("/")) {
projectPath = projectPath.substring(1);
}
} else {
final Matcher matcher = URLPattern.matcher(repository.getUrl());
if (!matcher.find()) {
throw new GitlabException("Invalid Project-URL!");
}

projectPath = matcher.group(2);
host = matcher.group(1);
}

final String username =
repository.getCredentials(AuthenticationType.REPOSITORY).getUserName();
final String password =
repository.getCredentials(AuthenticationType.REPOSITORY).getPassword();

GitlabSession session = null;
String token = null;

if (repository.getProperty("usePrivateToken") != null
&& repository.getProperty("usePrivateToken").equals("true")) {
session = GitlabAPI.connect(host, password).getCurrentSession();
token = password;
} else {
session = GitlabAPI.connect(host, username, password);
token = session.getPrivateToken();
}

final GitlabAPI api = GitlabAPI.connect(host, token);

if (projectPath.endsWith(".git")) {
projectPath = projectPath.substring(0, projectPath.length() - 4);
}

final int projectSeparatorIdx = projectPath.lastIndexOf('/');
final String namespace = projectPath.substring(0, projectSeparatorIdx);
final String projectName = projectPath.substring(projectSeparatorIdx + 1);

final GitlabProject project = api.getProject(namespace, projectName);

if (null != project) {
final GitlabConnection connection = new GitlabConnection(host, project, token,
new GitlabAttributeMapper(repository));
return connection;
}

// At this point the authentication was successful, but the corresponding project
// could not be found!
throw new UnknownProjectException(projectPath);
} catch (final GitlabException e) {
throw e;
} catch (final Exception e) {
throw GitlabExceptionHandler.handle(e);
} catch (final Error e) {
throw GitlabExceptionHandler.handle(e);
}
}

}
2 changes: 1 addition & 1 deletion de.weingardt.mylyn.gitlab.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Mylyn Gitlab Connector UI
Bundle-SymbolicName: de.weingardt.mylyn.gitlab.ui;singleton:=true
Bundle-Version: 1.6.0
Bundle-Version: 1.6.1
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui,
Expand Down
2 changes: 1 addition & 1 deletion de.weingardt.mylyn.gitlab.ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<relativePath>../pom.xml</relativePath>
<groupId>de.weingardt.mylyn.gitlab</groupId>
<artifactId>de.weingardt.mylyn.gitlab-parent</artifactId>
<version>1.6.0</version>
<version>1.6.1</version>
</parent>

<artifactId>de.weingardt.mylyn.gitlab.ui</artifactId>
Expand Down
Loading

0 comments on commit ce1da62

Please sign in to comment.