git2rs
equivalent of ssh credentials or remote_callbacks
#2179
-
I'm trying to migrate my push-to-deploy platfrom away from git2-rs for easier Rust build system integration but struggle with finding the example of setting up ssh key credentials. This is my working git2-rs implementation: let mut push_opts = PushOptions::new();
let mut callbacks = RemoteCallbacks::new();
// For updating status to failed
let deployment_failed_flag = Arc::new(AtomicBool::new(false));
let update_env = env; // Env is Copy
let update_access_token = access_token.clone();
let update_project_id = config.project.id;
let update_deployment_id = created_deployment.id;
// Set the credentials
callbacks.credentials(config.credentials(user));
callbacks.sideband_progress(|data| {
// Convert bytes to string, print line by line
if let Ok(text) = std::str::from_utf8(data) {
for line in text.lines() {
if line.contains(&build_next_app()) {
println!("Building the app {}", succeed_symbol());
}
if line.contains(&start_server(repository)) {
println!("App restart {}", succeed_symbol());
}
}
}
true // continue receiving.
});
callbacks.push_update_reference({
let flag_clone = deployment_failed_flag.clone();
let access_token_for_update_cb = update_access_token.clone();
let project_id_for_update_cb = update_project_id;
let deployment_id_for_update_cb = update_deployment_id;
move |_refname, status_message| {
if let Some(e) = status_message {
// Try to set the flag. If it was already true, do nothing.
if !flag_clone.swap(true, Ordering::SeqCst) {
println!(
"Deployment ref update failed: {}. Marking deployment as Failed.",
e
);
let update_payload = DeploymentPayload {
commit_hash: commit_hash.to_string(),
status: DeploymentStatus::Failed,
};
// We are in a sync callback, so we need to block on the async task.
let handle = tokio::runtime::Handle::current();
let result = handle.block_on(async {
update(
update_env, // Env is Copy
access_token_for_update_cb.clone(),
project_id_for_update_cb,
deployment_id_for_update_cb,
update_payload,
)
.await
});
match result {
Ok(_) => println!("Deployment status successfully updated to Failed."),
Err(update_err) => {
eprintln!("Error updating deployment status to Failed: {}", update_err)
}
}
}
}
Ok(()) // Report success for the git callback itself, error is handled above.
}
});
push_opts.remote_callbacks(callbacks);
let spinner = Spinner::new(
spinners::Spinners::Hamburger,
succeed_message("Deploying > "),
); Any pointer? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Found this Action but there's no SSH option: pub(crate) fn clone_private(repo_url: &str, project_path: &Path) -> Result<Repository, GitError> {
let mut prepare_fetch = match prepare_clone(repo_url, project_path) {
Ok(prepare_fetch) => prepare_fetch,
Err(err) => {
error!("Failed to prepare fetch: {}", err);
return Err(GitError {
message: format!("Failed to prepare fetch: {}", err),
});
}
};
&prepare_fetch.configure_connection(|config| {
// config.configured_credentials(url)
//
config.with_credentials(helper)
config.set_credentials(|x| match x {
Action::Get(ctx) => {
// Implement logic to get credentials
ctx.
}
_ => panic!(),
});
panic!()
});
let (mut prepare_checkout, _) =
match prepare_fetch.fetch_then_checkout(Discard, &IS_INTERRUPTED) {
Ok(x) => x,
Err(err) => {
return Err(GitError {
message: format!("Failed to fetch and checkout: {}", err),
})
}
}; |
Beta Was this translation helpful? Give feedback.
-
Thanks for reaching out! Actually, there is nothing equivalent to the The credential callbacks would only happen for HTTP based connections, if I am not mistaken. |
Beta Was this translation helpful? Give feedback.
Thanks for reaching out!
Actually, there is nothing equivalent to the
git2
version, as thessh
transport is usingssh
(program) just like GitHub. There is no way to pass custom keys unless the user configures it. Maybe Git has some more support for configuring it, but I doubt this is implemented here.The credential callbacks would only happen for HTTP based connections, if I am not mistaken.