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
5 changes: 5 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.

5 changes: 5 additions & 0 deletions vdev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ directories = "4.0.1"
# 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 @@ -27,7 +28,11 @@ os_info = { version = "3.6.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.1", default-features = false, features = ["std", "perf"] }
reqwest = { version = "0.11", features = ["json", "blocking"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.94"
serde_yaml = "0.9.19"
sha2 = "0.10.6"
tempfile = "3.4.0"
toml = { version = "0.7.2", default-features = false, features = ["parse"] }
80 changes: 80 additions & 0 deletions vdev/src/commands/release/homebrew.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use anyhow::{Result};
use std::env;
use tempfile::TempDir;
use crate::git;
use hex;
use sha2::Digest;
use reqwest::blocking::get;
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
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 = vec![
("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_dir = td.path().join("homebrew-brew");
let homebrew_repo = format!("https://{github_token}:x-oauth-basic@github.com/vectordotdev/homebrew-brew");
git::clone(&homebrew_repo)?;
env::set_current_dir(&homebrew_dir)?;
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved

// Get package details for updating Formula/vector.rb
// TODO: use app::version() when it's checked in to master, currently in another PR here: https://github.com/vectordotdev/vector/pull/16724/files#diff-492220caf4fa036bb031d00a23eaa01aa4a0fd5636b2a789bd18f3ce184ede21
let vector_version = env::var("VECTOR_VERSION")?;
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
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(get(&package_url)?.bytes()?));

// Update content of Formula/vector.rb
let file_path = homebrew_dir.join("Formula").join("vector.rb");
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
let new_content = update_content(file_path.as_path(), &package_url, &package_sha256, &vector_version)?;
std::fs::write(file_path, new_content)?;
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved

// 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(file_path: &Path, package_url: &str, package_sha256: &str, vector_version: &str) -> Result<String> {
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)?;
Ok(new_content)
}

fn substitute(content: &str, patterns: &[(String, &str)]) -> Result<String> {
let mut result = content.to_owned();
for (value, pattern) in patterns {
let re = regex::Regex::new(pattern)?;
result = re.replace_all(&result, value.as_str()).to_string();
}
Ok(result)
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
}
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,
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! {
push = "Pushes new versions produced by `make release` to the repository"
=> "release-push.sh"
Expand Down
60 changes: 58 additions & 2 deletions vdev/src/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashSet, process::Command};
use std::{collections::HashSet, process::Command, process::Output};

use anyhow::Result;
use anyhow::{anyhow, Result};

use crate::app::CommandExt as _;

Expand Down Expand Up @@ -63,6 +63,62 @@ pub fn get_modified_files() -> Result<Vec<String>> {
Ok(capture_output(&args)?.lines().map(str::to_owned).collect())
}

pub fn set_config_values(config_values: Vec<(&str, &str)>) -> Result<String> {
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
let mut args = Vec::new();
args.push("config");
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
// args.push("--global");
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved

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

capture_output(&args)
}

// Checks if the current directory's repo is clean
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
pub fn check_git_repository_clean() -> Result<bool> {
let output = Command::new("git")
.arg("diff-index")
.arg("--quiet")
.arg("HEAD")
.output()
.map_err(|e| anyhow!("{}", e))?;

Ok(output.status.success())
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
}

// Commits changes from the current repo
pub fn commit(commit_message: &str) -> Result<Output> {
let output = Command::new("git")
.arg("-am")
.arg(commit_message)
.output()
.map_err(|e| anyhow!("{}", e))?;

Ok(output)
}
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved

// Pushes changes from the current repo
pub fn push() -> Result<Output> {
let output = Command::new("git")
.arg("push")
.output()
.map_err(|e| anyhow!("{}", e))?;

Ok(output)
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn clone(repo_url: &str) -> Result<Output> {
let output = Command::new("git")
.arg("clone")
.arg(repo_url)
.output()
.map_err(|e| anyhow!("{}", e))?;

Ok(output)
}

fn capture_output(args: &[&str]) -> Result<String> {
Command::new("git").in_repo().args(args).capture_output()
}
Expand Down