Skip to content

Commit 999960e

Browse files
authored
Merge pull request #4236 from cdr/jsjoeio/fix-brew-bump
fix(brew-bump.sh): add checks and handle errors
2 parents 8f72481 + 8ef950a commit 999960e

File tree

3 files changed

+160
-8
lines changed

3 files changed

+160
-8
lines changed

ci/steps/brew-bump.sh

+67-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ main() {
55
cd "$(dirname "$0")/../.."
66
# Only sourcing this so we get access to $VERSION
77
source ./ci/lib.sh
8+
source ./ci/steps/steps-lib.sh
9+
10+
echo "Checking environment variables"
11+
12+
# We need VERSION to bump the brew formula
13+
if is_env_var_set "VERSION"; then
14+
echo "VERSION is not set"
15+
exit 1
16+
fi
17+
18+
# We need HOMEBREW_GITHUB_API_TOKEN to push up commits
19+
if is_env_var_set "HOMEBREW_GITHUB_API_TOKEN"; then
20+
echo "HOMEBREW_GITHUB_API_TOKEN is not set"
21+
exit 1
22+
fi
823

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

31+
# Make sure the git clone step is successful
32+
if directory_exists "homebrew-core"; then
33+
echo "git clone failed. Cannot find homebrew-core directory."
34+
ls -la
35+
exit 1
36+
fi
37+
1638
echo "Changing into homebrew-core directory"
17-
cd homebrew-core && pwd
39+
pushd homebrew-core && pwd
1840

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

44+
# Make sure the git remote step is successful
45+
if ! git config remote.upstream.url > /dev/null; then
46+
echo "git remote add upstream failed."
47+
echo "Could not find upstream in list of remotes."
48+
git remote -v
49+
exit 1
50+
fi
51+
52+
# TODO@jsjoeio - can I somehow check that this succeeded?
2253
echo "Fetching upstream Homebrew/hombrew-core commits"
2354
git fetch upstream
2455

56+
# TODO@jsjoeio - can I somehow check that this succeeded?
2557
echo "Merging in latest Homebrew/homebrew-core changes"
2658
git merge upstream/master
2759

2860
echo "Pushing changes to cdrci/homebrew-core fork on GitHub"
61+
62+
# GIT_ASKPASS lets us use the password when pushing without revealing it in the process list
63+
# See: https://serverfault.com/a/912788
64+
PATH_TO_GIT_ASKPASS="$HOME/git-askpass.sh"
2965
# Source: https://serverfault.com/a/912788
3066
# shellcheck disable=SC2016,SC2028
31-
echo '#!/bin/sh\nexec echo "$HOMEBREW_GITHUB_API_TOKEN"' > "$HOME"/.git-askpass.sh
67+
echo 'echo $HOMEBREW_GITHUB_API_TOKEN' > "$PATH_TO_ASKPASS"
68+
69+
# Make sure the git-askpass.sh file creation is successful
70+
if file_exists "$PATH_TO_GIT_ASKPASS"; then
71+
echo "git-askpass.sh not found in $HOME."
72+
ls -la "$HOME"
73+
exit 1
74+
fi
75+
3276
# Ensure it's executable since we just created it
33-
chmod +x "$HOME/.git-askpass.sh"
34-
# GIT_ASKPASS lets us use the password when pushing without revealing it in the process list
35-
# See: https://serverfault.com/a/912788
36-
GIT_ASKPASS="$HOME/.git-askpass.sh" git push https://[email protected]/cdr-oss/homebrew-core.git --all
77+
chmod +x "$PATH_TO_GIT_ASKPASS"
78+
79+
# Make sure the git-askpass.sh file is executable
80+
if is_executable "$PATH_TO_GIT_ASKPASS"; then
81+
echo "$PATH_TO_GIT_ASKPASS is not executable."
82+
ls -la "$PATH_TO_GIT_ASKPASS"
83+
exit 1
84+
fi
85+
86+
# Export the variables so git sees them
87+
export HOMEBREW_GITHUB_API_TOKEN="$HOMEBREW_GITHUB_API_TOKEN"
88+
export GIT_ASKPASS="$PATH_TO_ASKPASS"
89+
git push https://[email protected]/cdr-oss/homebrew-core.git --all
3790

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

50103
# Clean up and remove homebrew-core
51-
cd ..
104+
popd
52105
rm -rf homebrew-core
106+
107+
# Make sure homebrew-core is removed
108+
if directory_exists "homebrew-core"; then
109+
echo "rm -rf homebrew-core failed."
110+
ls -la
111+
fi
53112
}
54113

55114
main "$@"

ci/steps/steps-lib.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
# This is a library which contains functions used inside ci/steps
4+
#
5+
# We separated it into it's own file so that we could easily unit test
6+
# these functions and helpers
7+
8+
# Checks whether and environment variable is set.
9+
# Source: https://stackoverflow.com/a/62210688/3015595
10+
is_env_var_set() {
11+
local name="${1:-}"
12+
if test -n "${!name:-}"; then
13+
return 0
14+
else
15+
return 1
16+
fi
17+
}
18+
19+
# Checks whether a directory exists.
20+
directory_exists() {
21+
local dir="${1:-}"
22+
if [[ -d "${dir:-}" ]]; then
23+
return 0
24+
else
25+
return 1
26+
fi
27+
}
28+
29+
# Checks whether a file exists.
30+
file_exists() {
31+
local file="${1:-}"
32+
if test -f "${file:-}"; then
33+
return 0
34+
else
35+
return 1
36+
fi
37+
}
38+
39+
# Checks whether a file is executable.
40+
is_executable() {
41+
local file="${1:-}"
42+
if [ -f "${file}" ] && [ -r "${file}" ] && [ -x "${file}" ]; then
43+
return 0
44+
else
45+
return 1
46+
fi
47+
}

test/scripts/steps-lib.bats

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bats
2+
3+
SCRIPT_NAME="steps-lib.sh"
4+
SCRIPT="$BATS_TEST_DIRNAME/../../ci/steps/$SCRIPT_NAME"
5+
6+
source "$SCRIPT"
7+
8+
@test "is_env_var_set should return 1 if env var is not set" {
9+
run is_env_var_set "ASDF_TEST_SET"
10+
[ "$status" = 1 ]
11+
}
12+
13+
@test "is_env_var_set should return 0 if env var is set" {
14+
ASDF_TEST_SET="test" run is_env_var_set "ASDF_TEST_SET"
15+
[ "$status" = 0 ]
16+
}
17+
18+
@test "directory_exists should 1 if directory doesn't exist" {
19+
run directory_exists "/tmp/asdfasdfasdf"
20+
[ "$status" = 1 ]
21+
}
22+
23+
@test "directory_exists should 0 if directory exists" {
24+
run directory_exists "$(pwd)"
25+
[ "$status" = 0 ]
26+
}
27+
28+
@test "file_exists should 1 if file doesn't exist" {
29+
run file_exists "hello-asfd.sh"
30+
[ "$status" = 1 ]
31+
}
32+
33+
@test "file_exists should 0 if file exists" {
34+
run file_exists "$SCRIPT"
35+
[ "$status" = 0 ]
36+
}
37+
38+
@test "is_executable should 1 if file isn't executable" {
39+
run is_executable "hello-asfd.sh"
40+
[ "$status" = 1 ]
41+
}
42+
43+
@test "is_executable should 0 if file is executable" {
44+
run is_executable "$SCRIPT"
45+
[ "$status" = 0 ]
46+
}

0 commit comments

Comments
 (0)