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

fix(brew-bump.sh): add checks and handle errors #4236

Merged
merged 2 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 67 additions & 8 deletions ci/steps/brew-bump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ main() {
cd "$(dirname "$0")/../.."
# Only sourcing this so we get access to $VERSION
source ./ci/lib.sh
source ./ci/steps/steps-lib.sh

echo "Checking environment variables"

# We need VERSION to bump the brew formula
if is_env_var_set "VERSION"; then
echo "VERSION is not set"
exit 1
fi

# We need HOMEBREW_GITHUB_API_TOKEN to push up commits
if is_env_var_set "HOMEBREW_GITHUB_API_TOKEN"; then
echo "HOMEBREW_GITHUB_API_TOKEN is not set"
exit 1
fi

# NOTE: we need to make sure cdrci/homebrew-core
# is up-to-date
Expand All @@ -13,27 +28,65 @@ main() {
echo "Cloning cdrci/homebrew-core"
git clone https://github.com/cdrci/homebrew-core.git

# Make sure the git clone step is successful
if directory_exists "homebrew-core"; then
echo "git clone failed. Cannot find homebrew-core directory."
ls -la
exit 1
fi

echo "Changing into homebrew-core directory"
cd homebrew-core && pwd
pushd homebrew-core && pwd

echo "Adding Homebrew/homebrew-core as $(upstream)"
echo "Adding Homebrew/homebrew-core"
git remote add upstream https://github.com/Homebrew/homebrew-core.git

# Make sure the git remote step is successful
if ! git config remote.upstream.url > /dev/null; then
echo "git remote add upstream failed."
echo "Could not find upstream in list of remotes."
git remote -v
exit 1
fi

# TODO@jsjoeio - can I somehow check that this succeeded?
echo "Fetching upstream Homebrew/hombrew-core commits"
git fetch upstream

# TODO@jsjoeio - can I somehow check that this succeeded?
jsjoeio marked this conversation as resolved.
Show resolved Hide resolved
echo "Merging in latest Homebrew/homebrew-core changes"
git merge upstream/master

echo "Pushing changes to cdrci/homebrew-core fork on GitHub"

# GIT_ASKPASS lets us use the password when pushing without revealing it in the process list
jsjoeio marked this conversation as resolved.
Show resolved Hide resolved
# See: https://serverfault.com/a/912788
PATH_TO_GIT_ASKPASS="$HOME/git-askpass.sh"
# Source: https://serverfault.com/a/912788
# shellcheck disable=SC2016,SC2028
echo '#!/bin/sh\nexec echo "$HOMEBREW_GITHUB_API_TOKEN"' > "$HOME"/.git-askpass.sh
echo 'echo $HOMEBREW_GITHUB_API_TOKEN' > "$PATH_TO_ASKPASS"

# Make sure the git-askpass.sh file creation is successful
if file_exists "$PATH_TO_GIT_ASKPASS"; then
echo "git-askpass.sh not found in $HOME."
ls -la "$HOME"
exit 1
fi

# Ensure it's executable since we just created it
chmod +x "$HOME/.git-askpass.sh"
# GIT_ASKPASS lets us use the password when pushing without revealing it in the process list
# See: https://serverfault.com/a/912788
GIT_ASKPASS="$HOME/.git-askpass.sh" git push https://[email protected]/cdr-oss/homebrew-core.git --all
chmod +x "$PATH_TO_GIT_ASKPASS"

# Make sure the git-askpass.sh file is executable
if is_executable "$PATH_TO_GIT_ASKPASS"; then
echo "$PATH_TO_GIT_ASKPASS is not executable."
ls -la "$PATH_TO_GIT_ASKPASS"
exit 1
fi

# Export the variables so git sees them
export HOMEBREW_GITHUB_API_TOKEN="$HOMEBREW_GITHUB_API_TOKEN"
export GIT_ASKPASS="$PATH_TO_ASKPASS"
git push https://[email protected]/cdr-oss/homebrew-core.git --all

# Find the docs for bump-formula-pr here
# https://github.com/Homebrew/brew/blob/master/Library/Homebrew/dev-cmd/bump-formula-pr.rb#L18
Expand All @@ -48,8 +101,14 @@ main() {
fi

# Clean up and remove homebrew-core
cd ..
popd
rm -rf homebrew-core

# Make sure homebrew-core is removed
if directory_exists "homebrew-core"; then
echo "rm -rf homebrew-core failed."
ls -la
fi
}

main "$@"
47 changes: 47 additions & 0 deletions ci/steps/steps-lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

# This is a library which contains functions used inside ci/steps
#
# We separated it into it's own file so that we could easily unit test
# these functions and helpers

# Checks whether and environment variable is set.
# Source: https://stackoverflow.com/a/62210688/3015595
is_env_var_set() {
local name="${1:-}"
if test -n "${!name:-}"; then
return 0
else
return 1
fi
}

# Checks whether a directory exists.
directory_exists() {
local dir="${1:-}"
if [[ -d "${dir:-}" ]]; then
return 0
else
return 1
fi
}

# Checks whether a file exists.
file_exists() {
local file="${1:-}"
if test -f "${file:-}"; then
return 0
else
return 1
fi
}

# Checks whether a file is executable.
is_executable() {
local file="${1:-}"
if [ -f "${file}" ] && [ -r "${file}" ] && [ -x "${file}" ]; then
return 0
else
return 1
fi
}
46 changes: 46 additions & 0 deletions test/scripts/steps-lib.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bats

SCRIPT_NAME="steps-lib.sh"
SCRIPT="$BATS_TEST_DIRNAME/../../ci/steps/$SCRIPT_NAME"

source "$SCRIPT"

@test "is_env_var_set should return 1 if env var is not set" {
run is_env_var_set "ASDF_TEST_SET"
[ "$status" = 1 ]
}

@test "is_env_var_set should return 0 if env var is set" {
ASDF_TEST_SET="test" run is_env_var_set "ASDF_TEST_SET"
[ "$status" = 0 ]
}

@test "directory_exists should 1 if directory doesn't exist" {
run directory_exists "/tmp/asdfasdfasdf"
[ "$status" = 1 ]
}

@test "directory_exists should 0 if directory exists" {
run directory_exists "$(pwd)"
[ "$status" = 0 ]
}

@test "file_exists should 1 if file doesn't exist" {
run file_exists "hello-asfd.sh"
[ "$status" = 1 ]
}

@test "file_exists should 0 if file exists" {
run file_exists "$SCRIPT"
[ "$status" = 0 ]
}

@test "is_executable should 1 if file isn't executable" {
run is_executable "hello-asfd.sh"
[ "$status" = 1 ]
}

@test "is_executable should 0 if file is executable" {
run is_executable "$SCRIPT"
[ "$status" = 0 ]
}