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

automatically pull and update branch #25

Merged
merged 2 commits into from
Jan 29, 2024
Merged
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
1 change: 1 addition & 0 deletions src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ pub enum RollupMode {
#[derive(Debug, Serialize, Deserialize)]
pub enum ConfigVersion {
Version1,
Version2,
}
6 changes: 3 additions & 3 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,8 +53,8 @@ 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 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");

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
20 changes: 11 additions & 9 deletions src/utils/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,22 +15,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 @@ -39,6 +37,10 @@ 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 {
execute_cmd("git", &["fetch"], path)?;
execute_cmd_stdio("git", &["checkout", branch], path, Stdio::null(), Stdio::null())?;
}
return Ok(());
}
}
Expand Down
18 changes: 13 additions & 5 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::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");
}
Expand Down
Loading