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
github-actions[bot] committed Sep 2, 2023
2 parents 56fbc17 + e2c573b commit 9ca65c4
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 50 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/ci.yml

This file was deleted.

164 changes: 143 additions & 21 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ runs:
fi
# Download composer.json for use in branches.php
curl -s -o __composer.json https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$DEFAULT_BRANCH/composer.json
RESP_CODE=$(curl -w %{http_code} -s -o __composer.json https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$DEFAULT_BRANCH/composer.json)
if [[ $RESP_CODE != 200 ]]; then
rm __composer.json
fi
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"
Expand All @@ -88,7 +91,9 @@ runs:
echo "branches=$BRANCHES" >> $GITHUB_OUTPUT
rm __tags.json
rm __branches.json
rm __composer.json
if [[ -f __composer.json ]]; then
rm __composer.json
fi
# Check to see if there is anything to merge-up using the GitHub API
# Another approach is to see if we should merged using git, however that approach requires us to
Expand Down Expand Up @@ -119,14 +124,51 @@ runs:
FILES=$(jq -r .files[].filename __compare.json)
rm __compare.json
# Don't allow merge-ups when there are changes in dependency files
DEPENDENCY_FILES="composer.json package.json yarn.lock"
# Don't allow merge-ups when there are changes in javascript dependency files
DEPENDENCY_FILES="package.json yarn.lock"
for DEPENDENCY_FILE in $DEPENDENCY_FILES; do
if [[ $(echo "$FILES" | grep $DEPENDENCY_FILE) != "" ]]; then
echo "Unable to mergeup between $FROM_BRANCH and $INTO_BRANCH - there are changes in $DEPENDENCY_FILE"
echo "Unable to mergeup $FROM_BRANCH into $INTO_BRANCH - there are changes in $DEPENDENCY_FILE"
exit 1
fi
done
# Don't allow merge-ups where the are changes composer.json, unless the only changes are in "require-dev"
if [[ $(echo "$FILES" | grep composer.json) != "" ]]; then
RESP_CODE=$(curl -w %{http_code} -s -o __composer_from.json https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$FROM_BRANCH/composer.json)
if [[ $RESP_CODE != 200 ]]; then
echo "Unable to download composer.json for branch $FROM_BRANCH - HTTP response code was $RESP_CODE"
exit 1
fi
RESP_CODE=$(curl -w %{http_code} -s -o __composer_into.json https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$INTO_BRANCH/composer.json)
if [[ $RESP_CODE != 200 ]]; then
echo "Unable to download composer.json for branch $INTO_BRANCH - HTTP response code was $RESP_CODE"
exit 1
fi
CAN_MERGE_UP_COMPOSER_JSON=$(php -r '
$from = json_decode(file_get_contents("__composer_from.json"), true);
$into = json_decode(file_get_contents("__composer_into.json"), true);
if (!$from) {
throw new Exception("Could not parse __composer_from.json - " . json_last_error_msg());
}
if (!$into) {
throw new Exception("Could not parse __composer_into.json - " . json_last_error_msg());
}
if (array_key_exists("require-dev", $from)) {
unset($from["require-dev"]);
}
if (array_key_exists("require-dev", $into)) {
unset($into["require-dev"]);
}
echo json_encode($from) === json_encode($into) ? "1" : "0";
')
rm __composer_from.json
rm __composer_into.json
if [[ $CAN_MERGE_UP_COMPOSER_JSON == "0" ]]; then
echo "Unable to mergeup $FROM_BRANCH into $INTO_BRANCH - there are non require-dev changes in composer.json"
exit 1
fi
fi
done
# actions/checkout with fetch-depth: 0 will fetch ALL git history for the repository
Expand All @@ -137,6 +179,7 @@ runs:
fetch-depth: 0

- name: Git merge-up
id: git-merge-up
shell: bash
env:
BRANCHES: ${{ steps.determine.outputs.branches }}
Expand All @@ -149,6 +192,19 @@ runs:
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions"
# List of branches to trigger-ci for later
# Normally we'll only ever trigger 3 branches, though providing up to 6 for scenarios
# where there is are things like betas in the mix
# branches.php will throw an Exception early if there is more than 6 branches which will cause
# this job to fail without any merge-ups being performed
TRIGGER_CI_BRANCH_01=""
TRIGGER_CI_BRANCH_02=""
TRIGGER_CI_BRANCH_03=""
TRIGGER_CI_BRANCH_04=""
TRIGGER_CI_BRANCH_05=""
TRIGGER_CI_BRANCH_06=""
TRIGGER_CI_EXCEEDED=""
FROM_BRANCH=""
INTO_BRANCH=""
for BRANCH in $BRANCHES; do
Expand All @@ -160,8 +216,13 @@ runs:
echo "Attempting to merge-up $FROM_BRANCH into $INTO_BRANCH"
# Checkout both branches to ensure branch info is up to date
# Also runs `git pull` which should not be necessary, though in practice GitHub actions sometimes
# rejects when pushing saying the tip of the branch is out of date. This possibly only happens
# when running a failed job though that's still a valid scenario
git checkout $FROM_BRANCH
git pull
git checkout $INTO_BRANCH
git pull
# Determine if we will rebuild dist file during merge-up
# This is based simply on if there are changes in the client/ directory
Expand Down Expand Up @@ -317,22 +378,83 @@ runs:
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
if [[ $TRIGGER_CI_BRANCH_01 == "" ]]; then
TRIGGER_CI_BRANCH_01=$INTO_BRANCH
elif [[ $TRIGGER_CI_BRANCH_02 == "" ]]; then
TRIGGER_CI_BRANCH_02=$INTO_BRANCH
elif [[ $TRIGGER_CI_BRANCH_03 == "" ]]; then
TRIGGER_CI_BRANCH_03=$INTO_BRANCH
elif [[ $TRIGGER_CI_BRANCH_04 == "" ]]; then
TRIGGER_CI_BRANCH_04=$INTO_BRANCH
elif [[ $TRIGGER_CI_BRANCH_05 == "" ]]; then
TRIGGER_CI_BRANCH_05=$INTO_BRANCH
elif [[$TRIGGER_CI_BRANCH_06 == "" ]]; then
TRIGGER_CI_BRANCH_06=$INTO_BRANCH
else
echo "Succesfully triggered CI workflow for $INTO_BRANCH"
if [[ $TRIGGER_CI_EXCEEDED == "" ]]; then
TRIGGER_CI_EXCEEDED=$INTO_BRANCH
else
TRIGGER_CI_EXCEEDED="$TRIGGER_CI_EXCEEDED,$INTO_BRANCH"
fi
fi
done
echo "TRIGGER_CI_BRANCH_01 is $TRIGGER_CI_BRANCH_01"
echo "TRIGGER_CI_BRANCH_02 is $TRIGGER_CI_BRANCH_02"
echo "TRIGGER_CI_BRANCH_03 is $TRIGGER_CI_BRANCH_03"
echo "TRIGGER_CI_BRANCH_04 is $TRIGGER_CI_BRANCH_04"
echo "TRIGGER_CI_BRANCH_05 is $TRIGGER_CI_BRANCH_05"
echo "TRIGGER_CI_BRANCH_06 is $TRIGGER_CI_BRANCH_06"
echo "TRIGGER_CI_EXCEEDED is $TRIGGER_CI_EXCEEDED"
echo "trigger_ci_branch_01=$TRIGGER_CI_BRANCH_01" >> $GITHUB_OUTPUT
echo "trigger_ci_branch_02=$TRIGGER_CI_BRANCH_02" >> $GITHUB_OUTPUT
echo "trigger_ci_branch_03=$TRIGGER_CI_BRANCH_03" >> $GITHUB_OUTPUT
echo "trigger_ci_branch_04=$TRIGGER_CI_BRANCH_04" >> $GITHUB_OUTPUT
echo "trigger_ci_branch_05=$TRIGGER_CI_BRANCH_05" >> $GITHUB_OUTPUT
echo "trigger_ci_branch_06=$TRIGGER_CI_BRANCH_06" >> $GITHUB_OUTPUT
echo "trigger_ci_exceeded=$TRIGGER_CI_EXCEEDED" >> $GITHUB_OUTPUT
- name: Trigger CI 01
if: ${{ steps.git-merge-up.outputs.trigger_ci_branch_01 != '' }}
uses: silverstripe/gha-trigger-ci@v1
with:
branch: ${{ steps.git-merge-up.outputs.trigger_ci_branch_01 }}

- name: Trigger CI 02
if: ${{ steps.git-merge-up.outputs.trigger_ci_branch_02 != '' }}
uses: silverstripe/gha-trigger-ci@v1
with:
branch: ${{ steps.git-merge-up.outputs.trigger_ci_branch_02 }}

- name: Trigger CI 03
if: ${{ steps.git-merge-up.outputs.trigger_ci_branch_03 != '' }}
uses: silverstripe/gha-trigger-ci@v1
with:
branch: ${{ steps.git-merge-up.outputs.trigger_ci_branch_03 }}

- name: Trigger CI 04
if: ${{ steps.git-merge-up.outputs.trigger_ci_branch_04 != '' }}
uses: silverstripe/gha-trigger-ci@v1
with:
branch: ${{ steps.git-merge-up.outputs.trigger_ci_branch_04 }}

- name: Trigger CI 05
if: ${{ steps.git-merge-up.outputs.trigger_ci_branch_05 != '' }}
uses: silverstripe/gha-trigger-ci@v1
with:
branch: ${{ steps.git-merge-up.outputs.trigger_ci_branch_05 }}

- name: Trigger CI 06
if: ${{ steps.git-merge-up.outputs.trigger_ci_branch_06 != '' }}
uses: silverstripe/gha-trigger-ci@v1
with:
branch: ${{ steps.git-merge-up.outputs.trigger_ci_branch_06 }}

- name: Trigger CI Exceeded
if: ${{ steps.git-merge-up.outputs.trigger_ci_exceeded != '' }}
shell: bash
env:
TRIGGER_CI_EXCEEDED: ${{ steps.git-merge-up.outputs.trigger_ci_exceeded }}
run: |
echo "Exceeded the number of branches that can be have CI triggered"
echo "Branches where CI was not triggered is $TRIGGER_CI_EXCEEDED"
exit 1
33 changes: 31 additions & 2 deletions funcs.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

// This should always match default branch of silverstripe/framework
const CURRENT_CMS_MAJOR = 5;

function branches(
string $defaultBranch,
string $minimumCmsMajor,
Expand All @@ -21,7 +24,20 @@ function branches(
$defaultMajor = $matches[1];

// read __composer.json of the current (default) branch
$contents = $composerJson ?: file_get_contents('__composer.json');
if ($composerJson) {
$contents = $composerJson;
} elseif (file_exists('__composer.json')) {
$contents = file_get_contents('__composer.json');
} else {
// respository such as silverstripe/eslint-config or silverstripe/gha-auto-tag
// make some fake json so that this branch is treated as though it's latest supported major version
$contents = json_encode([
'require' => [
'silverstripe/framework' => '^' . CURRENT_CMS_MAJOR,
],
], JSON_UNESCAPED_SLASHES);
}

$json = json_decode($contents);
if (is_null($json)) {
$lastError = json_last_error();
Expand All @@ -44,7 +60,15 @@ function branches(
}
if (!$version) {
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'silverstripe/assets'} ?? '');
$matchedOnBranchThreeLess = true;
if ($version) {
$matchedOnBranchThreeLess = true;
}
}
if (!$version) {
$version = preg_replace('#[^0-9\.]#', '', $json->require->{'cwp/starter-theme'} ?? '');
if ($version) {
$version += 1;
}
}
if (preg_match('#^([0-9]+)+\.?[0-9]*$#', $version, $matches)) {
$defaultCmsMajor = $matches[1];
Expand Down Expand Up @@ -131,6 +155,11 @@ function branches(

// reverse the array so that oldest is first
$branches = array_reverse($branches);

// max of 6 branches - also update action.yml if you need to increase this limit
if (count($branches) > 6) {
throw new Exception('More than 6 branches to merge up. Aborting.');
}

return $branches;
}
Loading

0 comments on commit 9ca65c4

Please sign in to comment.