Skip to content

Commit

Permalink
refactor: improve code to manage different urls
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexD10S committed Apr 26, 2024
1 parent c430b96 commit 5a515f9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
4 changes: 2 additions & 2 deletions crates/pop-parachains/src/new_parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn instantiate_template_dir(
if matches!(template, &Template::Base) {
return instantiate_base_template(target, config, tag_version);
}
let tag = Git::clone_and_degit(template, target, tag_version)?;
let tag = Git::clone_and_degit(template.repository_url()?, target, tag_version)?;
Ok(tag)
}

Expand All @@ -37,7 +37,7 @@ pub fn instantiate_base_template(
let source = temp_dir.path();
let template = crate::templates::Template::Base;

let tag = Git::clone_and_degit(&template, source, tag_version)?;
let tag = Git::clone_and_degit(template.repository_url()?, source, tag_version)?;

for entry in WalkDir::new(&source) {
let entry = entry?;
Expand Down
33 changes: 11 additions & 22 deletions crates/pop-parachains/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,28 @@ pub enum Template {
serialize = "base",
message = "Standard",
detailed_message = "A standard parachain",
props(Provider = "Pop", Repository = "r0gue-io/base-parachain")
props(Provider = "Pop", Repository = "https://github.com/r0gue-io/base-parachain")
)]
Base,
// Parity
#[strum(
serialize = "cpt",
message = "Contracts",
detailed_message = "Minimal Substrate node configured for smart contracts via pallet-contracts.",
props(Provider = "Parity", Repository = "paritytech/substrate-contracts-node")
props(
Provider = "Parity",
Repository = "https://github.com/paritytech/substrate-contracts-node"
)
)]
ParityContracts,
#[strum(
serialize = "fpt",
message = "EVM",
detailed_message = "Template node for a Frontier (EVM) based parachain.",
props(Provider = "Parity", Repository = "paritytech/frontier-parachain-template")
props(
Provider = "Parity",
Repository = "https://github.com/paritytech/frontier-parachain-template"
)
)]
ParityFPT,
}
Expand All @@ -108,13 +114,8 @@ impl Template {
self.get_str("Provider") == Some(provider.to_string().as_str())
}

pub fn repository_url(&self) -> Result<String, Error> {
Ok(["https://github.com/", self.get_str("Repository").ok_or(Error::RepositoryMissing)?]
.concat())
}
pub fn ssh_repository_url(&self) -> Result<String, Error> {
Ok(["[email protected]:", self.get_str("Repository").ok_or(Error::RepositoryMissing)?, ".git"]
.concat())
pub fn repository_url(&self) -> Result<&str, Error> {
self.get_str("Repository").ok_or(Error::RepositoryMissing)
}
}

Expand Down Expand Up @@ -159,28 +160,16 @@ mod tests {
template.repository_url().unwrap(),
"https://github.com/r0gue-io/base-parachain"
);
assert_eq!(
template.ssh_repository_url().unwrap(),
"[email protected]:r0gue-io/base-parachain.git"
);
template = Template::ParityContracts;
assert_eq!(
template.repository_url().unwrap(),
"https://github.com/paritytech/substrate-contracts-node"
);
assert_eq!(
template.ssh_repository_url().unwrap(),
"[email protected]:paritytech/substrate-contracts-node.git"
);
template = Template::ParityFPT;
assert_eq!(
template.repository_url().unwrap(),
"https://github.com/paritytech/frontier-parachain-template"
);
assert_eq!(
template.ssh_repository_url().unwrap(),
"[email protected]:paritytech/frontier-parachain-template.git"
);
}

#[test]
Expand Down
53 changes: 43 additions & 10 deletions crates/pop-parachains/src/utils/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
use crate::errors::Error;
use crate::Template;
use anyhow::Result;
use git2::{
build::RepoBuilder, FetchOptions, IndexAddOption, RemoteCallbacks, Repository, ResetType,
Expand All @@ -13,8 +12,6 @@ use url::Url;

pub struct Git;
impl Git {
const GIT_SSH: &'static str = "[email protected]:";

pub(crate) fn clone(url: &Url, working_dir: &Path, branch: Option<&str>) -> Result<()> {
if !working_dir.exists() {
let mut fo = FetchOptions::new();
Expand All @@ -31,8 +28,7 @@ impl Git {
Ok(())
}
pub(crate) fn ssh_clone(url: &Url, working_dir: &Path, branch: Option<&str>) -> Result<()> {
// Change the url to the ssh url with [email protected]: prefix, remove / from path and adding .git as suffix
let ssh_url = [Self::GIT_SSH, &url.path()[1..], ".git"].concat();
let ssh_url = GitHub::convert_to_shh_url(url);
if !working_dir.exists() {
// Prepare callback and fetch options.
let mut fo = FetchOptions::new();
Expand All @@ -49,13 +45,16 @@ impl Git {
}
/// Clone `url` into `target` and degit it
pub fn clone_and_degit(
template: &Template,
url: &str,
target: &Path,
tag_version: Option<String>,
) -> Result<Option<String>> {
let repo = match Repository::clone(&template.repository_url()?, target) {
let repo = match Repository::clone(url, target) {
Ok(repo) => repo,
Err(_e) => Self::ssh_clone_and_degit(&template.ssh_repository_url()?, target)?,
Err(_e) => Self::ssh_clone_and_degit(
url::Url::parse(url).map_err(|err| Error::from(err))?,
target,
)?,
};

if let Some(tag_version) = tag_version {
Expand Down Expand Up @@ -84,14 +83,15 @@ impl Git {
}

/// For users that have ssh configuration for cloning repositories
fn ssh_clone_and_degit(url: &str, target: &Path) -> Result<Repository> {
fn ssh_clone_and_degit(url: Url, target: &Path) -> Result<Repository> {
let ssh_url = GitHub::convert_to_shh_url(&url);
// Prepare callback and fetch options.
let mut fo = FetchOptions::new();
Self::set_up_ssh_fetch_options(&mut fo)?;
// Prepare builder and clone.
let mut builder = RepoBuilder::new();
builder.fetch_options(fo);
let repo = builder.clone(url, target)?;
let repo = builder.clone(&ssh_url, target)?;
Ok(repo)
}

Expand Down Expand Up @@ -218,6 +218,9 @@ impl GitHub {
pub(crate) fn release(repo: &Url, tag: &str, artifact: &str) -> String {
format!("{}/releases/download/{tag}/{artifact}", repo.as_str())
}
pub(crate) fn convert_to_shh_url(url: &Url) -> String {
format!("git@{}:{}.git", url.host_str().unwrap_or("github.com"), &url.path()[1..])
}
}

#[derive(serde::Deserialize)]
Expand All @@ -227,3 +230,33 @@ pub struct Release {
pub prerelease: bool,
pub commit: Option<String>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_convert_to_shh_url() {
assert_eq!(
GitHub::convert_to_shh_url(
&Url::parse("https://github.com/r0gue-io/base-parachain")
.expect("valid repository url")
),
"[email protected]:r0gue-io/base-parachain.git"
);
assert_eq!(
GitHub::convert_to_shh_url(
&Url::parse("https://github.com/paritytech/substrate-contracts-node")
.expect("valid repository url")
),
"[email protected]:paritytech/substrate-contracts-node.git"
);
assert_eq!(
GitHub::convert_to_shh_url(
&Url::parse("https://github.com/paritytech/frontier-parachain-template")
.expect("valid repository url")
),
"[email protected]:paritytech/frontier-parachain-template.git"
);
}
}

0 comments on commit 5a515f9

Please sign in to comment.