From 5fcfadb93a29666be5cfc535afbe51746e1a4e0b Mon Sep 17 00:00:00 2001 From: apoorvsadana <95699312+apoorvsadana@users.noreply.github.com> Date: Sun, 28 Jan 2024 19:48:22 +0530 Subject: [PATCH] automatically pull and update branch --- src/app/config.rs | 1 + src/cli/init.rs | 6 +++--- src/cli/run.rs | 2 +- src/utils/constants.rs | 5 ++--- src/utils/github.rs | 22 +++++++++++++--------- src/utils/madara.rs | 18 +++++++++++++----- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/app/config.rs b/src/app/config.rs index c3b48ea..6519a3e 100644 --- a/src/app/config.rs +++ b/src/app/config.rs @@ -34,4 +34,5 @@ pub enum RollupMode { #[derive(Debug, Serialize, Deserialize)] pub enum ConfigVersion { Version1, + Version2, } diff --git a/src/cli/init.rs b/src/cli/init.rs index 653741f..fe24019 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -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}; @@ -53,8 +53,8 @@ async fn generate_config() -> Result { let mode = get_option("Select mode for your app chain:", RollupMode::iter().collect::>())?; let da_layer = get_option("Select DA layer for your app chain:", DALayer::iter().collect::>())?; - let madara_version = get_latest_commit_hash(MADARA_REPO_ORG, MADARA_REPO_NAME).await?; - let config_version = ConfigVersion::Version1; + let madara_version = get_latest_commit_hash(MADARA_REPO_ORG, MADARA_REPO_NAME, MADARA_BRANCH_NAME).await?; + let config_version = ConfigVersion::Version2; log::info!("\n"); diff --git a/src/cli/run.rs b/src/cli/run.rs index 1d8a077..4afb895 100644 --- a/src/cli/run.rs +++ b/src/cli/run.rs @@ -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)?; diff --git a/src/utils/constants.rs b/src/utils/constants.rs index f3277ce..86eafd9 100644 --- a/src/utils/constants.rs +++ b/src/utils/constants.rs @@ -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"; diff --git a/src/utils/github.rs b/src/utils/github.rs index cf32363..9d48032 100644 --- a/src/utils/github.rs +++ b/src/utils/github.rs @@ -2,6 +2,7 @@ use std::fs; use std::path::PathBuf; use std::process::{Command, Stdio}; +use crate::utils::cmd::{execute_cmd, execute_cmd_stdio}; use git2::Repository; use reqwest::Client; use serde::Deserialize; @@ -14,22 +15,19 @@ struct Commit { sha: String, } -pub async fn get_latest_commit_hash(org: &str, repo: &str) -> Result { - 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 { + 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::>().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::().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> { @@ -39,6 +37,12 @@ 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 { + log::info!("fetching"); + execute_cmd("git", &["fetch"], path)?; + log::info!("checking out"); + execute_cmd_stdio("git", &["checkout", branch], path, Stdio::null(), Stdio::null())?; + } return Ok(()); } } diff --git a/src/utils/madara.rs b/src/utils/madara.rs index 8e28bb0..32d3304 100644 --- a/src/utils/madara.rs +++ b/src/utils/madara.rs @@ -1,18 +1,26 @@ -use crate::app::config::AppChainConfig; +use crate::app::config::{AppChainConfig, ConfigVersion}; 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)) { + // there was a bug in Version1 where the incorrect commit was + // going inside the toml file, so override with the correct commit + // in that case + let checkout_commit = match config.config_version { + ConfigVersion::Version2 => config.madara_version.as_str(), + ConfigVersion::Version1 => "5de416aeb2d9e4297e58f7f2dff99aeae521855e", + }; + + match git_clone(&repo_url, &madara_path, Some(checkout_commit)) { Ok(_) => { log::info!("Successfully cloned Madara repo"); }