2023-07-29 19:23:05 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum VcsClient {
|
|
|
|
Noop {},
|
2023-08-01 22:54:26 +02:00
|
|
|
Git {
|
|
|
|
source: PathBuf,
|
|
|
|
username: String,
|
|
|
|
email: String,
|
2023-08-03 23:31:53 +02:00
|
|
|
token: String,
|
2023-08-01 22:54:26 +02:00
|
|
|
},
|
2023-07-29 19:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VcsClient {
|
|
|
|
pub fn new_noop() -> VcsClient {
|
|
|
|
Self::Noop {}
|
|
|
|
}
|
2023-07-30 22:23:03 +02:00
|
|
|
|
2023-08-01 22:54:26 +02:00
|
|
|
pub fn new_git(
|
|
|
|
path: &Path,
|
|
|
|
git_username: Option<impl Into<String>>,
|
|
|
|
git_email: Option<impl Into<String>>,
|
2023-08-03 23:31:53 +02:00
|
|
|
git_token: String,
|
2023-08-01 22:54:26 +02:00
|
|
|
) -> anyhow::Result<VcsClient> {
|
2023-07-29 19:23:05 +02:00
|
|
|
if !path.to_path_buf().join(".git").exists() {
|
|
|
|
anyhow::bail!("git directory not found in: {}", path.display().to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self::Git {
|
|
|
|
source: path.to_path_buf(),
|
2023-08-01 22:54:26 +02:00
|
|
|
username: git_username
|
|
|
|
.map(|u| u.into())
|
|
|
|
.unwrap_or("cuddle-please".to_string()),
|
|
|
|
email: git_email
|
|
|
|
.map(|e| e.into())
|
|
|
|
.unwrap_or("bot@cuddle.sh".to_string()),
|
2023-08-03 23:31:53 +02:00
|
|
|
token: git_token,
|
2023-07-29 19:23:05 +02:00
|
|
|
})
|
|
|
|
}
|
2023-07-30 22:23:03 +02:00
|
|
|
|
|
|
|
pub fn checkout_branch(&self) -> anyhow::Result<()> {
|
|
|
|
match self {
|
|
|
|
VcsClient::Noop {} => {}
|
|
|
|
VcsClient::Git { .. } => {
|
2023-07-30 22:47:00 +02:00
|
|
|
if let Err(_e) = self.exec_git(&["branch", "-D", "cuddle-please/release"]) {
|
2023-07-30 22:23:03 +02:00
|
|
|
tracing::debug!("failed to cleaned up local branch for force-push, this may be because it didn't exist before running this command");
|
|
|
|
}
|
|
|
|
self.exec_git(&["checkout", "-b", "cuddle-please/release"])?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec_git(&self, args: &[&str]) -> anyhow::Result<()> {
|
|
|
|
match self {
|
|
|
|
VcsClient::Noop {} => {}
|
2023-08-01 22:54:26 +02:00
|
|
|
VcsClient::Git {
|
|
|
|
source,
|
|
|
|
username,
|
|
|
|
email,
|
2023-08-03 23:31:53 +02:00
|
|
|
token,
|
2023-08-01 22:54:26 +02:00
|
|
|
} => {
|
2023-07-30 22:23:03 +02:00
|
|
|
let checkout_branch = std::process::Command::new("git")
|
|
|
|
.current_dir(source.as_path())
|
2023-08-03 00:46:20 +02:00
|
|
|
.args([
|
2023-08-03 23:31:53 +02:00
|
|
|
"-c",
|
|
|
|
&format!("http.extraHeader='Authorization: token {}'", token),
|
|
|
|
"-c",
|
|
|
|
&format!("http.extraHeader='Sudo: kjuulh'"),
|
2023-08-01 22:54:26 +02:00
|
|
|
"-c",
|
|
|
|
&format!("user.name={}", username),
|
|
|
|
"-c",
|
|
|
|
&format!("user.email={}", email),
|
|
|
|
])
|
2023-07-30 22:23:03 +02:00
|
|
|
.args(args)
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
let stdout = std::str::from_utf8(&checkout_branch.stdout)?;
|
|
|
|
let stderr = std::str::from_utf8(&checkout_branch.stderr)?;
|
|
|
|
tracing::debug!(stdout = stdout, stderr = stderr, "git {}", args.join(" "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn commit_and_push(&self, version: impl Into<String>, dry_run: bool) -> anyhow::Result<()> {
|
|
|
|
match self {
|
|
|
|
VcsClient::Noop {} => {}
|
|
|
|
VcsClient::Git { .. } => {
|
|
|
|
self.exec_git(&["add", "."])?;
|
|
|
|
self.exec_git(&[
|
|
|
|
"commit",
|
|
|
|
"-m",
|
|
|
|
&format!("chore(release): {}", version.into()),
|
|
|
|
])?;
|
|
|
|
|
|
|
|
tracing::trace!("git push -u -f origin cuddle-please/release");
|
|
|
|
if !dry_run {
|
|
|
|
self.exec_git(&["push", "-u", "-f", "origin", "cuddle-please/release"])?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-29 19:23:05 +02:00
|
|
|
}
|