Skip to content

Commit

Permalink
A non-nonsense (and quite working) implementation of gitlab scm provi…
Browse files Browse the repository at this point in the history
…der (because yes, I finally got the hands on a non-trivial gitlab project, and it's dope)
  • Loading branch information
Riduidel committed Feb 15, 2024
1 parent 0275fde commit 00cfb4a
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 32 deletions.
18 changes: 18 additions & 0 deletions git-scm-handler-helper/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.Riduidel.aadarchi</groupId>
<artifactId>system</artifactId>
<version>0.1.13-SNAPSHOT</version>
</parent>
<artifactId>git-scm-handler-helper</artifactId>
<dependencies>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.ndx.aadarchi.github;
package org.ndx.aadarchi.gitlab;

import java.io.File;
import java.io.IOException;
Expand All @@ -8,35 +8,29 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import com.structurizr.annotation.Component;

/**
* Component dedicated to low-level git operations
* @author Nicolas
* A class providing basic it operations
*/
@Component
@ApplicationScoped
public class GitOperator {
@Inject @ConfigProperty(name=Constants.CONFIG_GITHUB_LOGIN) private String login;
@Inject @ConfigProperty(name=Constants.CONFIG_GITHUB_TOKEN) private String token;
private static final Logger logger = Logger.getLogger(GitOperator.class.getName());
private String login;
private String token;
private Set<String> branchesToCheckout;
@Inject
public void setBranchesToCheckout(@ConfigProperty(name=Constants.CONFIG_GIT_BRANCHES_TO_CHECKOUT, defaultValue = "develop, main, main") String names) {
/**
* Set the branches to checkout from a comma-separated string
* @param names
*/
public void setBranchesToCheckout(String names) {
this.branchesToCheckout = Arrays.asList(names.split(",")).stream()
.map(name -> name.trim())
.collect(Collectors.toSet());
}
@Inject Logger logger;

/**
* Clone the repo
Expand All @@ -46,10 +40,6 @@ public void setBranchesToCheckout(@ConfigProperty(name=Constants.CONFIG_GIT_BRAN
* @throws IOException if local file operation fails
*/
public void clone(String from, File into) throws GitAPIException, IOException {
if(login==null || login.isBlank()) {
throw new UnsupportedOperationException(
String.format("Unable to checkout projects of no Github login is provided. Please set the %s property to a non-null value", Constants.CONFIG_GITHUB_LOGIN));
}
if(into.exists()) {
if(new File(into, ".git").exists()) {
logger.info(String.format("%s seems to already be a git repository, we consider job's done.", into.getAbsolutePath()));
Expand Down Expand Up @@ -84,9 +74,29 @@ public void clone(String from, File into) throws GitAPIException, IOException {
.setAllPaths(true)
.setName(from);
}, () -> {
logger.warning(String.format("We found none of the %s branches in remote (but found %s). Please add one to %s in order for checkout to work",
branchesToCheckout, branchesNames, Constants.CONFIG_GIT_BRANCHES_TO_CHECKOUT));
logger.warning(String.format("We found none of the %s branches in remote (but found %s). Please add one to branches to checkout in order for checkout to work",
branchesToCheckout, branchesNames));
});
}
}

public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public Set<String> getBranchesToCheckout() {
return branchesToCheckout;
}
}
10 changes: 5 additions & 5 deletions github-scm-handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<artifactId>github-vfs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.Riduidel.aadarchi</groupId>
<artifactId>git-scm-handler-helper</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>markdown-to-asciidoc</artifactId>
Expand All @@ -28,11 +33,6 @@
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
</dependency>
<dependency>
<groupId>com.pivovarit</groupId>
<artifactId>throwing-function</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.ndx.aadarchi.github;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.ndx.aadarchi.gitlab.GitOperator;

import com.structurizr.annotation.Component;

/**
* Component dedicated to low-level git operations
* @author Nicolas
*/
public class GitOperatorProducer {
@Produces @Named("github")
public GitOperator buildGitOperator(
@ConfigProperty(name=Constants.CONFIG_GITHUB_LOGIN) String login,
@ConfigProperty(name=Constants.CONFIG_GITHUB_TOKEN) String token,
@ConfigProperty(name=Constants.CONFIG_GIT_BRANCHES_TO_CHECKOUT, defaultValue = "develop, main, main") String names) {
GitOperator returned = new GitOperator();
returned.setLogin(login);
returned.setToken(token);
returned.setBranchesToCheckout(names);
return returned;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
Expand All @@ -20,6 +21,7 @@
import org.ndx.aadarchi.base.utils.FileContentCache;
import org.ndx.aadarchi.base.utils.icon.FontIcon;
import org.ndx.aadarchi.github.vfs.GitHubFileSystemProvider;
import org.ndx.aadarchi.gitlab.GitOperator;

import com.structurizr.annotation.Component;

Expand All @@ -29,7 +31,7 @@ public class GithubSCMHandler implements SCMHandler {
@Inject Logger logger;
@Inject GitHub github;
@Inject FileContentCache fileCache;
@Inject Instance<GitOperator> cloner;
@Inject @Named("github") Instance<GitOperator> cloner;
@Inject @FontIcon(name="github") String githubIcon;
@Inject GitHubFileSystemProvider gitHubFileSystem;
@Override
Expand Down
9 changes: 9 additions & 0 deletions gitlab-scm-handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<artifactId>gitlab-vfs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.Riduidel.aadarchi</groupId>
<artifactId>git-scm-handler-helper</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
Expand All @@ -27,6 +32,10 @@
<groupId>com.pivovarit</groupId>
<artifactId>throwing-function</artifactId>
</dependency>
<dependency>
<groupId>com.pivovarit</groupId>
<artifactId>throwing-function</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
public interface Constants {

String CONFIG_GITLAB_TOKEN = ModelElementKeys.PREFIX+"gitlab.token";
String CONFIG_GITLAB_LOGIN = ModelElementKeys.PREFIX+"gitlab.login";
String CONFIG_GITLAB_URL = ModelElementKeys.PREFIX+"gitlab.url";
String CONFIG_GIT_BRANCHES_TO_CHECKOUT = ModelElementKeys.PREFIX+"git.branches.to.checkout";
static boolean isGitLabProject(GitLabApi api, String project) {
return project.contains(api.getGitLabServerUrl());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.ndx.aadarchi.gitlab;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.ndx.aadarchi.gitlab.GitOperator;

import com.structurizr.annotation.Component;

/**
* Component dedicated to low-level git operations
* @author Nicolas
*/
public class GitOperatorProducer {
@Produces @Named("gitlab")
public GitOperator buildGitOperator(
@ConfigProperty(name=Constants.CONFIG_GITLAB_LOGIN) String login,
@ConfigProperty(name=Constants.CONFIG_GITLAB_TOKEN) String token,
@ConfigProperty(name=Constants.CONFIG_GIT_BRANCHES_TO_CHECKOUT, defaultValue = "develop, main, main") String names) {
GitOperator returned = new GitOperator();
returned.setLogin(login);
returned.setToken(token);
returned.setBranchesToCheckout(names);
return returned;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package org.ndx.aadarchi.gitlab;


import java.io.File;
import java.io.IOException;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.gitlab4j.api.GitLabApiException;
import org.ndx.aadarchi.base.enhancers.scm.SCMHandler;
import org.ndx.aadarchi.base.utils.icon.FontIcon;
import org.ndx.aadarchi.gitlab.vfs.GitLabFileSystemProvider;

import com.structurizr.annotation.Component;

@Component
public class GitlabSCMHandler implements SCMHandler {
private @Inject GitLabContainer gitlab;
@Inject @FontIcon(name="gitlab") String gitlabIcon;
@Inject @Named("gitlab") Instance<GitOperator> cloner;
@Inject
GitLabFileSystemProvider gitlabFileSystem;
@Inject
@FontIcon(name = "gitlab")
String gitlabIcon;

@Override
public boolean canHandle(String project) {
Expand All @@ -34,11 +44,22 @@ public String asciidocText() {

@Override
public void checkout(String projectUrl, File checkoutLocation) throws IOException {
throw new UnsupportedOperationException(String.format("SCMHandler#checkout(%s,%s) is not yet implemented in GitlabSCMHandler. Sorry", projectUrl, checkoutLocation.getAbsolutePath()));
if(Constants.isGitLabProject(gitlab.getApi(), projectUrl)) {
try {
String httpTransportUrl = gitlab.getApi().getProjectApi().getProject(projectUrl).getHttpUrlToRepo();
cloner.get().clone(httpTransportUrl, checkoutLocation);
} catch (GitLabApiException | GitAPIException e) {
throw new IOException(String.format("Unable to clone %s to %s", projectUrl, checkoutLocation.getAbsolutePath()), e);
}
}
}

@Override
public FileObject getProjectRoot(String project) {
throw new UnsupportedOperationException("TODO Implement "+getClass().getSimpleName()+"#getProjectRoot()");
try {
return gitlabFileSystem.getProjectRoot(project);
} catch (FileSystemException e) {
throw new GitLabHandlerException("Unable to obtain VFS", e);
}
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<module>structurizr-components-detector</module>
<module>aadarchi-test-utils</module>
<module>freemarker-cdi-producer</module>
<module>git-scm-handler-helper</module>
</modules>
<dependencyManagement>
<dependencies>
Expand Down

0 comments on commit 00cfb4a

Please sign in to comment.