Skip to content

Commit

Permalink
chore(releasing): Fix homebrew release script (vectordotdev#17131)
Browse files Browse the repository at this point in the history
* rustfmt

Signed-off-by: Jesse Szwedko <jesse.szwedko@datadoghq.com>

* Fix hombrew release subcommand

A few changes:

* `git config` only takes one key/value at a time
* The `git` commands here need to run in the context of the homebrew-repo so shouldn't use
  the `check_output` function which changes directory to `vector`

Signed-off-by: Jesse Szwedko <jesse.szwedko@datadoghq.com>

* clippy and one more git cwd change

Signed-off-by: Jesse Szwedko <jesse.szwedko@datadoghq.com>

* Move git config to after cd into homebrew repo

Signed-off-by: Jesse Szwedko <jesse.szwedko@datadoghq.com>

---------

Signed-off-by: Jesse Szwedko <jesse.szwedko@datadoghq.com>
  • Loading branch information
jszwedko committed Apr 12, 2023
1 parent 4bf6805 commit cfbf233
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
42 changes: 27 additions & 15 deletions vdev/src/commands/release/homebrew.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use anyhow::{Result};
use std::env;
use tempfile::TempDir;
use crate::git;
use anyhow::Result;
use hex;
use sha2::Digest;
use regex;
use reqwest;
use sha2::Digest;
use std::env;
use std::path::Path;
use regex;
use tempfile::TempDir;

/// Releases latest version to the vectordotdev homebrew tap
#[derive(clap::Args, Debug)]
Expand All @@ -19,33 +19,40 @@ impl Cli {
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");
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")?;

// Set git configurations
git::set_config_value("user.name", "vic")?;
git::set_config_value("user.email", "vector@datadoghq.com")?;

// 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()?));
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)?;
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
Expand All @@ -55,7 +62,12 @@ impl Cli {
}

/// 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<()>
fn update_content<P>(
file_path: P,
package_url: &str,
package_sha256: &str,
vector_version: &str,
) -> Result<()>
where
P: AsRef<Path>,
{
Expand Down
20 changes: 9 additions & 11 deletions vdev/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,11 @@ 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)
pub fn set_config_value(key: &str, value: &str) -> Result<String> {
Command::new("git")
.args(["config", key, value])
.stdout(std::process::Stdio::null())
.check_output()
}

/// Checks if the current directory's repo is clean
Expand All @@ -113,12 +109,14 @@ pub fn check_git_repository_clean() -> Result<bool> {

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

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

pub fn clone(repo_url: &str) -> Result<String> {
Expand Down

0 comments on commit cfbf233

Please sign in to comment.