Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/main/java/bio/terra/cli/command/resource/addref/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import bio.terra.cli.command.shared.options.Format;
import bio.terra.cli.command.shared.options.ReferencedResourceCreation;
import bio.terra.cli.command.shared.options.WorkspaceOverride;
import bio.terra.cli.exception.SystemException;
import bio.terra.cli.exception.UserActionableException;
import bio.terra.cli.serialization.userfacing.input.AddGitRepoParams;
import bio.terra.cli.serialization.userfacing.input.CreateResourceParams;
import bio.terra.cli.serialization.userfacing.resource.UFGitRepo;
import bio.terra.cli.service.ExternalCredentialsManagerService;
import bio.terra.externalcreds.model.SshKeyPairType;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpStatusCodeException;
import picocli.CommandLine;

/** This class corresponds to the fourth-level "terra resource add-ref git-repo" command. */
Expand Down Expand Up @@ -46,5 +52,17 @@ protected void execute() {
bio.terra.cli.businessobject.resource.GitRepo addedResource =
bio.terra.cli.businessobject.resource.GitRepo.addReferenced(createParams.build());
formatOption.printReturnValue(new UFGitRepo(addedResource), GitRepo::printText);
ExternalCredentialsManagerService ecmService = ExternalCredentialsManagerService.fromContext();
try {
ecmService.getSshKeyPair(SshKeyPairType.GITHUB);
} catch (SystemException e) {
if (e.getCause() instanceof HttpStatusCodeException
&& ((HttpStatusCodeException) e.getCause()).getStatusCode() == HttpStatus.NOT_FOUND) {
throw new UserActionableException(
"You do not have a Terra ssh key, cloning the git repo in the GCP notebook will"
+ " fail. Please run `terra user ssh-key generate` and store the output (public key) in"
+ " your GitHub account https://github.com/settings/keys.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ protected void execute() {
confirmationPrompt.confirmOrThrow(
"Generating a new Terra SSH key will replace the old Terra SSH key if it exists. "
+ "You must associate the new SSH public key with your GitHub account using "
+ "https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent. "
+ "Are you sure you want to proceed (y/N)?",
+ "https://github.com/settings/keys. Are you sure you want to proceed (y/N)?",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought it's more straightforward to just go to https://github.com/settings/keys to add your key? step 1 and 2 should be automatically covered by the notebook startup script.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, is adding a key to https://github.com/settings/keys the same as ssh-add -K ~/.ssh/id_ed25519 ? If so, SG, thanks

"Generating new SSH key is aborted");
var ecmService = ExternalCredentialsManagerService.fromContext();
var sshKeyPair = ecmService.generateSshKeyPair(SshKeyPairType.GITHUB);
Expand Down
27 changes: 9 additions & 18 deletions src/test/java/unit/GitRepoReferenced.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ static List<UFGitRepo> listGitRepoResourcesWithName(String resourceName)
void listDescribeReflectAdd() throws IOException {
workspaceCreator.login();

// Generate ssh key to avoid prompt about ssh key
TestCommand.runCommandExpectSuccess("user", "ssh-key", "generate", "--quiet");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Generate ssh key to avoid prompt about ssh key


// `terra workspace set --id=$id`
TestCommand.runCommandExpectSuccess("workspace", "set", "--id=" + getUserFacingId());

Expand Down Expand Up @@ -103,13 +106,8 @@ void resolve() throws IOException {

// `terra resource add-ref git-repo --name=$name --repo-url=$repoUrl
String name = "resolve";
TestCommand.runAndParseCommandExpectSuccess(
UFGitRepo.class,
"resource",
"add-ref",
"git-repo",
"--name=" + name,
"--repo-url=" + GIT_REPO_SSH_URL);
TestCommand.runCommandExpectSuccess(
"resource", "add-ref", "git-repo", "--name=" + name, "--repo-url=" + GIT_REPO_SSH_URL);

// `terra resource resolve --name=$name --format=json`
JSONObject resolved =
Expand All @@ -130,13 +128,8 @@ void listReflectsDelete() throws IOException {

// `terra resource add-ref git-repo --name=$name --repo-url=$repoUrl
String name = "listReflectsDelete";
TestCommand.runAndParseCommandExpectSuccess(
UFGitRepo.class,
"resource",
"add-ref",
"git-repo",
"--name=" + name,
"--repo-url=" + GIT_REPO_SSH_URL);
TestCommand.runCommandExpectSuccess(
"resource", "add-ref", "git-repo", "--name=" + name, "--repo-url=" + GIT_REPO_SSH_URL);

// `terra resource delete --name=$name --format=json`
TestCommand.runCommandExpectSuccess("resource", "delete", "--name=" + name, "--quiet");
Expand Down Expand Up @@ -206,8 +199,7 @@ void updateIndividualProperties() throws IOException {
// `terra resource add-ref git-repo --name=$name --repo-url=$repoUrl --description=$description`
String name = "updateIndividualProperties";
String description = "updateDescription";
TestCommand.runAndParseCommandExpectSuccess(
UFGitRepo.class,
TestCommand.runCommandExpectSuccess(
"resource",
"add-ref",
"git-repo",
Expand Down Expand Up @@ -286,8 +278,7 @@ void updateMultipleOrNoProperties() throws IOException {
// --repo-url=$gitUrl
String name = "updateMultipleOrNoProperties";
String description = "updateDescription";
TestCommand.runAndParseCommandExpectSuccess(
UFGitRepo.class,
TestCommand.runCommandExpectSuccess(
"resource",
"add-ref",
"git-repo",
Expand Down