Skip to content

Commit

Permalink
LOPS-2300 - New command: workflow:wait:commit (#2591)
Browse files Browse the repository at this point in the history
* New command: waitForCommit

---------

Co-authored-by: Amanda Ferry <[email protected]>
Co-authored-by: Greg Anderson <[email protected]>
Co-authored-by: Brian Weaver <[email protected]>
  • Loading branch information
4 people authored Jun 6, 2024
1 parent dfad0c0 commit 95a34c5
Show file tree
Hide file tree
Showing 31 changed files with 6,127 additions and 142 deletions.
31 changes: 30 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
export PROJECT_PATH=$(realpath .)
export TERMINUS_SITE="terminus-test-site"
## Note: The terminus test site must have some quicksilver scripts enabled
## in order for tests to pass.
## Needed for unit test fixutures
export TERMINUS_PROJECT_ROOT=${PROJECT_PATH}

## This is the site that will be used for testing
export TERMINUS_SITE="ci-terminus-composer"

## This is the site that will be used for testing wordpress commands
export TERMINUS_SITE_WP="terminus-test-site-wordpress"

## This is the site that will be used for testing wordpress network
export TERMINUS_SITE_WP_NETWORK="terminus-test-site-wp-network"

## This is the site that will be used for cloning a test environment on which to run tests
export TERMINUS_ENV="dev"

## this is the org that will be used. The site(s) above need to be in this org
export TERMINUS_ORG="Agency"

## This is the user that will be used for testing Should be attached the token below
export TERMINUS_USER="[email protected]"

## To Autoload your token from your local machine, change the TERMINUS_TOKEN to the following command:
## export TERMINUS_TOKEN=$(cat $HOME/.terminus/cache/tokens/[email protected] | jq -r .token)
export TERMINUS_TOKEN="{TERMINUS TOKEN}"

## this is the folder that terminus will use to store its data during the tests
export TERMINUS_BASE_DIR="/tmp/terminus-data"

## this is the folder that terminus will use to store its plugins during the tests
export TERMINUS_PLUGINS2_DIR="${TERMINUS_BASE_DIR}/plugins"

## this is the folder that terminus will use to store its plugins during the tests
export TERMINUS_PLUGINS_DIR="${TERMINUS_BASE_DIR}/plugins-3.x"

## Used for testing plugins
export TERMINUS_DEPENDENCIES_BASE_DIR="${TERMINUS_BASE_DIR}/dependencies-1"

## this will prevent the tests from creating a test environment for the run
export TERMINUS_TESTING_RUNTIME_ENV=

## this will prevent tests from reinstalling the development packages once a phar is built
export TERMINUS_ON_PHAR_COMPLETE_REINSTALL_COMPOSER_WITH_DEV=1
4 changes: 2 additions & 2 deletions .github/workflows/3x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ jobs:
name: Checkout & build Phar
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Save repo content as artifact
uses: actions/upload-artifact@v4
with:
name: full-workspace
path: ${{ github.workspace }}
- name: Full Composer Install
run: composer install
run: composer install --dev
- name: Validate Code
run: composer code:lint
- name: Phar Build
Expand Down
137 changes: 137 additions & 0 deletions RoboFile.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use CzProject\GitPhp\Git;
use CzProject\GitPhp\GitException;
use Pantheon\Terminus\Config\ConfigAwareTrait;
use Pantheon\Terminus\Helpers\CommandCoverageReport;
use Pantheon\Terminus\Terminus;
Expand Down Expand Up @@ -206,6 +208,124 @@ public function setTerminus(Terminus $terminus): void
$this->terminus = $terminus;
}

