Skip to content

Commit

Permalink
add http downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
stCarolas committed Apr 9, 2020
1 parent 8cc4006 commit 00f3676
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 26 deletions.
2 changes: 1 addition & 1 deletion core/pom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ parent:

dependencies:
- {groupId: com.typesafe, artifactId: config, version: 1.3.4}
- {groupId: io.vavr, artifactId: vavr, version: 1.0.0}
- {groupId: io.vavr, artifactId: vavr, version: 1.0.1}
- {groupId: org.apache.logging.log4j, artifactId: log4j-api, version: 2.12.1}
- {groupId: org.apache.logging.log4j, artifactId: log4j-core, version: 2.12.1}
- {groupId: org.eclipse.jgit, artifactId: org.eclipse.jgit, version: 5.4.0.201906121030-r}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import java.util.function.Supplier;

import com.github.stcarolas.enki.core.Repo;
import com.github.stcarolas.enki.core.provider.strategies.download.GitCloneDownloadStrategy;
import static com.github.stcarolas.enki.core.provider.strategies.upload.GitPushUploadStrategy.GitPushUploadStrategy;
import static com.github.stcarolas.enki.core.provider.strategies.download.GitCloneDownloadStrategy.GitSshClone;
import static com.github.stcarolas.enki.core.provider.strategies.download.GitHttpDownloadStrategy.GitHttpClone;
import static com.github.stcarolas.enki.core.provider.strategies.upload.GitPushUploadStrategy.GitPush;

import org.eclipse.jgit.transport.PushResult;

