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

fix: clone when user use ssh #113

Merged
merged 14 commits into from
Apr 22, 2024
33 changes: 33 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ serde_json = { version = "1.0"}
serde = { version = "1.0", features = ["derive"] }
zombienet-sdk = { git = "https://github.com/r0gue-io/zombienet-sdk", branch = "pop", version = "0.1.0-alpha.1"}
zombienet-support = { git = "https://github.com/r0gue-io/zombienet-sdk", branch = "pop", version = "0.1.0-alpha.1" }
git2_credentials = "0.13.0"
weezy20 marked this conversation as resolved.
Show resolved Hide resolved

# pop-cli
clap = { version = "4.4", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions crates/pop-parachains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2021"
anyhow.workspace = true
duct.workspace = true
git2.workspace = true
git2_credentials.workspace = true
tempfile.workspace = true
url.workspace = true
tokio.workspace = true
Expand Down
16 changes: 4 additions & 12 deletions crates/pop-parachains/src/new_parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
},
};
use anyhow::Result;
use git2::Repository;
use std::{fs, path::Path};
use walkdir::WalkDir;

Expand All @@ -29,23 +28,17 @@ pub fn instantiate_template_dir(
config: Config,
) -> Result<Option<String>> {
sanitize(target)?;
use Template::*;
let url = match template {
FPT => "https://github.com/paritytech/frontier-parachain-template.git",
Contracts => "https://github.com/paritytech/substrate-contracts-node.git",
Base => {
return instantiate_base_template(target, config);
},
if matches!(template, Template::Base) {
return instantiate_base_template(target, config);
};
let tag = Git::clone_and_degit(url, target)?;
Repository::init(target)?;
let tag = Git::clone_and_degit(template, target)?;
Ok(tag)
}

pub fn instantiate_base_template(target: &Path, config: Config) -> Result<Option<String>> {
let temp_dir = ::tempfile::TempDir::new_in(std::env::temp_dir())?;
let source = temp_dir.path();
let tag = Git::clone_and_degit("https://github.com/r0gue-io/base-parachain", source)?;
let tag = Git::clone_and_degit(&crate::Template::Base, source)?;

for entry in WalkDir::new(&source) {
let entry = entry?;
Expand All @@ -72,7 +65,6 @@ pub fn instantiate_base_template(target: &Path, config: Config) -> Result<Option
// Add network configuration
let network = Network { node: "parachain-template-node".into() };
write_to_file(&target.join("network.toml"), network.render().expect("infallible").as_ref())?;
Repository::init(target)?;
Ok(tag)
}

Expand Down
42 changes: 38 additions & 4 deletions crates/pop-parachains/src/utils/git.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use anyhow::{anyhow, Result};
use git2::{build::RepoBuilder, FetchOptions, IndexAddOption, Repository, ResetType};
use git2::{
build::RepoBuilder, FetchOptions, IndexAddOption, RemoteCallbacks, Repository, ResetType,
};
use git2_credentials::CredentialHandler;
use regex::Regex;
use std::fs;
use std::path::Path;
use std::{env, fs};
use url::Url;

use crate::Template;

pub struct Git;
impl Git {
pub(crate) fn clone(url: &Url, working_dir: &Path, branch: Option<&str>) -> Result<()> {
Expand All @@ -21,8 +26,13 @@ impl Git {
Ok(())
}
/// Clone `url` into `target` and degit it
pub fn clone_and_degit(url: &str, target: &Path) -> Result<Option<String>> {
let repo = Repository::clone(url, target)?;
pub fn clone_and_degit(template: &Template, target: &Path) -> Result<Option<String>> {
let url = match template {
Template::FPT => "https://github.com/paritytech/frontier-parachain-template.git",
Template::Contracts => "https://github.com/paritytech/substrate-contracts-node.git",
Template::Base => "https://github.com/r0gue-io/base-parachain",
};
let repo = Repository::clone(url, target).unwrap_or(Self::ssh_clone(template, target)?);

// fetch tags from remote
let release = Self::fetch_latest_tag(&repo);
Expand All @@ -32,6 +42,30 @@ impl Git {
Ok(release)
}

/// For users that have ssh configuration for cloning repositories
fn ssh_clone(template: &Template, target: &Path) -> Result<Repository> {
let ssh_url = match template {
Template::FPT => "[email protected]:paritytech/frontier-parachain-template.git",
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
Template::Contracts => "[email protected]:paritytech/substrate-contracts-node.git",
Template::Base => "[email protected]:r0gue-io/base-parachain.git",
};
// Prepare callback and fetch options.
let mut callbacks = RemoteCallbacks::new();
let git_config = git2::Config::open_default().unwrap();
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
let mut ch = CredentialHandler::new(git_config);
callbacks.credentials(move |url, username, allowed| {
ch.try_next_credential(url, username, allowed)
});
let mut fo = git2::FetchOptions::new();
fo.remote_callbacks(callbacks);

// Prepare builder and clone.
let mut builder = git2::build::RepoBuilder::new();
builder.fetch_options(fo);
let repo = builder.clone(ssh_url, target)?;
Ok(repo)
}

/// Fetch the latest release from a repository
fn fetch_latest_tag(repo: &Repository) -> Option<String> {
let version_reg = Regex::new(r"v\d+\.\d+\.\d+").expect("Valid regex");
Expand Down
Loading