Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/openai #114

Merged
merged 7 commits into from
Dec 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added intellij plugin project
shannah committed Dec 29, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 86fb12dac19c19a020739bc437bd62c315690392
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cli/pom.xml
Original file line number Diff line number Diff line change
@@ -213,6 +213,19 @@
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.purejava/tweetnacl-java -->
<dependency>
<groupId>org.purejava</groupId>
<artifactId>tweetnacl-java</artifactId>
<version>1.1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.github.alphazero/Blake2b -->
<dependency>
<groupId>com.github.alphazero</groupId>
<artifactId>Blake2b</artifactId>
<version>bbf094983c</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
@@ -275,6 +288,10 @@
</snapshots>
<url>file://${basedir}/../maven-repository</url>
</repository>
<repository>
<id>sci-java</id>
<url>https://maven.scijava.org/content/repositories/public</url>
</repository>

</repositories>

Original file line number Diff line number Diff line change
@@ -16,6 +16,9 @@ public class ProjectGeneratorRequestBuilder implements ProjectGeneratorRequest.P
@CommandLineParser.Help("The github repository to use. E.g. \"username/repo\". If not specified, will be inferred from the package name and project name.")
private String githubRepository;

@CommandLineParser.Help("Whether the repository should be private")
private boolean privateRepository;

@CommandLineParser.PositionalArg(1)
@CommandLineParser.Help("The fully-qualified main class name. If not specified, will be inferred from the package name and project name.")
private String magicArg;
@@ -376,6 +379,13 @@ public ProjectGeneratorRequest.Params setGithubRepository(String githubRepositor
return this;
}

@Override
public ProjectGeneratorRequest.Params setPrivateRepository(boolean privateRepository) {
this.privateRepository = privateRepository;

return this;
}

