From 14bc0c1e641ee93d50b2d6fced35fa6235606ba0 Mon Sep 17 00:00:00 2001 From: Joey Kimsey Date: Sat, 10 Aug 2024 15:49:18 -0400 Subject: [PATCH] bugfixes --- app/classes/installer.php | 8 ++-- gitlab/auto_merge_mr.sh | 76 ++++++++++++++++++++++++++++++++++++++ gitlab/update_PHP_deps.yml | 46 +++++++++++++++++++++++ install.php | 7 +++- 4 files changed, 131 insertions(+), 6 deletions(-) create mode 100644 gitlab/auto_merge_mr.sh create mode 100644 gitlab/update_PHP_deps.yml diff --git a/app/classes/installer.php b/app/classes/installer.php index 51e6569..e433a15 100644 --- a/app/classes/installer.php +++ b/app/classes/installer.php @@ -326,7 +326,7 @@ class Installer { } if ( empty( $module_data->class_object ) ) { - self::$errors[] = [ 'errorInfo' => 'Class not found: ' . $module_data->class ]; + self::$errors[] = [ 'errorInfo' => 'installPlugin: class_object not found: ' . $module_data->class ]; return false; } @@ -373,7 +373,7 @@ class Installer { $errors = []; if ( empty( $module_data->class_object ) ) { - self::$errors[] = [ 'errorInfo' => 'Class not found: ' . $module_data->class ]; + self::$errors[] = [ 'errorInfo' => 'uninstallPlugin: class_object not found: ' . $module_data->class ]; return false; } @@ -408,7 +408,7 @@ class Installer { } if ( empty( $module_data->class_object ) ) { - self::$errors[] = [ 'errorInfo' => 'Class not found: ' . $module_data->class ]; + self::$errors[] = [ 'errorInfo' => 'installModel class_object not found: ' . $module_data->class ]; return false; } @@ -475,7 +475,7 @@ class Installer { $errors = []; if ( empty( $module_data->class_object ) ) { - self::$errors[] = [ 'errorInfo' => 'Class not found: ' . $module_data->class ]; + self::$errors[] = [ 'errorInfo' => 'uninstallModel: class_object not found: ' . $module_data->class ]; return false; } diff --git a/gitlab/auto_merge_mr.sh b/gitlab/auto_merge_mr.sh new file mode 100644 index 0000000..8131b8d --- /dev/null +++ b/gitlab/auto_merge_mr.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env sh + +ACCESS_TOKEN="" +CURL_FLAGS="" +GITLAB_API="${CI_API_V4_URL}" +MERGE_IF_SUCCESSFUL="true" +MR_BRANCH="" +MR_TITLE="Auto-Merge-$(date '+%Y%m%d%H%M%S')" +PROJECT_ID="${CI_PROJECT_ID}" +SECONDS_BETWEEN_POOLING=10 +TARGET_BRANCH="main" +eval "${@}" + +if [ "${MR_BRANCH}" = '' ]; then + echo "[ERROR] No MR branch was given." + exit 1 +fi + +if [ "${ACCESS_TOKEN}" = '' ]; then + echo "[ERROR] No access token was given." + exit 2 +fi + +if [ "${MERGE_IF_SUCCESSFUL}" = "1" ] || [ "${MERGE_IF_SUCCESSFUL}" = "true" ]; then + MERGE_IF_SUCCESSFUL="true" +else + MERGE_IF_SUCCESSFUL="false" +fi + +MR_JSON=$(curl ${CURL_FLAGS} \ + --request POST \ + --header "PRIVATE-TOKEN: ${ACCESS_TOKEN}" \ + --data-urlencode "source_branch=${MR_BRANCH}" \ + --data-urlencode "target_branch=${TARGET_BRANCH}" \ + --data-urlencode "title=${MR_TITLE}" \ + --data-urlencode "remove_source_branch=true" \ + "${GITLAB_API}/projects/${PROJECT_ID}/merge_requests" +) +CURL_EXIT_CODE=$? + +if [ "${CURL_EXIT_CODE}" != "0" ]; then + echo "[ERROR] Please check the returned response for details." + echo "[INFO] CURL_EXIT_CODE=${CURL_EXIT_CODE}" + echo "[INFO] MR_JSON=${MR_JSON}" + exit ${CURL_EXIT_CODE} +fi + +echo "[SUCCESS] Merge request created successfully!" +MR_URL=$(echo "${MR_JSON}" | jq '.web_url') +echo "[INFO] Merge request URL: ${MR_URL}" + +if [ "${MERGE_IF_SUCCESSFUL}" = "true" ]; then + MR_ID=$(echo "${MR_JSON}" | jq '.iid') + + echo "[INFO] Will merge MR ID ${MR_ID} (branch '${MR_BRANCH}' into '${TARGET_BRANCH}'), if and when the pipeline passes." + echo "[INFO] Waiting for pipeline to finish..." + while true; do + PIPELINE_STATUS=$(curl ${CURL_FLAGS} --silent --header "PRIVATE-TOKEN: ${ACCESS_TOKEN}" \ + "${GITLAB_API}/projects/${PROJECT_ID}/merge_requests/${MR_ID}/pipelines" | jq '.[0].status') + + if [ "${PIPELINE_STATUS}" = "\"success\"" ]; then + echo "[INFO] Pipeline succeeded!" + # Merge the merge request + curl ${CURL_FLAGS} --request PUT --header "PRIVATE-TOKEN: ${ACCESS_TOKEN}" \ + "${GITLAB_API}/projects/${PROJECT_ID}/merge_requests/${MR_ID}/merge" + echo "[SUCCESS] Merge request merged successfully!" + exit 0 + elif [ "${PIPELINE_STATUS}" = "\"failed\"" ]; then + echo "[ERROR] Pipeline failed!" + exit 1 + else + echo "[INFO] Pipeline still running..." + sleep ${SECONDS_BETWEEN_POOLING} + fi + done +fi diff --git a/gitlab/update_PHP_deps.yml b/gitlab/update_PHP_deps.yml new file mode 100644 index 0000000..a5f6f6d --- /dev/null +++ b/gitlab/update_PHP_deps.yml @@ -0,0 +1,46 @@ +.update_PHP_deps: + variables: + TIMEZONE: "Europe/Amsterdam" # For the system in general + DATE_TIMEZONE: ${TIMEZONE} # For PHP + + GIT_DEPTH: 1 + GITLAB_API_URL: ${CI_API_V4_URL} + TARGET_BRANCH: ${CI_COMMIT_REF_NAME} # This is the branch chosen in the `Pipeline Schedule` + TARGET_REMOTE: "https://${GITLAB_USERNAME}:${GITLAB_ACCESS_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}.git" + + # These could/should be overridden in an extending job: + UPDATE_BRANCH_PREFIX: "update_PHP_deps_" # Used for the update branch name, it will be followed by the datetime + GIT_USER: "" # Used for the update commit + GIT_EMAIL: "" # Used for the update commit + GITLAB_USERNAME: "" # Used for pushing the new branch and opening the MR + GITLAB_ACCESS_TOKEN: "" # Used for pushing the new branch and opening the MR + MERGE_IF_SUCCESSFUL: "false" # Set to true, to merge automatically if the pipeline succeeds + SECONDS_BETWEEN_POOLING: 10 # Nbr of seconds between checking if the MR pipeline is successful, so then it will merge + JOB_GIT_FLAGS: "" + JOB_CURL_FLAGS: "" + JOB_COMPOSER_FLAGS: "" + interruptible: true # allows to stop the job if a newer pipeline starts, saving resources and allowing new jobs to start because job concurrency is limited + stage: "update_PHP_deps" + rules: + - if: '$SCHEDULED_PIPELINE == $CI_JOB_NAME' + script: # `git`, `jq` and `curl` are needed, so if they are not already installed, they should be installed in the `before_script` of the extending job + - git ${JOB_GIT_FLAGS} fetch origin ${TARGET_BRANCH} + - git ${JOB_GIT_FLAGS} checkout ${TARGET_BRANCH} + - export DATE_TIME="$(date '+%Y%m%d%H%M%S')" + - export MR_BRANCH="${UPDATE_BRANCH_PREFIX}${DATE_TIME}" + - git ${JOB_GIT_FLAGS} checkout -b "${MR_BRANCH}" + - composer update ${JOB_COMPOSER_FLAGS} + - if [ "$(git diff)" == "" ]; then echo "No updates needed!"; exit 0; fi + - export TITLE="Update PHP dependencies [${DATE_TIME}]" + - git ${JOB_GIT_FLAGS} commit -a -m "${TITLE}" + - git ${JOB_GIT_FLAGS} push "${TARGET_REMOTE}" "${MR_BRANCH}" + - | + vendor/hgraca/ci-cd-composer-update/src/GitLab/auto_merge_mr.sh \ + MERGE_IF_SUCCESSFUL="${MERGE_IF_SUCCESSFUL}" \ + MR_BRANCH="${MR_BRANCH}" \ + MR_TITLE="\"${TITLE}\"" \ + TARGET_BRANCH="${TARGET_BRANCH}" \ + ACCESS_TOKEN="${GITLAB_ACCESS_TOKEN}" \ + SECONDS_BETWEEN_POOLING="${SECONDS_BETWEEN_POOLING}" \ + GITLAB_API="${GITLAB_API_URL}" \ + CURL_FLAGS="${JOB_CURL_FLAGS}" diff --git a/install.php b/install.php index fad0f04..3b854ee 100644 --- a/install.php +++ b/install.php @@ -99,7 +99,7 @@ class Install extends Controller { Debug::error( 'install hash not found on file.' ); return false; } - if ( !Session::exists( 'installHash' ) && !Cookie::exists( 'installHash' ) ) { + if ( ! Session::exists( 'installHash' ) && ! Cookie::exists( 'installHash' ) ) { Debug::error( 'install hash not found in session or cookie.' ); return false; } @@ -207,6 +207,9 @@ class Install extends Controller { ], ], 'database' => [ + 'dbMaxQuery' => [ + 'value'=> 100, + ], 'dbEnabled' => [ 'value' => true, ], @@ -227,7 +230,7 @@ class Install extends Controller { ], ], ]; - if ( !TheTempusProject::$activeConfig->generate( CONFIG_JSON, $configMatrix ) ) { + if ( ! TheTempusProject::$activeConfig->generate( CONFIG_JSON, $configMatrix ) ) { return Issues::add( 'error', 'Config file already exists so the installer has been halted. If there was an error with installation, please delete app/config/config.json manually and try again. The installer should automatically bring you back to this step.' ); } Session::flash( 'success', 'Config saved successfully.' );