Commit a33ae5c0 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Lisa Seeto
Browse files

Bug 1886880: check if need to update composer dependencies

If the dependencies have been installed already, there's no need to
update dependencies unless the composer.json version values
are changed

behatnotneeded

Change-Id: I8c793f56fb3c35ae57ba67a060c321b8cf5d2af5
(cherry picked from commit b1becae9)
parent 92f8e952
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -153,8 +153,14 @@ try {
        }
        else {
            // Update behat and dependencies using composer
            if (needs_dependencies_update()) {
                cli::cli_print("Composer lock file out of date");
                testing_update_dependencies();
            }
            else {
                cli::cli_print("Composer lock file up to date");
            }
        }

        if ($cli->get_cli_param('inithtml')) {
            // Now composer is updated, apply custom styling to the html report
@@ -253,3 +259,5 @@ catch (Exception $e) {
}

exit(0);

+44 −0
Original line number Diff line number Diff line
@@ -203,6 +203,50 @@ function testing_update_dependencies() {
    }
}

/*
 * Will check if changes have been made in the composer.json file.
 * This is done by calculating the composer.json hash and then comparing it with the hash saved in composer.lock
 * The composer.lock file is generated everytime there is a change composer.json
 *
 * @return boolean
 */
function needs_dependencies_update() {
    $content = json_decode(file_get_contents(get_config('docroot') . '../external/composer.json'), true);
    // calculate composer.json hash manually because it's not saved in composer.lock like it used to be
    // copied the following code from the composer function that used to generate the hash
    $relevantKeys = array(
        'name',
        'version',
        'require',
        'require-dev',
        'conflict',
        'replace',
        'provide',
        'minimum-stability',
        'prefer-stable',
        'repositories',
        'extra',
    );

    $relevantContent = array();

    foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
        $relevantContent[$key] = $content[$key];
    }
    if (isset($content['config']['platform'])) {
        $relevantContent['config']['platform'] = $content['config']['platform'];
    }

    ksort($relevantContent);

    $json = md5(json_encode($relevantContent));

    $hash = 'content-hash';
    $lock = json_decode(file_get_contents(get_config('docroot') . '../external/composer.lock'))->$hash;

    return ($lock !== $json);
}

/**
* Copies mahara styling to external html folder
*/