Expand All @@ -16,11 +17,15 @@
public class DefaultRepoProviderStrategiesFactory {

public static <T extends Repo>Supplier<Iterable<PushResult>> gitSshPush(T repo){
return GitPushUploadStrategy(repo);
return GitPush(repo);
}

public static <T extends Repo>Supplier<File> gitSshClone(T repo, String sshUrl){
return GitCloneDownloadStrategy.GitSshClone(repo,sshUrl);
public static <T extends Repo>Supplier<File> gitSshClone(T repo, String url){
return GitSshClone(repo,url);
}

public static <T extends Repo>Supplier<File> gitHttpClone(T repo, String url){
return GitHttpClone(repo,url);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.github.stcarolas.enki.core.provider.strategies.download;

import static com.github.stcarolas.enki.core.util.Lifting.call;
import static io.vavr.API.Option;
import static io.vavr.API.Try;

import java.io.File;
import java.util.function.Supplier;

import com.github.stcarolas.enki.core.Repo;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import io.vavr.Function4;
import io.vavr.control.Option;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.With;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
public class GitHttpCloneWithAllBranches<T extends Repo> implements Supplier<File> {

@With private final Option<String> url;
@With private final Option<T> repository;
@With private final Supplier<CloneCommand> cloneCommand;
@With private final Supplier<CredentialsProvider> credentials;
@With private final Function4<
Supplier<CredentialsProvider>, Supplier<CloneCommand>, String, File,
CloneCommand
> clone;

public static final Supplier<CloneCommand> cloneCommandFn =
() -> Git.cloneRepository();

public static final Supplier<CredentialsProvider> credentialsFn =
() -> new UsernamePasswordCredentialsProvider("","");

public static final Function4<
Supplier<CredentialsProvider>, Supplier<CloneCommand>, String, File,
CloneCommand
> cloneFn = ( credentials, cloneCommand, url, dir ) ->
cloneCommand.get()
.setURI(url)
.setCloneAllBranches(true)
.setDirectory(dir)
.setCredentialsProvider(credentials.get());

@Override
public File get(){
var dir = repository.flatMap(
repo -> call(repo::directory)
.onEmpty(() -> log.error("missing any directory to clone into"))
);
var cloneResult = Option(clone)
.map($ -> $.apply(credentials))
.map($ -> $.apply(cloneCommand))
.flatMap($ -> url.map($::apply))
.flatMap($ -> dir.map($::apply))
.toTry()
.mapTry(CloneCommand::call)
.onFailure( error -> log.error("error while cloning {}: {}", url, error) )
.onSuccess( git -> log.info("repository with url {} was cloned", url) );
return cloneResult.isSuccess() ? dir.getOrNull() : null;
}

public static <T extends Repo>Supplier<File> GitHttpClone(T repo, String url){
return GitHttpDownloadStrategy.builder()
.url(
Option(url).onEmpty(() -> log.error("missing ssh url to use for cloning"))
)
.repository(
Option((Repo)repo).onEmpty(
() -> log.error("missing repository to clone for url {}", url)
)
)
.credentials(credentialsFn)
.cloneCommand(cloneCommandFn)
.clone(cloneFn)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.github.stcarolas.enki.core.provider.strategies.download;

import static com.github.stcarolas.enki.core.util.Lifting.call;
import static io.vavr.API.Option;
import static io.vavr.API.Try;

import java.io.File;
import java.util.function.Supplier;

import com.github.stcarolas.enki.core.Repo;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import io.vavr.Function4;
import io.vavr.control.Option;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.With;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
public class GitHttpDownloadStrategy<T extends Repo> implements Supplier<File> {

@With private final Option<String> url;
@With private final Option<T> repository;
@With private final Supplier<CloneCommand> cloneCommand;
@With private final Supplier<CredentialsProvider> credentials;
@With private final Function4<
Supplier<CredentialsProvider>, Supplier<CloneCommand>, String, File,
CloneCommand
> clone;

public static final Supplier<CloneCommand> cloneCommandFn =
() -> Git.cloneRepository();

public static final Supplier<CredentialsProvider> credentialsFn =
() -> new UsernamePasswordCredentialsProvider("","");

public static final Function4<
Supplier<CredentialsProvider>, Supplier<CloneCommand>, String, File,
CloneCommand
> cloneFn = ( credentials, cloneCommand, url, dir ) ->
cloneCommand.get()
.setURI(url)
.setCloneAllBranches(true)
.setDirectory(dir)
.setCredentialsProvider(credentials.get());

@Override
public File get(){
var dir = repository.flatMap(
repo -> call(repo::directory)
.onEmpty(() -> log.error("missing any directory to clone into"))
);
var cloneResult = Option(clone)
.map($ -> $.apply(credentials))
.map($ -> $.apply(cloneCommand))
.flatMap($ -> url.map($::apply))
.flatMap($ -> dir.map($::apply))
.toTry()
.mapTry(CloneCommand::call)
.onFailure( error -> log.error("error while cloning {}: {}", url, error) )
.onSuccess( git -> log.info("repository with url {} was cloned", url) );
return cloneResult.isSuccess() ? dir.getOrNull() : null;
}

public static <T extends Repo>Supplier<File> GitHttpClone(T repo, String url){
return GitHttpDownloadStrategy.builder()
.url(
Option(url).onEmpty(() -> log.error("missing ssh url to use for cloning"))
)
.repository(
Option((Repo)repo).onEmpty(
() -> log.error("missing repository to clone for url {}", url)
)
)
.credentials(credentialsFn)
.cloneCommand(cloneCommandFn)
.clone(cloneFn)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.function.Supplier;
import com.github.stcarolas.enki.core.Repo;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.PushResult;

import io.vavr.Function1;
Expand All @@ -25,6 +26,7 @@
public class GitPushUploadStrategy<T extends Repo> implements Supplier<Iterable<PushResult>> {

private final Option<T> repository;
private final Supplier<CredentialsProvider> credentials;
private final Function1<File, Option<Git>> gitOpenFn;
private final Function1<Git, Option<Iterable<PushResult>>> gitPushFn;
private final Function1<Repo, Option<File>> repoDirectoryFn;
Expand Down Expand Up @@ -62,9 +64,24 @@ public Iterable<PushResult> get(){
;
}

public static <T extends Repo> Supplier<Iterable<PushResult>> GitPushUploadStrategy(T repo){
public static <T extends Repo> Supplier<Iterable<PushResult>> GitPush(T repo){
return GitPushUploadStrategy.builder()
.repository(Option(repo))
.credentials(None())
.repoDirectoryFn(defaultRepoDirectoryFn)
.gitOpenFn(defaultGitOpenFn)
.gitPushFn(defaultGitPushFn)
.build()
;
}

public static <T extends Repo> Supplier<Iterable<PushResult>> GitPush(
T repo,
CredentialsProvider credentials
){
return GitPushUploadStrategy.builder()
.repository(Option(repo))
.credentials(Option(credentials))
.repoDirectoryFn(defaultRepoDirectoryFn)
.gitOpenFn(defaultGitOpenFn)
.gitPushFn(defaultGitPushFn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ public Try<RevCommit> apply(String message) {
dir -> git.apply(dir)
.filterNot($ -> isClean.apply($))
.flatMap(stage)
.onFailure($ -> log.error("error while staging: ", $))
.flatMap(commit.apply(message))
.onFailure($ -> log.error("error while commiting: ", $))
)
.peek(revision -> log.info("Successful commit: {}", revision.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void error() throws Exception {
public class TestGitPushUploadStrategy {
@Test
public void use_default_functions() throws Exception {
var strategy = (GitPushUploadStrategy)GitPushUploadStrategy.GitPushUploadStrategy(null);
var strategy = (GitPushUploadStrategy)GitPushUploadStrategy.GitPush(null);
assertEquals(
GitPushUploadStrategy.defaultGitOpenFn,
strategy.getGitOpenFn()
Expand All @@ -111,11 +111,11 @@ public void use_default_functions() throws Exception {

@Test
public void setting_repo() throws Exception {
var strategy = (GitPushUploadStrategy)GitPushUploadStrategy.GitPushUploadStrategy(null);
var strategy = (GitPushUploadStrategy)GitPushUploadStrategy.GitPush(null);
assertTrue(strategy.getRepository().isEmpty());

var repo = mock(Repo.class);
strategy = (GitPushUploadStrategy)GitPushUploadStrategy.GitPushUploadStrategy(repo);
strategy = (GitPushUploadStrategy)GitPushUploadStrategy.GitPush(repo);
assertEquals(repo, strategy.getRepository().get());
}
}
Expand Down
3 changes: 2 additions & 1 deletion pom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ modules:
- core
- providers/bitbucket
- providers/gitea
- providers/gitlab
#- providers/github
#- providers/gitlab
#- providers/one-repo
#- server
#- handlers/discord-commit-hook
#- handlers/gitea-mirror
#- handlers/gitlab-push
#- handlers/github-archive-repo
#- handlers/gocd-handlers
#- handlers/jacoco
Expand Down
15 changes: 8 additions & 7 deletions providers/gitlab/pom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ modelVersion: 4.0.0
parent:
artifactId: enki-parent
groupId: com.github.stcarolas.enki
version: 1.0.1
version: 1.0.4

dependencies:
- {groupId: com.github.stcarolas.enki, artifactId: enki-core, version: '${project.version}'}
- {groupId: io.vavr, artifactId: vavr, version: 1.0.0}
- {groupId: org.apache.logging.log4j, artifactId: log4j-api, version: 2.12.1}
- {groupId: org.apache.logging.log4j, artifactId: log4j-core, version: 2.12.1}
- {groupId: org.projectlombok, artifactId: lombok, version: 1.18.8, scope: provided, optional: true}
- {groupId: org.gitlab4j, artifactId: gitlab4j-api, version: 4.14.15, exclusions:[
- {groupId: com.github.stcarolas.enki, artifactId: enki-core, version: '${project.version}'}
- {groupId: io.vavr, artifactId: vavr, version: 1.0.0}
- {groupId: org.apache.logging.log4j, artifactId: log4j-api, version: 2.12.1}
- {groupId: org.apache.logging.log4j, artifactId: log4j-core, version: 2.12.1}
- {groupId: org.projectlombok, artifactId: lombok, version: 1.18.8, scope: provided, optional: true}
- {groupId: org.eclipse.jgit, artifactId: org.eclipse.jgit, version: 5.4.0.201906121030-r}
- {groupId: org.gitlab4j, artifactId: gitlab4j-api, version: 4.14.15, exclusions:[
{groupId: com.fasterxml.jackson.jaxrs, artifactId: jackson-jaxrs-json-provider}
]}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

import com.github.stcarolas.enki.core.repo.StrategiesAsRepo;

import lombok.Getter;
import lombok.Setter;

public class GitlabRepo extends StrategiesAsRepo {
// TODO optional
@Getter
@Setter
String sshUrl;
// TODO make httpUrl for GitlabRepo as Optional
@Getter
@Setter
String httpUrl;
@Getter
@Setter
String path;
@Getter
@Setter
String namespace;
}
Loading

0 comments on commit 00f3676

Please sign in to comment.