Skip to content

Commit

Permalink
ENH Rebuild client/dist on merge-up
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 1, 2023
1 parent c28c875 commit 25726e4
Showing 1 changed file with 132 additions and 14 deletions.
146 changes: 132 additions & 14 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,6 @@ runs:
exit 1
fi
done
# Don't allow merge-ups when there are JS changes that would require a yarn build
if [[ $(echo "$FILES" | grep client/dist) != "" ]]; then
echo "Unable to mergeup between $FROM_BRANCH and $INTO_BRANCH - there are changes to JS dist files"
exit 1
fi
done
# actions/checkout with fetch-depth: 0 will fetch ALL git history for the repository
Expand All @@ -146,6 +140,7 @@ runs:
shell: bash
env:
BRANCHES: ${{ steps.determine.outputs.branches }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# Set git user to github-actions bot
# The 41898282+ email prefixed is the required, matches the ID here
Expand All @@ -168,21 +163,141 @@ runs:
git checkout $FROM_BRANCH
git checkout $INTO_BRANCH
# Determine if we will rebuild dist file during merge-up
# This is based simply on if there are changes in the client/ directory
REBUILD=
CLIENT_DIFF_FILES=$(git diff --name-only $INTO_BRANCH...$FROM_BRANCH | grep -P ^client/)
if [[ $CLIENT_DIFF_FILES != "" ]]; then
REBUILD=1
fi
echo "CLIENT_DIFF_FILES is:"
# The following line is quoted so that newlines show
echo "$CLIENT_DIFF_FILES"
echo "REBUILD is $REBUILD"
# Perform the merge-up
git merge --no-ff --no-commit $FROM_BRANCH
# `|| true is suffixed to the command to prevent the job from stopping on merge conflict
# This is because git will sent a non-zero exit code when there is a merge conflict
# We often expect a merge-conflict when there are client/dist file differences
git merge --no-ff --no-commit $FROM_BRANCH || true
# Check for merge conflicts - this is just an additional check that is probably
# not required as git seems like it does the equivalent of exit 1 when it
# detects a merge conflict. Still it doesn't hurt to be extra cautious.
GIT_STATUS=$(git status)
if [[ "$GIT_STATUS" =~ 'Changes not staged for commit' ]]; then
# Only merge conflicts in client/dist are allowed, stop for all others
MERGE_CONFLICTS=$(git status | grep "both modified" | grep -v client/dist) || true
if [[ $MERGE_CONFLICTS != "" ]]; then
echo "Merge conflict found when merging-up $FROM_BRANCH into $INTO_BRANCH. Aborting."
# The following line needs to be quoted so that line breaks show
echo "$MERGE_CONFLICTS"
exit 1
fi
# Rebuild client/dist if needed
if [[ $REBUILD == 1 ]]; then
# Ensure .nvmrc is present
if ! [[ -f .nvmrc ]]; then
echo "Unable to find .nvmrc file"
exit 1
fi
NVM_VERSION=$(cat .nvmrc | xargs)
echo "NVM_VERSION is $NVM_VERSION"
# Ensure nvm is installed
if [[ $(which nvm) == "" ]]; then
wget -q -O __nvm_install.sh https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh
if [[ $(sha1sum __nvm_install.sh) != "c10365646e699a74e279e6ae71569430f82ba014 __nvm_install.sh" ]]; then
"Unable to verify integrity of nvm install script"
exit 1
fi
. __nvm_install.sh
rm __nvm_install.sh
# Load nvm without needing terminal restart
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
fi
# Install correct version of node and yarn
nvm install $NVM_VERSION
nvm use $NVM_VERSION
if [[ $(which yarn) == "" ]]; then
npm install -g yarn
fi
# Get composer name of the module
COMPOSER_NAME=$(jq -r ".name" composer.json)
# Install silverstripe/admin in sibling directory if needed so that shared components are available
if [[ $COMPOSER_NAME != silverstripe/admin ]]; then
DIR=$(pwd)
# Work out version of silverstripe/admin to checkout
cd ..
mkdir __tmp
cd __tmp
ADMIN_VERSION=$(COMPOSER_NAME=$COMPOSER_NAME INTO_BRANCH=$INTO_BRANCH php -r '
$COMPOSER_NAME = getenv("COMPOSER_NAME");
$INTO_BRANCH = getenv("INTO_BRANCH");
$json = [
"require" => [
"$COMPOSER_NAME" => "$INTO_BRANCH.x-dev",
"silverstripe/recipe-kitchen-sink" => "*"
],
"prefer-stable" => false,
"minimum-stability" => "dev"
];
file_put_contents("composer.json", json_encode($json, JSON_UNESCAPED_SLASHES));
shell_exec("composer update --no-install");
$lock = json_decode(file_get_contents("composer.lock"), true);
$version = array_values(array_filter(
$lock["packages"],
fn($p) => $p["name"] === "silverstripe/admin"
))[0]["version"];
echo str_replace(".x-dev", "", $version);
')
cd ..
rm -rf __tmp
if [[ $ADMIN_VERSION == "" ]]; then
echo "Unable to determine version of silverstripe/admin version to checkout"
exit 1
fi
echo "ADMIN_VERSION is $ADMIN_VERSION"
# Install admin if required
if [[ ! -d admin ]]; then
git clone https://github.com/silverstripe/silverstripe-admin.git admin
fi
# Checkout admin version
cd admin
git checkout $ADMIN_VERSION
yarn install
cd $DIR
fi
# Rebuild dist files
yarn build
# Only add client files
for FILE in $CLIENT_DIFF_FILES; do
echo "Adding $FILE to commit"
git add $FILE
done
fi
GIT_STATUS=$(git status)
# Check for any unmerged files
if [[ "$GIT_STATUS" =~ 'Unmerged paths' ]]; then
echo "Unmerged files found when merging-up $FROM_BRANCH into $INTO_BRANCH. Aborting."
echo $GIT_STATUS
exit 1
fi
# Check for any random files that shouldn't be committed
if [[ "$GIT_STATUS" =~ 'Untracked files' ]]; then
echo "Untracked files found when merging-up $FROM_BRANCH into $INTO_BRANCH. Aborting."
echo $GIT_STATUS
exit 1
fi
Expand All @@ -193,8 +308,11 @@ runs:
fi
# Commit and push the merge-up
# --no-edit in the context of a merge commit uses the default auto-generated commit message.
git commit --no-edit
# The quotes in the commit message are intential and match the auto-generated
# title of e.g. Merge branch '2.0' into 2
# Using this instead of simply `git status --no-edit` which auto-generates a commit message because
# that includes commented out details of the merge conflict if there was one
git commit -m "Merge branch '$FROM_BRANCH' into $INTO_BRANCH"
git push origin $INTO_BRANCH
echo "Succesfully merged-up $FROM_BRANCH into $INTO_BRANCH"
Expand Down

0 comments on commit 25726e4

Please sign in to comment.