Skip to content

Commit

Permalink
merge: main
Browse files Browse the repository at this point in the history
  • Loading branch information
anshalshukla committed Jan 29, 2024
2 parents 660c146 + 1f5540c commit 57d291c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thiserror::Error;
use super::prompt::{get_option, get_text_input};
use crate::app::config::{AppChainConfig, ConfigVersion, RollupMode};
use crate::da::da_layers::{DAFactory, DALayer};
use crate::utils::constants::{APP_CONFIG_NAME, MADARA_REPO_NAME, MADARA_REPO_ORG};
use crate::utils::constants::{APP_CONFIG_NAME, MADARA_BRANCH_NAME, MADARA_REPO_NAME, MADARA_REPO_ORG};
use crate::utils::errors::GithubError;
use crate::utils::github::get_latest_commit_hash;
use crate::utils::paths::{get_app_chains_home, get_app_home};
Expand Down Expand Up @@ -53,7 +53,7 @@ async fn generate_config() -> Result<AppChainConfig, InitError> {

let mode = get_option("Select mode for your app chain:", RollupMode::iter().collect::<Vec<_>>())?;
let da_layer = get_option("Select DA layer for your app chain:", DALayer::iter().collect::<Vec<_>>())?;
let madara_version = get_latest_commit_hash(MADARA_REPO_ORG, MADARA_REPO_NAME).await?;
let madara_version = get_latest_commit_hash(MADARA_REPO_ORG, MADARA_REPO_NAME, MADARA_BRANCH_NAME).await?;
let config_version = ConfigVersion::Version1;

log::info!("\n");
Expand Down
2 changes: 1 addition & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn start_app_chain() -> Result<(), RunError> {
}
};

madara::clone_madara_and_build_repo()?;
madara::clone_madara_and_build_repo(&config)?;

let da_factory = DAFactory::new_da(&config.da_layer);
da_factory.confirm_minimum_balance(&config)?;
Expand Down
5 changes: 2 additions & 3 deletions src/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub const MADARA_REPO_ORG: &str = "keep-starknet-strange";
pub const MADARA_REPO_NAME: &str = "madara";

pub const KARNOT_REPO_ORG: &str = "karnotxyz";
pub const MADARA_REPO_ORG: &str = "karnotxyz";

pub const BRANCH_NAME: &str = "custom_events_page";
pub const MADARA_BRANCH_NAME: &str = "cli_branch";

pub const APP_CONFIG_NAME: &str = "config.toml";
pub const APP_DA_CONFIG_NAME: &str = "da-config.json";
Expand Down
32 changes: 23 additions & 9 deletions src/utils/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ struct Commit {
sha: String,
}

pub async fn get_latest_commit_hash(org: &str, repo: &str) -> Result<String, GithubError> {
let github_api_url = format!("{}/repos/{}/{}/commits", GITHUB_API_BASE_URL, org, repo);
pub async fn get_latest_commit_hash(org: &str, repo: &str, branch: &str) -> Result<String, GithubError> {
let github_api_url = format!("{}/repos/{}/{}/commits/{}", GITHUB_API_BASE_URL, org, repo, branch);

let client = Client::new();
let response = client.get(github_api_url).header("User-Agent", "reqwest").send().await;

return match response {
Ok(response) => match response.json::<Vec<Commit>>().await {
Ok(commits) => match commits.first() {
Some(latest_commit) => Ok(latest_commit.sha.clone()),
None => Err(GithubError::NoCommitsFound),
},
match response {
Ok(response) => match response.json::<Commit>().await {
Ok(latest_commit) => Ok(latest_commit.sha.clone()),
Err(err) => Err(GithubError::FailedToGetCommits(err)),
},
Err(err) => Err(GithubError::FailedToGetCommits(err)),
};
}
}

pub fn git_clone(url: &str, path: &PathBuf, branch: Option<&str>) -> Result<(), GithubError> {
Expand All @@ -38,6 +36,22 @@ pub fn git_clone(url: &str, path: &PathBuf, branch: Option<&str>) -> Result<(),
let remote = repo.find_remote("origin")?;
if let Some(remote_url) = remote.url() {
if remote_url == url {
if let Some(branch) = branch {
Command::new("git")
.arg("fetch")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.current_dir(path)
.output()?;
Command::new("git")
.arg("checkout")
.arg(branch)
.current_dir(path)
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()?;
Command::new("git").arg("pull").stdout(Stdio::inherit()).stderr(Stdio::inherit()).output()?;
}
return Ok(());
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/utils/madara.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
use crate::app::config::AppChainConfig;
use crate::da::da_layers::DALayer;
use crate::utils::cmd::execute_cmd;
use crate::utils::constants::{APP_DA_CONFIG_NAME, BRANCH_NAME, KARNOT_REPO_ORG, MADARA_REPO_NAME};
use crate::utils::constants::{APP_DA_CONFIG_NAME, MADARA_REPO_NAME, MADARA_REPO_ORG};
use crate::utils::errors::MadaraError;
use crate::utils::github::git_clone;
use crate::utils::paths::{get_app_home, get_madara_home};

pub const GITHUB_BASE_URL: &str = "https://github.com";

pub fn clone_madara_and_build_repo() -> Result<(), MadaraError> {
let repo_url = format!("{}/{}/{}", GITHUB_BASE_URL, KARNOT_REPO_ORG, MADARA_REPO_NAME);
pub fn clone_madara_and_build_repo(config: &AppChainConfig) -> Result<(), MadaraError> {
let repo_url = format!("{}/{}/{}", GITHUB_BASE_URL, MADARA_REPO_ORG, MADARA_REPO_NAME);
let madara_path = get_madara_home()?.join("madara");

match git_clone(&repo_url, &madara_path, Some(BRANCH_NAME)) {
let madara_version = match config.madara_version.as_str() {
// there was a bug initially where the wrong commit version was being set
// in the config
"523ceedc8afe7a4f58abfdbb915aff3c36e817fe" | "8b49fecfe6f420a1dede1e691d50cd59a326bbc0" => {
"5de416aeb2d9e4297e58f7f2dff99aeae521855e"
}
_ => config.madara_version.as_str(),
};
match git_clone(&repo_url, &madara_path, Some(madara_version)) {
Ok(_) => {
log::info!("Successfully cloned Madara repo");
}
Expand Down

0 comments on commit 57d291c

Please sign in to comment.