-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
58 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 49 additions & 32 deletions
81
...java/com/github/stcarolas/enki/core/provider/strategies/upload/GitPushUploadStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,74 @@ | ||
package com.github.stcarolas.enki.core.provider.strategies.upload; | ||
|
||
import static com.github.stcarolas.enki.core.util.Lifting.call; | ||
import static io.vavr.control.Option.of; | ||
import static io.vavr.collection.List.empty; | ||
|
||
import java.io.File; | ||
import java.util.function.Supplier; | ||
|
||
import com.github.stcarolas.enki.core.Repo; | ||
|
||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.transport.PushResult; | ||
|
||
import io.vavr.Function1; | ||
import io.vavr.control.Option; | ||
import io.vavr.control.Try; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.extern.log4j.Log4j2; | ||
import static io.vavr.API.*; | ||
|
||
@Log4j2 | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Builder | ||
@Getter | ||
public class GitPushUploadStrategy<T extends Repo> implements Supplier<Iterable<PushResult>> { | ||
private final Option<String> sshUrl; | ||
|
||
private final Option<T> repository; | ||
private final Function1<File, Option<Git>> gitOpenFn; | ||
private final Function1<Git, Option<Iterable<PushResult>>> gitPushFn; | ||
private final Function1<Repo, Option<File>> repoDirectoryFn; | ||
|
||
// todo test for defaultGitOpenFn in GitPushUploadStrategy | ||
public static final Function1<File, Option<Git>> defaultGitOpenFn = directory -> | ||
Try(() -> Git.open(directory)) | ||
.onFailure( | ||
error -> log.error( "error while acessing repository aborting push: {}", error) | ||
) | ||
.toOption() | ||
; | ||
|
||
public static final Function1<Git, Option<Iterable<PushResult>>> defaultGitPushFn = git -> | ||
Try(() -> git.push().call()) | ||
.onFailure( error -> log.error("error while pushing repo: {}", error) ) | ||
.onSuccess( result -> log.info("git push result: {}", result) ) | ||
.toOption() | ||
; | ||
|
||
public static final Function1<Repo, Option<File>> defaultRepoDirectoryFn = repo -> | ||
call(repo::directory) | ||
.onEmpty( () -> log.error( "missing directory for repository {}, aborting push", repo) ) | ||
; | ||
|
||
@Override | ||
public Iterable<PushResult> get() { | ||
return sshUrl | ||
.onEmpty(() -> log.error("missing ssh url to push, aborting")) | ||
.flatMap( | ||
url -> repository | ||
.onEmpty( () -> log.error("missing repository for uploading to {}, aborting push", url) ) | ||
.peek( repo -> log.info("pushing {} to {}", repository, url) ) | ||
.flatMap( | ||
repo -> call(repo::directory) | ||
.onEmpty(()-> log.error("missing directory for repository {}, aborting push", repository)) | ||
) | ||
.flatMap( | ||
directory -> Try.of(() -> Git.open(directory)) | ||
.onFailure( | ||
error -> log.error("error while acessing repository {}, aborting push: {}", repository, error) | ||
) | ||
|
||
.mapTry(git -> git.push().call()) | ||
.onFailure( | ||
error -> log.error("error while pushing repo: {}", error) | ||
) | ||
.onSuccess( result -> log.info("git push result: {}", result)) | ||
.toOption() | ||
) | ||
).getOrElse(empty()); | ||
public Iterable<PushResult> get(){ | ||
return repository | ||
.onEmpty(() -> log.error("missing repository for uploading, aborting push")) | ||
.peek(repo -> log.info("pushing {}", repo)) | ||
.flatMap(repoDirectoryFn) | ||
.flatMap(gitOpenFn) | ||
.flatMap(gitPushFn) | ||
.getOrElse(empty()) | ||
; | ||
} | ||
|
||
public static <T extends Repo>Supplier<Iterable<PushResult>> gitSshPush(T repo, String sshUrl){ | ||
return new GitPushUploadStrategy<>(of(sshUrl), of(repo)); | ||
public static <T extends Repo> Supplier<Iterable<PushResult>> GitPushUploadStrategy(T repo){ | ||
return GitPushUploadStrategy.builder() | ||
.repository(Option(repo)) | ||
.repoDirectoryFn(defaultRepoDirectoryFn) | ||
.gitOpenFn(defaultGitOpenFn) | ||
.gitPushFn(defaultGitPushFn) | ||
.build() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters