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

chore(vdev): Rewrite release-homebrew into vdev #16772

Merged
merged 10 commits into from
Apr 3, 2023
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 0 additions & 36 deletions scripts/release-homebrew.sh

This file was deleted.

8 changes: 6 additions & 2 deletions vdev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ directories = "5.0.0"
# remove this when stabilized https://doc.rust-lang.org/stable/std/path/fn.absolute.html
dunce = "1.0.3"
hashlink = { version = "0.8.1", features = ["serde_impl"] }
hex = "0.4.3"
indicatif = { version = "0.17.3", features = ["improved_unicode"] }
itertools = "0.10.5"
log = "0.4.17"
Expand All @@ -28,8 +29,11 @@ os_info = { version = "3.7.0", default-features = false }
# watch https://github.com/epage/anstyle for official interop with Clap
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
paste = "1.0.12"
regex = { version = "1.7.3", default-features = false, features = ["std", "unicode-perl"] }
regex = { version = "1.7.1", default-features = false, features = ["std", "perf"] }
reqwest = { version = "0.11", features = ["json", "blocking"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.95"
serde_yaml = "0.9.19"
toml = { version = "0.7.3", default-features = false, features = ["parse"] }
sha2 = "0.10.6"
tempfile = "3.4.0"
toml = { version = "0.7.2", default-features = false, features = ["parse"] }
79 changes: 79 additions & 0 deletions vdev/src/commands/release/homebrew.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use anyhow::{Result};
use std::env;
use tempfile::TempDir;
use crate::git;
use hex;
use sha2::Digest;
use reqwest;
use std::path::Path;
use regex;

/// Releases latest version to the vectordotdev homebrew tap
#[derive(clap::Args, Debug)]
#[command()]
pub struct Cli {}

impl Cli {
pub fn exec(self) -> Result<()> {
// Create temporary directory for cloning the homebrew-brew repository
let td = TempDir::new()?;
env::set_current_dir(td.path())?;

// Set git configurations
let config_values = &[
("user.name", "vic"),
("user.email", "vector@datadoghq.com"),
];
git::set_config_values(config_values)?;
let github_token = env::var("GITHUB_TOKEN")?;

// Clone the homebrew-brew repository
let homebrew_repo = format!("https://{github_token}:x-oauth-basic@github.com/vectordotdev/homebrew-brew");
git::clone(&homebrew_repo)?;
env::set_current_dir("homebrew-brew")?;

// Get package details for updating Formula/vector.rb
let vector_version = env::var("VECTOR_VERSION")?;
let package_url = format!("https://packages.timber.io/vector/{vector_version}/vector-{vector_version}-x86_64-apple-darwin.tar.gz");
let package_sha256 = hex::encode(sha2::Sha256::digest(reqwest::blocking::get(&package_url)?.bytes()?));

// Update content of Formula/vector.rb
update_content("Formula/vector.rb", &package_url, &package_sha256, &vector_version)?;

// Check if there is any change in git index
let has_changes = !git::check_git_repository_clean()?;
if has_changes {
let commit_message = format!("Release Vector {vector_version}");
git::commit(&commit_message)?;
}
git::push()?;

// Remove temporary directory
td.close()?;
Ok(())
}
}

/// Open the vector.rb file and update the new content
fn update_content<P>(file_path: P, package_url: &str, package_sha256: &str, vector_version: &str) -> Result<()>
where
P: AsRef<Path>,
{
let content = std::fs::read_to_string(&file_path)?;
let patterns = [
(format!(r#"url "{package_url}""#), r#"url ".*""#),
(format!(r#"sha256 "{package_sha256}""#), r#"sha256 ".*""#),
(format!(r#"version "{vector_version}""#), r#"version ".*""#),
];
let new_content = substitute(content, &patterns);
std::fs::write(file_path, new_content)?;
Ok(())
}

fn substitute(mut content: String, patterns: &[(String, &str)]) -> String {
for (value, pattern) in patterns {
let re = regex::Regex::new(pattern).unwrap();
content = re.replace_all(&content, value.as_str()).to_string();
}
content
}
6 changes: 1 addition & 5 deletions vdev/src/commands/release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ crate::cli_subcommands! {
commit,
docker,
mod github,
homebrew,
mod homebrew,
mod prepare,
mod push,
s3,
Expand All @@ -23,10 +23,6 @@ crate::script_wrapper! {
docker = "Build the Vector docker images and optionally push it to the registry"
=> "build-docker.sh"
}
crate::script_wrapper! {
homebrew = "Releases latest version to the vectordotdev homebrew tap"
=> "release-homebrew.sh"
}
crate::script_wrapper! {
s3 = "Uploads archives and packages to AWS S3"
=> "release-s3.sh"
Expand Down
37 changes: 36 additions & 1 deletion vdev/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn get_git_sha() -> Result<String> {
check_output(&["rev-parse", "--short", "HEAD"]).map(|output| output.trim_end().to_string())
}

// Get a list of files that have been modified, as a vector of strings
/// Get a list of files that have been modified, as a vector of strings
pub fn get_modified_files() -> Result<Vec<String>> {
let args = vec![
"ls-files",
Expand All @@ -91,6 +91,41 @@ pub fn get_modified_files() -> Result<Vec<String>> {
Ok(check_output(&args)?.lines().map(str::to_owned).collect())
}

pub fn set_config_values(config_values: &[(&str, &str)]) -> Result<String> {
let mut args = vec!["config"];

for (key, value) in config_values {
args.push(key);
args.push(value);
}

check_output(&args)
}

/// Checks if the current directory's repo is clean
pub fn check_git_repository_clean() -> Result<bool> {
Ok(Command::new("git")
.args(["diff-index", "--quiet", "HEAD"])
.stdout(std::process::Stdio::null())
.status()
.map(|status| status.success())?)
}

/// Commits changes from the current repo
pub fn commit(commit_message: &str) -> Result<String> {
check_output(&["commit", "--all", "--message", commit_message])
}

/// Pushes changes from the current repo
pub fn push() -> Result<String> {
check_output(&["push"])
}

pub fn clone(repo_url: &str) -> Result<String> {
// We cannot use capture_output since this will need to run in the CWD
Command::new("git").args(["clone", repo_url]).check_output()
}

pub fn branch_exists(branch_name: &str) -> Result<bool> {
let output = check_output(&["rev-parse", "--verify", branch_name])?;
Ok(!output.is_empty())
Expand Down