Skip to content

Commit

Permalink
Merge pull request #217 from AdaptiveScale/feature/git_integration
Browse files Browse the repository at this point in the history
added: rosetta apply command can now be setup to execute git command(add, commit and push)
  • Loading branch information
nbesimi authored May 8, 2024
2 parents 52cc929 + 2feaedd commit 69e730d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
55 changes: 52 additions & 3 deletions cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@
import org.apache.commons.io.FilenameUtils;
import picocli.CommandLine;
import queryhelper.pojo.GenericResponse;
import queryhelper.pojo.QueryRequest;
import queryhelper.service.AIService;

import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -236,6 +234,10 @@ private void apply(@CommandLine.Option(names = {"-s", "--source"}, required = tr
DDLExecutor executor = DDLFactory.executor(source, new DriverManagerDriverProvider());
executor.execute(ddl);

if (config.isAutoCommit()) {
gitCommandExecutor(sourceWorkspace.toString());
}

log.info("Successfully written ddl ({}).", stringOutput.getFilePath());
}

Expand Down Expand Up @@ -521,6 +523,53 @@ public FileNameAndDatabasePair(String key, Database value) {
}
}

public void gitCommandExecutor(String sourceWorkspace) {
File workspaceDir = new File(sourceWorkspace);
File[] files = workspaceDir.listFiles((dir, name) -> name.endsWith(".yaml"));
String commitMessage = "added: model yaml files";

if (files == null || files.length == 0) {
System.out.println("No YAML files found in the directory: " + sourceWorkspace);
return;
}

Arrays.stream(files)
.forEach(file -> executeGitCommand(new String[]{"git", "add", file.getAbsolutePath()}));

executeGitCommand(new String[]{"git", "commit", "-m", commitMessage});

if (config.getGitRemoteName() != "origin"){
executeGitCommand(new String[]{"git", "push", config.getGitRemoteName(), "-u", "@"});
return;
}
executeGitCommand(new String[]{"git", "push", "origin", "-u", "@"});

}

public static void executeGitCommand(String[] command) {
try {
Process process = new ProcessBuilder(command).start();

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

int exitCode = process.waitFor();

if (exitCode == 0) {
System.out.println("Git Commands have been executed successfully.");
System.out.println("The YAML file has been pushed to your git branch ! ");
return;
}
System.err.println("Error executing command: " + String.join(" ", command));

} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}


@CommandLine.Command(name = "query", description = "Query schema", mixinStandardHelpOptions = true)
private void query(@CommandLine.Option(names = {"-s", "--source"}, required = true) String sourceName,
Expand Down
13 changes: 13 additions & 0 deletions cli/src/main/java/com/adaptivescale/rosetta/cli/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class Config {
@JsonProperty("openai_model")
private String openAIModel;

@JsonProperty("git_auto_commit")
private boolean gitAutoCommit = false;
@JsonProperty("git_remote_name")
private String gitRemoteName = "origin";

public List<Connection> getConnections() {
return connections;
}
Expand All @@ -31,4 +36,12 @@ public String getOpenAIApiKey() {
return openAIApiKey;
}
public String getOpenAIModel() { return openAIModel; }

public String getGitRemoteName() {
return gitRemoteName;
}
public boolean isAutoCommit() {
return gitAutoCommit;
}

}

0 comments on commit 69e730d

Please sign in to comment.