This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit addressed a new way to clone and archive git repositories inside Parodos. The main objective for these tasks is to implement work on top of Move2kube. Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
- Loading branch information
Showing
6 changed files
with
509 additions
and
0 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
99 changes: 99 additions & 0 deletions
99
prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/git/GitArchiveTask.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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.redhat.parodos.tasks.git; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.common.io.Files; | ||
import com.redhat.parodos.workflow.parameter.WorkParameter; | ||
import com.redhat.parodos.workflow.parameter.WorkParameterType; | ||
import com.redhat.parodos.workflow.task.BaseWorkFlowTask; | ||
import com.redhat.parodos.workflows.work.DefaultWorkReport; | ||
import com.redhat.parodos.workflows.work.WorkContext; | ||
import com.redhat.parodos.workflows.work.WorkReport; | ||
import com.redhat.parodos.workflows.work.WorkStatus; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.eclipse.jgit.api.ArchiveCommand; | ||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.archive.ZipFormat; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; | ||
|
||
@Slf4j | ||
@AllArgsConstructor | ||
public class GitArchiveTask extends BaseWorkFlowTask { | ||
|
||
@Override | ||
public @NonNull List<WorkParameter> getWorkFlowTaskParameters() { | ||
return List.of(WorkParameter.builder().key(GitUtils.getGitRepoPath()).type(WorkParameterType.TEXT) | ||
.optional(true).description("path where the git repo is located").build()); | ||
} | ||
|
||
@Override | ||
public WorkReport execute(WorkContext workContext) { | ||
String path = GitUtils.getRepoPath(workContext); | ||
if (Strings.isNullOrEmpty(path)) { | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, | ||
new IllegalArgumentException("The path parameter cannot be null or empty")); | ||
} | ||
Repository repo = null; | ||
try { | ||
repo = getRepo(path); | ||
var archivePath = archive(repo); | ||
workContext.put(GitUtils.getContextArchivePath(), archivePath.toAbsolutePath().toString()); | ||
} | ||
catch (FileNotFoundException | GitAPIException e) { | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, | ||
new Exception("Cannot archive the repository:" + e)); | ||
} | ||
catch (IOException e) { | ||
// This is the catch for Repository clone call or archive, we don't really | ||
// know. | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, | ||
new Exception("No repository at " + path + " Error:" + e.getMessage())); | ||
} | ||
catch (Exception e) { | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, | ||
new Exception("Cannot archive the repository:" + e)); | ||
} | ||
finally { | ||
if (repo != null) { | ||
repo.close(); | ||
} | ||
} | ||
|
||
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext, null); | ||
} | ||
|
||
private Repository getRepo(String path) throws IOException { | ||
Path gitDir = Paths.get(path); | ||
return new FileRepositoryBuilder().setGitDir(gitDir.toFile()).build(); | ||
} | ||
|
||
private Path archive(Repository repo) throws FileNotFoundException, IOException, GitAPIException { | ||
|
||
// Create a Git instance | ||
Git git = new Git(repo); | ||
ArchiveCommand.registerFormat("zip", new ZipFormat()); | ||
|
||
// Create a ZipFormat instance | ||
String tmpdir = Files.createTempDir().getAbsolutePath(); | ||
Path zipFile = Paths.get(tmpdir + "/output.zip"); | ||
|
||
try (FileOutputStream out = new FileOutputStream(zipFile.toAbsolutePath().toString())) { | ||
git.archive().setTree(repo.resolve("HEAD")).setFormat("zip").setOutputStream(out).call(); | ||
} | ||
finally { | ||
git.close(); | ||
} | ||
return zipFile; | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/git/GitCloneTask.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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.redhat.parodos.tasks.git; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
import com.redhat.parodos.workflow.context.WorkContextDelegate; | ||
import com.redhat.parodos.workflow.exception.MissingParameterException; | ||
import com.redhat.parodos.workflow.parameter.WorkParameter; | ||
import com.redhat.parodos.workflow.parameter.WorkParameterType; | ||
import com.redhat.parodos.workflow.task.BaseWorkFlowTask; | ||
import com.redhat.parodos.workflows.work.DefaultWorkReport; | ||
import com.redhat.parodos.workflows.work.WorkContext; | ||
import com.redhat.parodos.workflows.work.WorkReport; | ||
import com.redhat.parodos.workflows.work.WorkStatus; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NonNull; | ||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.api.errors.InvalidRemoteException; | ||
import org.eclipse.jgit.api.errors.TransportException; | ||
|
||
@AllArgsConstructor | ||
public class GitCloneTask extends BaseWorkFlowTask { | ||
|
||
@Override | ||
public @NonNull List<WorkParameter> getWorkFlowTaskParameters() { | ||
return List.of( | ||
WorkParameter.builder().key(GitUtils.getUri()).type(WorkParameterType.TEXT).optional(false) | ||
.description("Url to clone from").build(), | ||
WorkParameter.builder().key(GitUtils.getBranch()).type(WorkParameterType.TEXT).optional(true) | ||
.description("Branch to clone from, default main").build(), | ||
WorkParameter.builder().key("credentials").type(WorkParameterType.TEXT).optional(false) | ||
.description("Git credential").build()); | ||
} | ||
|
||
@Override | ||
public WorkReport execute(WorkContext workContext) { | ||
String gitUri = null; | ||
String destination = null; | ||
String gitBranch = null; | ||
|
||
try { | ||
gitUri = WorkContextDelegate.getRequiredValueFromRequestParams(workContext, GitUtils.getUri()); | ||
gitBranch = WorkContextDelegate.getOptionalValueFromRequestParams(workContext, GitUtils.getBranch(), | ||
"main"); | ||
destination = cloneRepo(gitUri, gitBranch); | ||
} | ||
catch (MissingParameterException e) { | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, e); | ||
} | ||
catch (TransportException e) { | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, | ||
new Exception("cannot connect to the repository server")); | ||
} | ||
catch (InvalidRemoteException e) { | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, | ||
new Exception("Remote repository " + gitUri + " is not available")); | ||
} | ||
catch (IOException | GitAPIException e) { | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, | ||
new Exception("cannot clone repository, error: " + e.getMessage())); | ||
} | ||
|
||
workContext.put(GitUtils.getContextUri(), gitUri); | ||
workContext.put(GitUtils.getContextDestination(), destination); | ||
workContext.put(GitUtils.getContextBranch(), gitBranch); | ||
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext, null); | ||
} | ||
|
||
private String cloneRepo(String gitUri, String gitBranch) | ||
throws InvalidRemoteException, TransportException, IOException, GitAPIException { | ||
Path tmpPath = (Path) Paths.get("/tmp/"); | ||
String tmpDir = Files.createTempDirectory(tmpPath, "GitTaskClone").toString(); | ||
Git.cloneRepository().setURI(gitUri).setBranch("refs/heads/" + gitBranch).setDirectory(new File(tmpDir)).call(); | ||
return tmpDir; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/git/GitUtils.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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.redhat.parodos.tasks.git; | ||
|
||
import com.redhat.parodos.workflow.context.WorkContextDelegate; | ||
import com.redhat.parodos.workflows.work.WorkContext; | ||
import lombok.Getter; | ||
|
||
public class GitUtils { | ||
|
||
private GitUtils() { | ||
return; | ||
} | ||
|
||
@Getter | ||
static final String gitRepoPath = "path"; | ||
|
||
@Getter | ||
static final String uri = "uri"; | ||
|
||
@Getter | ||
static final String branch = "branch"; | ||
|
||
@Getter | ||
static final String ContextUri = "gitUri"; | ||
|
||
@Getter | ||
static final String ContextBranch = "gitBranch"; | ||
|
||
@Getter | ||
static final String contextDestination = "gitDestination"; | ||
|
||
@Getter | ||
static final String contextArchivePath = "gitArchivePath"; | ||
|
||
public static String getRepoPath(WorkContext workContext) { | ||
var dest = workContext.get(contextDestination); | ||
if (dest == null) { | ||
return WorkContextDelegate.getOptionalValueFromRequestParams(workContext, getGitRepoPath(), ""); | ||
} | ||
return WorkContextDelegate.getOptionalValueFromRequestParams(workContext, getGitRepoPath(), dest.toString()); | ||
} | ||
|
||
} |
Oops, something went wrong.