/**
* Generates a test commit.
*
* @throws \Pantheon\Terminus\Exceptions\TerminusException
* @throws GitException
*/
public function generateTestCommit()
{
$this->output()->writeln('Getting the Site Repo');
// get the git host and port from terminus
$commandResponse = $this->getTerminus()->execute(
'%s connection:info %s.dev --fields=git_host,git_port --format=json',
[
$this->getProjectPath() . "/terminus.phar",
$this->getSiteName(),
]
);

// check if the command was successful
if ($commandResponse[1] !== 0) {
$this->output()->writeln('Failed to retrieve git host and port');
exit(1);
}

// decode the json response
$gitInfo = json_decode($commandResponse[0], true);
$this->output()->writeln('Retrieved git host and port' . print_r($gitInfo, true));

// check if the git host and port were retrieved
if (!isset($gitInfo['git_host']) || !isset($gitInfo['git_port'])) {
$this->output()->writeln('Failed to retrieve git host and port');
exit(1);
}

// Does the known_hosts file exist?
if (!file_exists(sprintf("%s/.ssh/known_hosts", getenv("HOME")))) {
// if not, create one
touch(sprintf("%s/.ssh/known_hosts", getenv("HOME")));
}

// get the contents of the known_hosts file
$knownHosts = file_get_contents(sprintf("%s/.ssh/known_hosts", getenv("HOME")));
// check if the git host is already in the known_hosts file
if (!str_contains($knownHosts, $gitInfo['git_host'])) {
// if not, add it
$this->output()->writeln('Adding the git host to known hosts file');
$addGitHostToKnownHostsCommand = sprintf(
'ssh-keyscan -p %d %s >> ~/.ssh/known_hosts',
$gitInfo['git_port'],
$gitInfo['git_host']
);
$this->output()->writeln($addGitHostToKnownHostsCommand);
exec($addGitHostToKnownHostsCommand);
}

// checkout the branch related to this test run
$clonedPath = sprintf(
"%s/pantheon-local-copies/%s",
getenv("HOME"),
$this->getSiteName()
);
if (is_dir($clonedPath)) {
// make sure you're working with a clean copy of the repo
exec("rm -rf {$clonedPath}");
}
$this->output()->writeln(sprintf('Cloning the site repository to %s', $clonedPath));
// get the git host and port from terminus
$commandResponse = $this->getTerminus()->execute(
'%s local:clone %s',
[
$this->getProjectPath() . "/terminus.phar",
$this->getSiteName(),
]
);

$response = "";
try {
$git = new Git();
$repo = $git->open($clonedPath);
chdir($clonedPath);
$branches = $repo->getBranches();
if (!in_array($this->getSiteEnv(), $branches)) {
$this->output()->writeln(sprintf('Creating the %s branch', $this->getSiteEnv()));
// Create the branch
$repo->createBranch($this->getSiteEnv());
}
// Check out the branch in question
$repo->checkout($this->getSiteEnv());
// create a text file
$testFilePath = sprintf('%s/test.txt', $clonedPath);
file_put_contents($testFilePath, 'test');
// add the file to the repository
$repo->addFile("test.txt");
// commit the file
$repo->commit('Test commit');
// push the commit
$response = $repo->execute(
'push',
'origin',
$this->getSiteEnv(),
);
} catch (GitException $e) {
$this->output()->writeln(["Git Exception:", $e->getMessage()]);
$this->output()->writeln(print_r($response, true));
exit(1);
} catch (Exception $e) {
$this->output()->writeln($e->getMessage());
$this->output()->writeln(print_r($response, true));
exit(1);
}

// get the last commit
$commit = $repo->getLastCommit();
// output the commit id
$this->output()->writeln($commit->getId());
return $commit->getId();
}

/**
* Returns the absolute path to the project.
*
Expand All @@ -215,4 +335,21 @@ private function getProjectPath(): string
{
return dirname(__FILE__);
}


/**
* @return string
*/
private function getSiteName(): string
{
return getenv('TERMINUS_SITE') ?? 'ci-terminus-composer';
}

