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

feat: branch support in installation #224

Closed
wants to merge 18 commits into from
Closed
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
5 changes: 4 additions & 1 deletion coffee_cmd/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum CoffeeCommand {
verbose: bool,
#[arg(short, long, action = clap::ArgAction::SetTrue)]
dynamic: bool,
#[arg(short, long)]
branch: Option<String>,
},
/// upgrade a single repository.
#[clap(arg_required_else_help = true)]
Expand Down Expand Up @@ -92,7 +94,8 @@ impl From<&CoffeeCommand> for coffee_core::CoffeeOperation {
plugin,
verbose,
dynamic,
} => Self::Install(plugin.to_owned(), *verbose, *dynamic),
branch,
} => Self::Install(plugin.to_owned(), *verbose, *dynamic, branch.to_owned()),
CoffeeCommand::Upgrade { repo, verbose } => Self::Upgrade(repo.to_owned(), *verbose),
CoffeeCommand::List {} => Self::List,
CoffeeCommand::Setup { cln_conf } => Self::Setup(cln_conf.to_owned()),
Expand Down
3 changes: 2 additions & 1 deletion coffee_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr
plugin,
verbose,
dynamic,
branch,
} => {
let spinner = if !verbose {
Some(term::spinner("Compiling and installing"))
} else {
None
};
let result = coffee.install(&plugin, verbose, dynamic).await;
let result = coffee.install(&plugin, verbose, dynamic, branch).await;
if let Some(spinner) = spinner {
if result.is_ok() {
spinner.finish();
Expand Down
4 changes: 2 additions & 2 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
plugin: &str,
verbose: bool,
try_dynamic: bool,
branch: Option<String>,
) -> Result<(), CoffeeError> {
log::debug!("installing plugin: {plugin}");
// keep track if the plugin is successfully installed
Expand Down Expand Up @@ -358,12 +359,11 @@
UpgradeStatus::Updated(_, _) => {
for plugins in status.plugins_effected.iter() {
self.remove(plugins).await?;
self.install(plugins, verbose, false).await?;
self.install(plugins, verbose, false, branch).await?;

Check failure on line 362 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

cannot find value `branch` in this scope

Check failure on line 362 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

cannot find value `branch` in this scope
}
}
_ => {}
}

self.flush().await?;
Ok(status)
}
Expand Down
4 changes: 2 additions & 2 deletions coffee_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub use coffee_lib as lib;

#[derive(Clone, Debug)]
pub enum CoffeeOperation {
/// Install(plugin name, verbose run, dynamic installation)
Install(String, bool, bool),
/// Install(plugin name, verbose run, dynamic installation, branch)
Install(String, bool, bool, Option<String>),
/// List
List,
// Upgrade(name of the repository, verbose run)
Expand Down
22 changes: 22 additions & 0 deletions coffee_github/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,28 @@ impl Repository for Github {
}
}

async fn switch_branch(&mut self, branch_name: &str) -> Result<(), CoffeeError> {
// FIXME: implement the From git2 Error for `CoffeError`
let repo = git2::Repository::open(&self.url.path_string)
.map_err(|err| error!("{}", err.message()))?;
let mut remote = repo
.find_remote("origin")
.map_err(|err| error!("{}", err.message()))?;
remote
.fetch(&[&branch_name], None, None)
.map_err(|err| error!("{}", err.message()))?;
let oid = repo
.refname_to_id(&format!("refs/remotes/origin/{}", branch_name))
.map_err(|err| error!("{}", err.message()))?;
let obj = repo
.find_object(oid, None)
.map_err(|err| error!("{}", err.message()))?;
repo.reset(&obj, git2::ResetType::Hard, None)
.map_err(|err| error!("{}", err.message()))?;
royalpinto007 marked this conversation as resolved.
Show resolved Hide resolved
self.branch = branch_name.to_string();
Ok(())
}

/// list of the plugin installed inside the repository.
async fn list(&self) -> Result<Vec<Plugin>, CoffeeError> {
Ok(self.plugins.clone())
Expand Down
3 changes: 2 additions & 1 deletion coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ async fn coffee_install(
let try_dynamic = body.try_dynamic;

let mut coffee = data.coffee.lock().await;
let result = coffee.install(plugin, false, try_dynamic).await;
let branch = body.branch;
let result = coffee.install(plugin, false, try_dynamic, branch).await;

handle_httpd_response!(result, "Plugin '{plugin}' installed successfully")
}
Expand Down
1 change: 1 addition & 0 deletions coffee_lib/src/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub trait PluginManager {
plugins: &str,
verbose: bool,
try_dynamic: bool,
branch: Option<String>,
) -> Result<(), CoffeeError>;

// remove a plugin by name, return an error if some error happens.
Expand Down
3 changes: 3 additions & 0 deletions coffee_lib/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub trait Repository: Any {
/// recover the repository from the commit id.
async fn recover(&mut self) -> Result<(), CoffeeError>;

/// switch to the specified branch.
async fn switch_branch(&mut self, branch_name: &str) -> Result<(), CoffeeError>;
Comment on lines +37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// switch to the specified branch.
async fn switch_branch(&mut self, branch_name: &str) -> Result<(), CoffeeError>;

Does not maske sense have the switch_branch inside the repository interface, it can be just an util. For example look at the repository.upgrade code


/// return the name of the repository.
fn name(&self) -> String;

Expand Down
1 change: 1 addition & 0 deletions coffee_lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod request {
pub struct Install {
pub plugin: String,
pub try_dynamic: bool,
pub branch: Option<String>,
}

#[cfg(feature = "open-api")]
Expand Down
2 changes: 1 addition & 1 deletion coffee_plugin/src/plugin/plugin_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn coffee_install(plugin: &mut Plugin<State>, request: Value) -> Result<Value, P
let rt = Runtime::new().unwrap();

let request: InstallReq = serde_json::from_value(request)?;
rt.block_on(coffee.install(&request.name, false, true))
rt.block_on(coffee.install(&request.name, false, true, Some(request.branch)))
.map_err(from)?;
Ok(json!({}))
}
Expand Down
7 changes: 7 additions & 0 deletions docs/docs-book/src/using-coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ To install a plugin statically, you simply need to run.
coffee install <plugin_name>
```

#### Branch Specification for Plugin Installation

User can install a plugin from a specific branch using the command:
```bash
coffee install <plugin_name> --branch <branch_name>
```

### Removing a Plugin

> ✅ Implemented
Expand Down
2 changes: 2 additions & 0 deletions tests/src/coffee_httpd_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub async fn httpd_init_add_remote() {
let install_request = Install {
plugin: "summary".to_string(),
try_dynamic: true,
branch: None,
};

// Send the request to install a plugin
Expand Down Expand Up @@ -107,6 +108,7 @@ pub async fn httpd_add_remove_plugins() {
let install_request = Install {
plugin: "summary".to_string(),
try_dynamic: false,
branch: None,
};

let response = client
Expand Down
20 changes: 10 additions & 10 deletions tests/src/coffee_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub async fn init_coffee_test_add_remote() {
.unwrap();
manager
.coffee()
.install("summary", true, true)
.install("summary", true, true, None)
.await
.unwrap();

Expand Down Expand Up @@ -167,13 +167,13 @@ pub async fn test_add_remove_plugins() {
);

// Install summary plugin
let result = manager.coffee().install("summary", true, false).await;
let result = manager.coffee().install("summary", true, false, None).await;
assert!(result.is_ok(), "{:?}", result);

// Install helpme plugin
manager
.coffee()
.install("helpme", true, false)
.install("helpme", true, false, None)
.await
.unwrap();

Expand Down Expand Up @@ -276,7 +276,7 @@ pub async fn test_errors_and_show() {
);

// Install summary plugin
let result = manager.coffee().install("summary", true, false).await;
let result = manager.coffee().install("summary", true, false, None).await;
assert!(result.is_ok(), "{:?}", result);

// Get the README file for a plugin that is not installed
Expand All @@ -285,7 +285,7 @@ pub async fn test_errors_and_show() {
assert!(val.starts_with("# Helpme plugin"));

// Install a plugin that is not in the repository
let result = manager.coffee().install("x", true, false).await;
let result = manager.coffee().install("x", true, false, None).await;
assert!(result.is_err(), "{:?}", result);

// Remove helpme plugin
Expand Down Expand Up @@ -353,7 +353,7 @@ pub async fn install_plugin_in_two_networks() -> anyhow::Result<()> {
// This should install summary plugin for regtest network
manager
.coffee()
.install("summary", true, true)
.install("summary", true, true, None)
.await
.unwrap();
// Ensure that summary is installed for regtest network
Expand Down Expand Up @@ -393,7 +393,7 @@ pub async fn install_plugin_in_two_networks() -> anyhow::Result<()> {
// This should install summary plugin for testnet network
manager
.coffee()
.install("summary", true, true)
.install("summary", true, true, None)
.await
.unwrap();
// Ensure that summary is installed for testnet network
Expand Down Expand Up @@ -428,13 +428,13 @@ pub async fn test_double_slash() {
.unwrap();

// Install summary plugin
let result = manager.coffee().install("summary", true, false).await;
let result = manager.coffee().install("summary", true, false, None).await;
assert!(result.is_ok(), "{:?}", result);

// Install helpme plugin
manager
.coffee()
.install("helpme", true, false)
.install("helpme", true, false, None)
.await
.unwrap();

Expand Down Expand Up @@ -493,7 +493,7 @@ pub async fn test_plugin_installation_path() {
// Install summary plugin for regtest network
manager
.coffee()
.install("summary", true, false)
.install("summary", true, false, None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to write a test also where you specify the branch.

.await
.unwrap();

Expand Down
Loading