Skip to content

Commit

Permalink
Merge branch '1.0' into 1
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 17, 2023
2 parents 0d8424d + a7fe7bb commit fd9aa49
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 22 deletions.
174 changes: 156 additions & 18 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ runs:
# Download composer.json for use in branches.php
curl -s -o __composer.json https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$DEFAULT_BRANCH/composer.json
BRANCHES=$(MINIMUM_CMS_MAJOR=$MINIMUM_CMS_MAJOR DEFAULT_BRANCH=$DEFAULT_BRANCH php ${{ github.action_path }}/branches.php)
BRANCHES=$(MINIMUM_CMS_MAJOR=$MINIMUM_CMS_MAJOR DEFAULT_BRANCH=$DEFAULT_BRANCH GITHUB_REPOSITORY=$GITHUB_REPOSITORY php ${{ github.action_path }}/branches.php)
echo "BRANCHES is $BRANCHES"
if [[ $BRANCHES =~ "^FAILURE \- (.+)$" ]]; then
MESSAGE=${BASH_REMATCH[1]}
Expand Down 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,33 +163,176 @@ 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=0
CLIENT_DIFF_FILES=$(git diff --name-only $INTO_BRANCH...$FROM_BRANCH | grep -P ^client/) || true
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
# See https://git-scm.com/docs/git-status#_output for information on the porcelain format
UNMERGED_FILES=$(git status --porcelain=v1 | grep -P '^(DD|AU|UD|UA|DU|AA|UU)' | grep -v client/dist) || true
if [[ $UNMERGED_FILES != "" ]]; 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 "$UNMERGED_FILES"
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
# Piping into xargs trims out whitespace
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"
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);
')
echo "ADMIN_VERSION is $ADMIN_VERSION"
if ! [[ $ADMIN_VERSION =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
echo "Unable to determine major or minor branch of silverstripe/admin version to checkout"
exit 1
fi
cd ..
rm -rf __tmp
# 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
git add client/dist
fi
# See https://git-scm.com/docs/git-status#_output for information on the porcelain format
GIT_STATUS=$(git status --porcelain=v1)
# Check for any unmerged files
UNMERGED_FILES=$(echo "$GIT_STATUS" | grep -P '^(DD|AU|UD|UA|DU|AA|UU)') || true
if [[ $UNMERGED_FILES != "" ]]; then
echo "Unmerged files found when merging-up $FROM_BRANCH into $INTO_BRANCH. Aborting."
echo "$UNMERGED_FILES"
exit 1
fi
# Check for any random files that shouldn't be committed
if [[ "$GIT_STATUS" =~ 'Untracked files' ]]; then
# Check for any random untracked files that shouldn't be committed
UNTRACKED_FILES=$(echo "$GIT_STATUS" | grep -P '^\?') || true
if [[ $UNTRACKED_FILES != "" ]]; then
echo "Untracked files found when merging-up $FROM_BRANCH into $INTO_BRANCH. Aborting."
echo "$UNTRACKED_FILES"
exit 1
fi
# Continue if there's nothing to commit
if [[ "$GIT_STATUS" =~ 'nothing to commit, working tree clean' ]]; then
if [[ $GIT_STATUS == "" ]]; then
echo "No changes found when merging-up $FROM_BRANCH into $INTO_BRANCH. Skipping."
continue
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"
# Trigger the CI workflow manually via GitHub API
# Do this because the ci.yml `push` event does not seem to be triggered by another workflow doing a push,
# instead it only seems to be triggered by a normal git user doing a push
# https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event
RESP_CODE=$(curl -w %{http_code} -s -L -o /dev/null \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$GITHUB_REPOSITORY/actions/workflows/ci.yml/dispatches \
-d "{\"ref\":\"$INTO_BRANCH\"}"
)
if [[ $RESP_CODE != "204" ]]; then
echo "Failed to dispatch workflow - HTTP response code was $RESP_CODE"
exit 1
else
echo "Succesfully triggered CI workflow for $INTO_BRANCH"
fi
done
3 changes: 2 additions & 1 deletion branches.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

$defaultBranch = getenv('DEFAULT_BRANCH');
$minimumCmsMajor = getenv('MINIMUM_CMS_MAJOR');
$githubRepository = getenv('GITHUB_REPOSITORY');

$branches = branches($defaultBranch, $minimumCmsMajor);
$branches = branches($defaultBranch, $minimumCmsMajor, $githubRepository);
echo implode(' ', $branches);
12 changes: 10 additions & 2 deletions funcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
function branches(
string $defaultBranch,
string $minimumCmsMajor,
string $githubRepository,
// The following params are purely for unit testing, for the actual github action it will read json files instead
string $composerJson = '',
string $branchesJson = '',
Expand All @@ -28,7 +29,13 @@ function branches(
}
$defaultCmsMajor = '';
$matchedOnBranchThreeLess = false;
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/framework'} ?? '');
$version = '';
if ($githubRepository === 'silverstripe/developer-docs') {
$version = $defaultBranch;
}
if (!$version) {
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/framework'} ?? '');
}
if (!$version) {
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/cms'} ?? '');
}
Expand Down Expand Up @@ -115,7 +122,8 @@ function branches(
unset($branches[$i]);
continue;
}
if (isset($minorsWithStableTags[$major][$branch])) {
// for developer-docs which has no tags, pretend that every branch has a tag
if (isset($minorsWithStableTags[$major][$branch]) || $githubRepository === 'silverstripe/developer-docs') {
$foundMinorBranchWithStableTag[$major] = true;
}
$foundMinorInMajor[$major] = true;
Expand Down
39 changes: 38 additions & 1 deletion tests/BranchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ public function testBranches(
array $expected,
string $defaultBranch,
string $minimumCmsMajor,
string $githubRepository,
string $composerJson = '',
string $branchesJson = '',
string $tagsJson = ''
) {
$actual = branches($defaultBranch, $minimumCmsMajor, $composerJson, $branchesJson, $tagsJson);
$actual = branches(
$defaultBranch,
$minimumCmsMajor,
$githubRepository,
$composerJson,
$branchesJson,
$tagsJson
);
$this->assertSame($expected, $actual);
}

Expand All @@ -26,6 +34,7 @@ public function provideBranches()
'expected' => ['4.13', '4', '5.0', '5.1', '5', '6'],
'defaultBranch' => '5',
'minimumCmsMajor' => '4',
'githubRepository' => 'lorem/ipsum',
'composerJson' => <<<EOT
{
"require": {
Expand Down Expand Up @@ -65,6 +74,7 @@ public function provideBranches()
'expected' => ['4.13', '4', '5.1', '5'],
'defaultBranch' => '5',
'minimumCmsMajor' => '4',
'githubRepository' => 'lorem/ipsum',
'composerJson' => <<<EOT
{
"require": {
Expand Down Expand Up @@ -103,6 +113,7 @@ public function provideBranches()
'expected' => ['4.13', '4', '5.1', '5'],
'defaultBranch' => '5',
'minimumCmsMajor' => '4',
'githubRepository' => 'lorem/ipsum',
'composerJson' => <<<EOT
{
"require": {
Expand Down Expand Up @@ -137,6 +148,7 @@ public function provideBranches()
'expected' => ['4.13', '4', '5.1', '5'],
'defaultBranch' => '5',
'minimumCmsMajor' => '4',
'githubRepository' => 'lorem/ipsum',
'composerJson' => <<<EOT
{
"require": {
Expand Down Expand Up @@ -166,6 +178,7 @@ public function provideBranches()
'expected' => ['1.13', '2.0', '2.1', '2'],
'defaultBranch' => '2',
'minimumCmsMajor' => '4',
'githubRepository' => 'lorem/ipsum',
'composerJson' => <<<EOT
{
"require": {
Expand Down Expand Up @@ -199,6 +212,7 @@ public function provideBranches()
'expected' => ['1.13', '1', '2.1', '2.2', '2.3', '2'],
'defaultBranch' => '2',
'minimumCmsMajor' => '4',
'githubRepository' => 'lorem/ipsum',
'composerJson' => <<<EOT
{
"require": {
Expand Down Expand Up @@ -231,6 +245,7 @@ public function provideBranches()
'expected' => ['5.9', '5', '6.0', '6', '7'],
'defaultBranch' => '5', // this repo has a `5` branch for CMS 4 and a '6' branch for CMS 5
'minimumCmsMajor' => '4',
'githubRepository' => 'lorem/ipsum',
'composerJson' => <<<EOT
{
"require": {
Expand Down Expand Up @@ -258,6 +273,28 @@ public function provideBranches()
]
EOT,
],
'developer-docs' => [
'expected' => ['4.13', '4', '5.0', '5'],
'defaultBranch' => '5',
'minimumCmsMajor' => '4',
'githubRepository' => 'silverstripe/developer-docs',
'composerJson' => <<<EOT
{
"no-require": {}
}
EOT,
'branchesJson' => <<<EOT
[
{"name": "5"},
{"name": "5.0"},
{"name": "4.13"},
{"name": "4.12"},
{"name": "4"},
{"name": "3"}
]
EOT,
'tagsJson' => '[]',
],
];
}
}

0 comments on commit fd9aa49

Please sign in to comment.