public String getGithubRepository() {
if (githubRepository != null) {
return githubRepository;
@@ -386,6 +396,10 @@ public String getGithubRepository() {
return null;
}

public boolean isPrivateRepository() {
return privateRepository;
}

private String getGithubUser() {
String repo = getGithubRepository();
if (repo != null) {
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@ public class ProjectGeneratorRequest {

private final String githubRepository;

private final boolean privateRepository;

public File getParentDirectory() {
return parentDirectory;
}
@@ -65,11 +67,19 @@ public String getGithubRepository() {
return githubRepository;
}

public boolean isPrivateRepository() {
return privateRepository;
}

public interface Params {

@CommandLineParser.Help("The github repository to use. E.g. \"username/repo\". If not specified, will be inferred from the package name and project name.")
String getGithubRepository();

@CommandLineParser.Help("Whether the repository should be private")
@CommandLineParser.Alias("p")
boolean isPrivateRepository();

@CommandLineParser.PositionalArg(1)
@CommandLineParser.Help("The fully-qualified main class name. If not specified, will be inferred from the package name and project name.")
String getMagicArg();
@@ -139,6 +149,8 @@ public interface Params {

Params setGithubRepository(String githubRepository);

Params setPrivateRepository(boolean privateRepository);

Params setExtensions(String[] extensions);

Params setWithCheerpj(boolean withCheerpj);
@@ -157,6 +169,7 @@ public ProjectGeneratorRequest(Params params) {
this.packageName = params.getPackageName();
this.extensions = params.getExtensions();
this.githubRepository = params.getGithubRepository();
this.privateRepository = params.isPrivateRepository();
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ca.weblite.jdeploy.services;

import ca.weblite.jdeploy.cli.util.CommandLineParser;
import ca.weblite.jdeploy.dtos.GitHubRepositoryIntializationRequest;
import ca.weblite.jdeploy.dtos.GithubRepositoryDto;
import ca.weblite.jdeploy.factories.GitHubRepositoryDtoFactory;
@@ -107,14 +106,13 @@ public void createGitHubRepository(GitHubRepositoryIntializationRequest request)
}
}

private void setupAndPushRemote(GitHubRepositoryIntializationRequest request)
public void setupAndPushRemote(GitHubRepositoryIntializationRequest request)
throws GitAPIException, URISyntaxException, IOException {
JDeployProject project = jDeployProjectCache.findByPath(request.getProjectPath());
GithubRepositoryDto githubRepositoryDto = githubRepositoryDtoFactory.newGithubRepository(
request.getRepoName(),
request.isPrivate()
);
File localPath = project.getPackageJSONFile().toFile().getParentFile();
File localPath = getLocalPath(request);
Git git;
try (Repository repository = Git.init().setDirectory(localPath).call().getRepository()) {

@@ -170,4 +168,13 @@ private void setupAndPushRemote(GitHubRepositoryIntializationRequest request)
git.close();

}

private File getLocalPath(GitHubRepositoryIntializationRequest request) {
try {
JDeployProject project = jDeployProjectCache.findByPath(request.getProjectPath());
return project.getPackageJSONFile().toFile().getParentFile();
} catch (IOException e) {
return new File(request.getProjectPath());
}
}
}
131 changes: 131 additions & 0 deletions cli/src/main/java/ca/weblite/jdeploy/services/GithubSecretSetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package ca.weblite.jdeploy.services;
import ca.weblite.tools.io.IOUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.json.JSONObject;

import javax.crypto.Cipher;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

@Singleton
public class GithubSecretSetter {

private static final String GITHUB_API_URL = "https://api.github.com";

private final SealedBoxUtility sealedBoxUtility;

@Inject
public GithubSecretSetter(SealedBoxUtility sealedBoxUtility) {
this.sealedBoxUtility = sealedBoxUtility;
}


public void setSecret(String owner, String repo, String token, String secretName, String secretValue) throws Exception {
// Step 1: Get the public key
PublicKeyInfo publicKey = getPublicKey(owner, repo, token);

// Step 2: Encrypt the secret (This is a placeholder, implement encryption with libsodium)
String encryptedSecret = encryptSecret(secretValue, publicKey);

// Step 3: Create or update the secret
createOrUpdateSecret(owner, repo, token, secretName, encryptedSecret, publicKey);
}

private PublicKeyInfo getPublicKey(String owner, String repo, String token) throws Exception {
URL url = new URL(GITHUB_API_URL + "/repos/" + owner + "/" + repo + "/actions/secrets/public-key");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "token " + token);
con.setRequestProperty("Accept", "application/vnd.github.v3+json");
int responseCode = con.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed to get public key: GitHub API responded with code " + responseCode);
}

// Read the response
StringBuilder response = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
}

JSONObject jsonResponse = new JSONObject(response.toString());
String publicKey = jsonResponse.getString("key");
String keyId = jsonResponse.getString("key_id");
return new PublicKeyInfo(publicKey, keyId);
}


private String encryptSecret(String secret, PublicKeyInfo publicKeyInfo) throws Exception {
// Decode the public key
byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyInfo.getKey());

return Base64.getEncoder().encodeToString(
sealedBoxUtility.crypto_box_seal(secret.getBytes(), publicKeyBytes)
);
}


private void createOrUpdateSecret(
String owner,
String repo,
String token,
String secretName,
String encryptedSecret,
PublicKeyInfo publicKeyInfo
) throws Exception {
URL url = new URL(GITHUB_API_URL + "/repos/" + owner + "/" + repo + "/actions/secrets/" + secretName);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("PUT");
con.setRequestProperty("Authorization", "token " + token);
con.setRequestProperty("Accept", "application/vnd.github.v3+json");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);

String jsonPayload = "{\"encrypted_value\":\"" + encryptedSecret + "\", \"key_id\":\"" + publicKeyInfo.getKeyId() + "\"}";
try (OutputStream os = con.getOutputStream()) {
byte[] input = jsonPayload.getBytes("utf-8");
os.write(input, 0, input.length);
}

// Handle the response (omitted for brevity)
int responseCode = con.getResponseCode();
if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
// Read error message from response (if any)
String errorMessage = IOUtil.readToString(con.getErrorStream());
throw new RuntimeException("Failed to set secret: HTTP error code: " + responseCode + " - " + errorMessage);
}
}

private static class PublicKeyInfo {
private final String key;
private final String keyId;

public PublicKeyInfo(String key, String keyId) {
this.key = key;
this.keyId = keyId;
}

// Getters
public String getKey() {
return key;
}

public String getKeyId() {
return keyId;
}
}
}

Loading