/**
* @return string
*/
private function getSiteEnv(): string
{
return getenv('TERMINUS_ENV') ?? 'dev';
}
}
2 changes: 1 addition & 1 deletion bin/terminus
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (!getenv('TERMINUS_ALLOW_UNSUPPORTED_NEWER_PHP') && version_compare(PHP_VERSI

// This variable is automatically managed via updateDependenciesversion() in /RoboFile.php,
// which is run after every call to composer update.
$terminusPluginsDependenciesVersion = '797fd58621';
$terminusPluginsDependenciesVersion = '8f626ec369';

// Cannot use $_SERVER superglobal since that's empty during phpunit testing
// getenv('HOME') isn't set on Windows and generates a Notice.
Expand Down
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"consolidation/comments": "^1.0.2",
"consolidation/filter-via-dot-access-data": "^2.0",
"consolidation/output-formatters": "^4",
"consolidation/robo": "^3",
"consolidation/self-update": "^2.0.4",
"consolidation/site-alias": "^4.0",
"czproject/git-php": "^4.0",
Expand All @@ -33,7 +32,8 @@
"symfony/finder": "^5",
"symfony/process": "^5",
"symfony/yaml": "^5",
"twig/twig": "^3.3"
"twig/twig": "^3.3",
"consolidation/robo": "^3.0"
},
"require-dev": {
"ext-pcov": "*",
Expand Down Expand Up @@ -115,14 +115,17 @@
"test:behat": [
"SHELL_INTERACTIVE=true TERMINUS_TEST_MODE=1 behat --colors --config tests/config/behat.yml --stop-on-failure --suite=default"
],
"tests:unit": [
"vendor/bin/phpunit --colors=always -c ./phpunit.xml --debug --testsuite unit --do-not-cache-result --verbose --stop-on-failure"
],
"test:short": [
"XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always -c ./phpunit.xml --debug --group=short --do-not-cache-result --verbose --stop-on-failure"
"XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always -c ./phpunit.xml --debug --testsuite functional --group=short --do-not-cache-result --verbose --stop-on-failure"
],
"test:long": [
"XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always -c ./phpunit.xml --debug --group=long --do-not-cache-result --verbose"
"XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always -c ./phpunit.xml --debug --testsuite functional --group=long --do-not-cache-result --verbose"
],
"test:functional": [
"XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always -c ./phpunit.xml --debug --group=short,long --do-not-cache-result --verbose",
"XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always -c ./phpunit.xml --debug --testsuite functional --group=short,long --do-not-cache-result --verbose",
"@coverage"
],
"test:all": [
Expand Down Expand Up @@ -156,7 +159,7 @@
"php-cs-fixer": [
"vendor/bin/php-cs-fixer fix ./src --rules=@PSR12",
"vendor/bin/php-cs-fixer fix ./tests/Functional --rules=@PSR12",
"vendor/bin/php-cs-fixer fix ./tests/unit_tests --rules=@PSR12"
"vendor/bin/php-cs-fixer fix ./tests/Unit --rules=@PSR12"
]
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/constants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ TERMINUS_CLIENT_OPTIONS:
http_errors: false

TERMINUS_WAIT_FOR_WAKE_REPEAT: 25
TERMINUS_REFRESH_WORKFLOW_DELAY: 25
10 changes: 9 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="tests/config/bootstrap.php" colors="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/config/bootstrap.php"
colors="true">
<testsuites>
<testsuite name="functional">
<directory suffix="Test.php">tests/Functional/</directory>
</testsuite>
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit/Collections/</directory>
<directory suffix="Test.php">tests/Unit/Models/</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true" cacheDirectory="reports">
<include>
Expand All @@ -25,5 +32,6 @@
<server name="TERMINUS_VERIFY_HOST_CERT" value="${TERMINUS_VERIFY_HOST_CERT}" />
<server name="TERMINUS_CACHE_DIR" value="${TERMINUS_CACHE_DIR}" />
<server name="TERMINUS_DEBUG" value="${TERMINUS_DEBUG}" />
<server name="TERMINUS_PROJECT_ROOT" value="${TERMINUS_PROJECT_ROOT}" />
</php>
</phpunit>
Empty file removed reports/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion scripts/phar_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo "Installing composer dependencies with --no-dev..."
composer install --no-dev

echo "Building terminus.phar..."
box compile
"${HOME}"/box/vendor/bin/box compile
echo "terminus.phar file has been created successfully!"

if [[ -n "${TERMINUS_ON_PHAR_COMPLETE_REINSTALL_COMPOSER_WITH_DEV}" ]]; then
Expand Down
Loading

0 comments on commit 95a34c5

Please sign in to comment.