Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add push:range:has CLI command #13

Merged
merged 3 commits into from Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -19,10 +19,10 @@ matrix:
if: type = push AND tag is blank
script: |
./test/online/driver travis-ci
if test -n "${TRAVIS_COMMIT_RANGE:-}"; then
if test -n "${TRAVIS_COMMIT_RANGE:-}" && test "${TRAVIS_COMMIT_RANGE%^*}" = "${TRAVIS_COMMIT_RANGE:-}"; then
./test/online/push "$TRAVIS_COMMIT" "$TRAVIS_BRANCH" "${TRAVIS_COMMIT_RANGE%...*}" ''
else
./test/online/push "$TRAVIS_COMMIT" "$TRAVIS_BRANCH" '' 'This is a branch creation event'
./test/online/push "$TRAVIS_COMMIT" "$TRAVIS_BRANCH" '' ''
fi
- name: Test tag events
if: tag is present
Expand Down
18 changes: 18 additions & 0 deletions bin/ci-info
Expand Up @@ -287,6 +287,24 @@ EOT
echo $state->getBranch(), "\n";
},
],
State::EVENT_PUSH . ':range:has' => [
'description' => <<<'EOT'
Check if the commit range is available.
In case it's not available, the exit code is 1, otherwise it's 0.
The reason why the range is not available is printed out to the standard error (use -q to prevent that).
EOT
,
'exec' => static function (): void {
$stateFactory = new StateFactory();
$state = $stateFactory->getCurrentState();
if ($state->getEvent() !== State::EVENT_PUSH) {
throw new RuntimeException('The current job is not for a push.');
}
if ($state instanceof PushWithoutBaseCommit) {
throw new RuntimeException($state->describeWhiBaseCommitIsUnavailable());
}
},
],
State::EVENT_PUSH . ':prev:sha1' => [
'description' => "Print the SHA-1 of the commit prior to the last commit for a push event",
'exec' => static function (): void {
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/GithubActions.php
Expand Up @@ -90,7 +90,7 @@ public function getState(Env $env): State
return new State\PushWithoutBaseCommit(
$matches[1],
$sha1,
new Exception\IncompleteEnvironmentException('This is a branch creation event: no commit range is available.')
new Exception\PushToNewBranchException()
);
}
throw new Exception\UnexpectedEnvironmentVariableValueException('GITHUB_REF', $ref);
Expand Down
12 changes: 4 additions & 8 deletions src/Driver/TravisCI.php
Expand Up @@ -140,23 +140,19 @@ protected function createPushState(Env $env): State\Push
$rawRange = $env->get('TRAVIS_COMMIT_RANGE');
if ($rawRange === '') {
// See https://github.com/travis-ci/docs-travis-ci-com/commit/2b9357a1701c4e33d364a41c8686db05751448ee
return new State\PushWithoutBaseCommit(
$branch,
$actualLastCommitSha1,
new Exception\IncompleteEnvironmentException('This is a branch creation event: no commit range is available.')
);
$rawRange = "{$actualLastCommitSha1}^...{$actualLastCommitSha1}";
}
$matches = null;
if (!preg_match('/^([0-9a-fA-F]{6,40})\.\.\.([0-9a-fA-F]{6,40})$/', $rawRange, $matches)) {
if (!preg_match('/^([0-9a-fA-F]{6,40}([~|\^].*)?)\.\.\.([0-9a-fA-F]{6,40})$/', $rawRange, $matches)) {
throw new Exception\UnexpectedEnvironmentVariableValueException('TRAVIS_COMMIT_RANGE', $rawRange);
}
$baseCommitSha1 = $matches[1];
$lastCommitSha1 = $matches[2];
$lastCommitSha1 = $matches[3];
if (stripos($actualLastCommitSha1, $lastCommitSha1) !== 0) {
throw new Exception\UnexpectedEnvironmentVariableValueException('TRAVIS_COMMIT_RANGE', $rawRange);
}
$lastCommitSha1 = $actualLastCommitSha1;
if (strlen($baseCommitSha1) < 40) {
if (strlen($baseCommitSha1) !== 40 || $matches[2] !== '') {
$git = new Git($this->getProjectRootDir($env));
try {
$baseCommitSha1 = $git->expandShortSha1($baseCommitSha1);
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/IncompleteEnvironmentException.php
Expand Up @@ -4,12 +4,10 @@

namespace CIInfo\Exception;

use CIInfo\Exception;

/**
* Exception thrown when the list of environment variables is not complete.
*/
class IncompleteEnvironmentException extends Exception
class IncompleteEnvironmentException extends PushWithoutBaseCommitException
{
public function __construct(string $message)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Exception/PushToNewBranchException.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace CIInfo\Exception;

/**
* Exception thrown when the list of environment variables is not complete.
*/
class PushToNewBranchException extends PushWithoutBaseCommitException
{
public function __construct()
{
parent::__construct('This is a branch creation event: no commit range is available.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/PushWithoutBaseCommitException.php
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace CIInfo\Exception;

use CIInfo\Exception;

/**
* Exception thrown when the base commit if a push event is not available.
*/
abstract class PushWithoutBaseCommitException extends Exception
{
public function __construct(string $message)
{
parent::__construct($message);
}
}
8 changes: 4 additions & 4 deletions src/State/PushWithoutBaseCommit.php
Expand Up @@ -4,24 +4,24 @@

namespace CIInfo\State;

use CIInfo\Exception\IncompleteEnvironmentException;
use CIInfo\Exception\PushWithoutBaseCommitException;

/**
* The state representing a push build when the base commit is not available.
*/
class PushWithoutBaseCommit extends Push
{
/**
* @var \CIInfo\Exception\IncompleteEnvironmentException
* @var \CIInfo\Exception\PushWithoutBaseCommitException
*/
private $baseCommitUnavilableDescription;

/**
* @param string $branch the name of the branch affected by the push
* @param string $lastCommitSha1 the SHA-1 of the head commit (that is, the last commit of the push)
* @param \CIInfo\Exception\IncompleteEnvironmentException a description about the problem detecting the base commit SHA-1
* @param \CIInfo\Exception\PushWithoutBaseCommitException a description about the problem detecting the base commit SHA-1
*/
public function __construct(string $branch, string $lastCommitSha1, IncompleteEnvironmentException $baseCommitUnavilableDescription)
public function __construct(string $branch, string $lastCommitSha1, PushWithoutBaseCommitException $baseCommitUnavilableDescription)
{
parent::__construct($branch, $lastCommitSha1, '');
$this->baseCommitUnavilableDescription = $baseCommitUnavilableDescription;
Expand Down
20 changes: 11 additions & 9 deletions test/online/push
Expand Up @@ -55,15 +55,17 @@ if test -z "$expectedBaseRangeFailure"; then
fi
printf 'done (%s).\n' "$baseSha1"
printf 'Checking previous commit SHA-1... '
if test ${#expectedBaseSHA1} -lt 40; then
if test "${baseSha1#$expectedBaseSHA1*}" = "$baseSha1"; then
printf "failed (expected '%s', got '%s')\n" "$expectedBaseSHA1" "$baseSha1" >&2
exit 1
fi
else
if test "$baseSha1" != "$expectedBaseSHA1"; then
printf "failed (expected '%s', got '%s')\n" "$expectedBaseSHA1" "$baseSha1" >&2
exit 1
if test -n "$expectedBaseSHA1"; then
if test ${#expectedBaseSHA1} -lt 40; then
if test "${baseSha1#$expectedBaseSHA1*}" = "$baseSha1"; then
printf "failed (expected '%s', got '%s')\n" "$expectedBaseSHA1" "$baseSha1" >&2
exit 1
fi
else
if test "$baseSha1" != "$expectedBaseSHA1"; then
printf "failed (expected '%s', got '%s')\n" "$expectedBaseSHA1" "$baseSha1" >&2
exit 1
fi
fi
fi
printf 'passed.\n'
Expand Down